3

I have fragment "Incoming" on one slide of a viewpager. This fragment contains a RecyclerView populated with custom-relative-layouts. The LinearLayoutManager orientation is Vertical.

I have a second fragment "Find" on the next slide of the said viewpager. "Find" will consist of two recyclerviews. It will have a Horizontal recyclerview filled with cardviews (fast loading of profile pictures). Underneath that, I am loading more slowly another recyclerview with a custom-relative-layout, the same as in the "incoming" fragment.

Does that make sense? I'll elaborate some more:

The question is for these three recyclerviews, should I declare a new RecyclerAdapter for each one? The reason I ask is that they'll all have unknown variable item_counts.

Here is the RecyclerAdapter I have for "Incoming":

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
    private Context mContext;

    public RecyclerAdapter(Context context, List<Incoming> items) {
        mContext = context;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View v) {
            super(v);
            // Define all of the components in the view
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater mInf = LayoutInflater.from(mContext);
        View customView = mInf.inflate(R.layout.item_layout_incoming, parent, false);

        final ViewHolder vh = new ViewHolder(customView);

        return vh;
    }

    @Override
    public int getItemCount(){ return 6; } // THIS IS TEMPORARY; WILL BE VARIABLE

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // Replace contents
    }

For my criteria, should I create another Adapter for my horizontal-cardview-recyclerview? It seems repetitive, but otherwise, how would I handle either inflating cardview or item_layout_incoming?

Seems like there should be a DRY way to do this, without hits to performance. Thanks

tonys
  • 3,855
  • 33
  • 39
Script Kitty
  • 1,737
  • 5
  • 27
  • 47

1 Answers1

4

You are using fragments so you will create 2 objects of that class. So it's the same thing. you just reduce compiler load by reducing the task of loading the new class into memory and then create its object. It's better to use two different Adapter because of 2 reasons.

  1. Your code will become ugly I mean so much congested and so many if else condition.
  2. In future, if you need to change something in layouts then again it will affect all objects if same adapter class.

So my advice do developer friendly code and create two classes.

aashish tamsya
  • 4,903
  • 3
  • 23
  • 34
Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55
  • Thanks man. That makes sense. I am going to create a new recyclerview for my card views. Should I reuse the same adapter for recycler 1 and 3 (with custom views)¿ – Script Kitty Apr 27 '16 at 16:01
  • If they both have same data and view (small changes can be ignored) then yes you can. – Wasim K. Memon Apr 27 '16 at 17:22