带有多个ArrayLists的Sticky Header RecyclerView

时间:2016-09-05 07:28:37

标签: android

我正在使用RecyclerView实现粘性标头。我有三个部分(即3个粘性标题)并且它们工作正常。现在我已经在每个部分中使用了三个arraylists,我已经在我的适配器中初始化了这些列表并且我试图根据头部标识内部获取这些列表的数据onBindViewHolder。但它没有给我完整的清单,只是每个清单中的一个字符串(即第一部分 - mylist的第一个位置的数据,,第二部分的数据 - mylist1的第二个位置的数据---第三部分下的数据 - - 关于mylist的第三个位置的数据2)

请帮助!!

上下文中的代码:

StickyTestAdapter

public class StickyTestAdapter extends RecyclerView.Adapter<StickyTestAdapter.ViewHolder> implements
        StickyHeaderAdapter<StickyTestAdapter.HeaderHolder> {

    private Context mContext;
    private ArrayList<String> mylist;
    private ArrayList<String> mylist1;
    private ArrayList<String> mylist2;
    private static  int countposition;
    private String HEADER_FIRIST="HEADER_FIRIST";
    private String HEADER_SECOND="HEADER_SECOND";
    private String HEADER_THIRD="HEADER_THIRD";

    public StickyTestAdapter(Context context) {
        prepareData();
        prepareData1();
        prepareData2();
        this.mContext=context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.item_test, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {


       long rowType= getHeaderId(position);
        Log.e("getHeaderId----",""+rowType);

        if (rowType==0){

            if(!mylist.equals(""))
            {
                Log.e("list_data----", "" + mylist.get(position));
                viewHolder.item.setText(mylist.get(position));
            }
            else
            {
                Log.e("Error--0--", "" + "error");
            }


        } else if (rowType==1){

            if(!mylist1.equals(""))
            {
                Log.e("list_data1----", "" + mylist1.get(position));
                viewHolder.item.setText(mylist1.get(position));
            }
            else
            {
                Log.e("Error---1-", "" + "error");
            }

        } else if (rowType==2){

            if(!mylist2.equals(""))
            {
                Log.e("list_data2----", "" + mylist2.get(position));
                viewHolder.item.setText(mylist2.get(position));
            }
            else
            {
                Log.e("Error----2", "" + "error");
            }
        }
    }


    @Override
    public int getItemCount() {

        if (getHeaderId(countposition)==0){
            Log.e("mylist",""+mylist.size());
            return mylist.size();


        }else  if (getHeaderId(countposition)==1){
            Log.e("mylist1",""+mylist1.size());
            return mylist1.size();
        }else if (getHeaderId(countposition)==2){
            Log.e("mylist2",""+mylist2.size());
            return mylist2.size();
        }
        return 0;
    }

    @Override
    public long getHeaderId(int pos) {
        return pos;
    }

    @Override
    public HeaderHolder onCreateHeaderViewHolder(ViewGroup parent) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.header_test, parent, false);

        return new HeaderHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(HeaderHolder viewholder, int count) {
       countposition=count;

        if (getItemViewType(count)==0){
            viewholder.headertext.setText(HEADER_FIRIST);

        }else  if (getItemViewType(count)==1){
            viewholder.headertext.setText(HEADER_SECOND);

        }else if (getItemViewType(count)==2){
            viewholder.headertext.setText(HEADER_THIRD);
        }

    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView item;

        public ViewHolder(View itemView) {
            super(itemView);

            item = (TextView)itemView.findViewById(R.id.text_item);
        }
    }

    static class HeaderHolder extends RecyclerView.ViewHolder {
        public TextView headertext;

        public HeaderHolder(View itemView) {
            super(itemView);

            headertext = (TextView)itemView.findViewById(R.id.header_text);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }



    public void prepareData()
    {
        mylist=new ArrayList<>();
        mylist.add("rajendra");
        mylist.add("rani");
        mylist.add("rahul");
    }

    public void prepareData1()
    {
        mylist1=new ArrayList<>();
        mylist1.add("ravi");
        mylist1.add("vikram");
        mylist1.add("rakesh");

    }

    public void prepareData2()
    {
        mylist2=new ArrayList<>();
        mylist2.add("apple");
        mylist2.add("ashok");
        mylist2.add("vikash");

    }


}

3 个答案:

答案 0 :(得分:4)

问题已经很久了。但是这些代码看起来很复杂,我个人试图不通过创建库来重新发明其他人做得很好。 GitHub充满了您可以导入的源代码。一些示例: FlexibleAdapter FastAdapter Epoxy 等。

我已经建立了第一个,但你也可以尝试其他的。 有了我的,有多个视图和带有节的标题非常简单,我在这里指出wiki page我在哪里定义节以及如何初始化它。

除此功能外,您还拥有很多更多功能,让您的生活更轻松。

答案 1 :(得分:4)

我根据联系人列表等字母将arraylist分为3部分。

为此,我使用SectionedRecyclerViewAdapter

MainActivity.java

public class MainActivity extends AppCompactActivity {

private SectionedRecyclerViewAdapter sectionAdapter;

@Override
public View onCreate(Bundle savedInstanceState) {

    setContentView(R.layout.activity_main);
    sectionAdapter = new SectionedRecyclerViewAdapter();


    for(char alphabet = 'a'; alphabet <= 'z';alphabet++) {
        List<String> contacts = getContactsWithLetter(alphabet);

        if (contacts.size() > 0) {
            sectionAdapter.addSection(new ContactsSection(String.valueOf(alphabet), contacts));
        }
    }

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.setAdapter(sectionAdapter);

    return view;
}

@Override
public void onResume() {
    super.onResume();

    if (getActivity() instanceof AppCompatActivity) {
        AppCompatActivity activity = ((AppCompatActivity) getActivity());
        if (activity.getSupportActionBar() != null)
            activity.getSupportActionBar().setTitle(R.string.nav_example1);
    }
}

private List<String> getContactsWithLetter(char letter) {
    List<String> contacts = new ArrayList<>();

    for (String contact : getResources().getStringArray(R.array.names_)) {
        if (contact.charAt(0) == letter) {
            contacts.add(contact);
        }
    }

    return contacts;
}

private class ContactsSection extends StatelessSection {

    String title;
    List<String> list;

    ContactsSection(String title, List<String> list) {
        super(new SectionParameters.Builder(R.layout.section_ex1_item)
                .headerResourceId(R.layout.section_ex1_header)
                .build());

        this.title = title;
        this.list = list;
    }

    @Override
    public int getContentItemsTotal() {
        return list.size();
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ItemViewHolder itemHolder = (ItemViewHolder) holder;

        String name = list.get(position);

        itemHolder.tvItem.setText(name);
        itemHolder.imgItem.setImageResource(name.hashCode() % 2 == 0 ? R.drawable.ic_face_black_48dp : R.drawable.ic_tag_faces_black_48dp);

        itemHolder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), String.format("Clicked on position #%s of Section %s", sectionAdapter.getPositionInSection(itemHolder.getAdapterPosition()), title), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new HeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        HeaderViewHolder headerHolder = (HeaderViewHolder) holder;

        headerHolder.tvTitle.setText(title);
    }
}

private class HeaderViewHolder extends RecyclerView.ViewHolder {

    private final TextView tvTitle;

    HeaderViewHolder(View view) {
        super(view);

        tvTitle = (TextView) view.findViewById(R.id.tvTitle);
    }
}

private class ItemViewHolder extends RecyclerView.ViewHolder {

    private final View rootView;
    private final ImageView imgItem;
    private final TextView tvItem;

    ItemViewHolder(View view) {
        super(view);

        rootView = view;
        imgItem = (ImageView) view.findViewById(R.id.imgItem);
        tvItem = (TextView) view.findViewById(R.id.tvItem);
    }
}
}

enter image description here

答案 2 :(得分:1)

使用类似于

的结构
    public class MultiArray<T> {

    List<ItemGroup> lists = new ArrayList<>();

    public void addList(String headerText, List<T> list) {
        lists.add(new ItemGroup(headerText, list));
    }

    public int itemCount() {
        int count = 0;
        for (ItemGroup group : lists) {
            count += group.count();
        }
        return count;
    }

    public T getItem(int position) {
        int count = 0;
        for (ItemGroup group : lists) {
            if (count + group.count() >= position) {
                return group.item(position - count);
            }
            count += group.count();

        }
        return null;
    }

    public int getGroupIndex(int position) {
        int count = 0;
        int groupIndex = 0;
        for (ItemGroup group : lists) {
            if (count + group.count() >= position) {
                return groupIndex;
            }
            count += group.count();
            groupIndex++;
        }
        return -1;
    }

    public String getHeaderText(int position){
        int count = 0;

        for (ItemGroup group : lists) {
            if (count + group.count() >= position) {
                return group.headerText;
            }
            count += group.count();
        }
        return "";
    }


    class ItemGroup {
        public final String headerText;
        public final List<T> list;

        public ItemGroup(String headerText, List<T> list) {
            this.headerText = headerText;
            this.list = list;
        }

        public int count() {
            return list.size();
        }

        public T item(int position) {
            return list.get(position);
        }
    }

}

您可以优化它以获得更快的性能