2

I have very small question here, in my recycle view adapter class i'm using List<FeaturedTags> and its working fine.

Now we have newly introduced class called 'FeaturedLangTags, the only difference between FeaturedTags & FeaturedLangTagsis just an addition of Lang field. But we are not using this Lang field anyway to show on screen.

The output of the recycle view looks exactly similar to existing FeaturedTags adapter. Here i want to know how i can re-use the existing adapter class to display List<FeaturedLangTags> items?

One simple way is to duplicate the existing adapter and pass the FeaturedLangTagslist, but here so much of code is getting duplicated. I would like to know how i can tweek the existing class?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Naruto
  • 9,476
  • 37
  • 118
  • 201

5 Answers5

1

Create Adapter of generic List<T> which can be used in both condition.

public abstract class GenericAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private ArrayList<T> items;

    public abstract RecyclerView.ViewHolder setViewHolder(ViewGroup parent);

    public abstract void onBindData(RecyclerView.ViewHolder holder, T val);

    public GenericAdapter(Context context, ArrayList<T> items){
        this.context = context;
        this.items = items;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = setViewHolder(parent);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        onBindData(holder,items.get(position));
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public void addItems( ArrayList<T> savedCardItemz){
        items = savedCardItemz;
        this.notifyDataSetChanged();
    }

    public T getItem(int position){
        return items.get(position);
    }
}


adapter = new GenericAdapter<DataModel>(context,modelList) {
        @Override
        public RecyclerView.ViewHolder setViewHolder(ViewGroup parent) {
            final View view =            LayoutInflater.from(context).inflate(R.layout.item_view_holder, parent, false);
            ItemViewHolder viewHolder = new ItemViewHolder(context, view);
            return viewHolder;
        }

        @Override
        public void onBindData(RecyclerView.ViewHolder holder1, DataModel val) {
                DataModel userModel = val;

                ItemViewHolder holder = (ItemViewHolder)holder1;
                holder.name.setText(userModel.getName());
                holder.fatherName.setText(userModel.getFatherName());
        }
    };
    mRecyclerView.setAdapter(adapter);
PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35
0

Create a common interface or superclass to FeaturedTags and FeaturedLangTags, then use the same adapter.

Or use a more generic approach to your adapter like AdapterDelegates.

fweigl
  • 21,278
  • 20
  • 114
  • 205
0

You can create two different constructors for the adapter and achieve it.

0

You can have both the classes implement one common interface say CommonInterface

Make your list of type

YourList<CommonInterface>

In your adapter, override the

getItemViewType

Method. Here use the instance of operator to check what type of class it is and return a view type integer. Say for without lang, it will be an integer 0 and with it, 1

Now in your onCreateViewHolder, you get passed the viewType as the second parameter. Using that inflate your layout as your choice for the view.

In your onBindViewHolder, you can check

getItemViewType 

On the holder passed in the parameter and cast your interface explicitly to the object you want. Then use it as usual.

Reference for this multiple view types is Recycler view multiple view types

Kushan
  • 5,855
  • 3
  • 31
  • 45
0

You can use ConcatAdapter of RecyclerView to concatenate your adapters and reuse them.

    MyAdapter adapter1 = ...;
    AnotherAdapter adapter2 = ...;
    ConcatAdapter concatenated = new ConcatAdapter(adapter1, adapter2);
    recyclerView.setAdapter(concatenated);

"Favor composition over inheritance"

Ali
  • 331
  • 4
  • 15