-2

I have an app that depends heavily on recyclerview.. Each activity has different model and layout for them.. So do I need to write separate adapters for all of them.?? Or could I have a base adapter which can have on create view holder, onbind view holder which would reduce the amount of repetitive code..PS. I also need onclick listener so, I wanted to include that in the base adapter.. What is the best way?? And if I can write a base adapter please give me some code samples.. Thanks in Advance...

f0f1
  • 35
  • 4

2 Answers2

0

Each activity has different model and layout for them.. So do I need to write separate adapters for all of them

Adapters are responsible for providing views that represent items in a data set, used by the RecyclerView. Now if those items in your ReyclerView are same across all the Activities then you can just have a single RecyclerView.Adapter

I also need onclick listener so, I wanted to include that in the base adapter.. What is the best way??

You can check this SO post for detailed implementation, but I am summarizing it briefly.

RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.addOnItemTouchListener(
    new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
      @Override public void onItemClick(View view, int position) {
        // do whatever
      }
    })
);

And provide RecyclerItemClickListener class, that implements RecyclerView.OnItemTouchListener interface

And if I can write a base adapter please give me some code samples.

You need to clarify what exactly needs to be implemented in BaseAdapter for anyone to help you

Community
  • 1
  • 1
AADProgramming
  • 6,077
  • 11
  • 38
  • 58
0

yes it is possible

enter code here holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (modelArrayList.get(position).heading.equals("maps")){
                Intent intent = new Intent(holder.button.getContext(),MainActivity2.class);

                holder.button.getContext().startActivity(intent);

            }
            if (modelArrayList.get(position).heading.equals("calls")){
                Intent intent = new Intent(holder.button.getContext(),MainActivity3.class);

                holder.button.getContext().startActivity(intent);

            }