Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sticky Header RecyclerView with multiple ArrayLists

Tags:

android

I am implementing sticky header with RecyclerView. I have three sections (i.e 3 sticky headers) and they are working fine.Now I have taken three arraylists inside each section, I have initialized these list in my adapter and I am trying to get data of these lists on basis of header id inside onBindViewHolder. But it is not giving me the full list,just one string from each list (i.e under first section--data on first position of mylist,,,under second section-- data on second position of mylist1 ---under third section-- data on third position of mylist2)

Please Help !!

Code in Context:

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");

    }


}
like image 521
Siddharth Mishra Avatar asked Sep 05 '16 07:09

Siddharth Mishra


1 Answers

Question is quite old. But that code looks complicated to read, personally I try to not reinvent what others did quite well by creating libraries. GitHub is full of source that you can import. Some examples: FlexibleAdapter, FastAdapter, Epoxy and others.

I have build the first one, but you can try the others as well. With mine, having multiple views and headers with sections is quite easy, I point here the wiki page where I define the section and how to initialize it.

Along with this feature, you have a lot more functionalities that makes your life easier.

like image 77
Davideas Avatar answered Sep 26 '22 06:09

Davideas