3

I am stuck with a issue in managing Multiple Recyclerview recycling inside a NestedScrollView. Let me tell what I am trying to do -

  1. I have two frame layouts i.e. frame1 and frame2.
  2. I have two fragments containing recyclerview , First fragment's recyclerview showing items horizontally while second fragment's recyclerview showing list Vertically.
  3. Now I have put both FrameLayout inside a NestedScroolView, frame1 recyclerview is recycling all the view's properly but frame2 recylerview is not recycling the view , dont know why ? it first load all items then shows on screen.

Some Code :

MainActivity.java

 FragmentTransaction transaction;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentA frag1=new FragmentA();
FragmentB frag2=new FragmentB();
transaction =    getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame1, frag1);
    transaction.addToBackStack(frag1.getClass().getName());
    transaction.commit();

 transaction =   getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame2, frag2);
    transaction.addToBackStack(frag2.getClass().getName());
    transaction.commit();
}

main.xml

  <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

 <android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/frame1"
            android:layout_width="match_parent"
            android:layout_height="185dp" />

        <FrameLayout
            android:id="@+id/frame2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             />

</LinearLayout>
</android.support.v4.widget.NestedScrollView>
    </LinearLayout>

FragmentA :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v= inflater.inflate(R.layout.recycler_view, container, false);
    mDataListView = (RecyclerView) v.findViewById(R.id.data_list_view);
    mDataListView .setHasFixedSize(true);
    final GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), getActivity().getResources().getInteger(R.integer.playlist_categories_columns), GridLayoutManager.VERTICAL, false);

    mDataListView .setNestedScrollingEnabled(false);
   }
    }));


    return v;
}

FragmentB :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v= inflater.inflate(R.layout.recycler_view, container, false);
    mDataListView = (RecyclerView) v.findViewById(R.id.data_list_view);
    mDataListView .setHasFixedSize(true);
     final GridLayoutManager gridLayoutManager = new    GridLayoutManager(getActivity(), 1, GridLayoutManager.HORIZONTAL, false);
    mDataListView .setLayoutManager(gridLayoutManager);

    mDataListView .setNestedScrollingEnabled(false);
   }
    }));


    return v;
}

recycler_view.xml

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v7.widget.RecyclerView    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/data_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

I hope I am clear with my question.

Kapil Rajput
  • 10,723
  • 7
  • 40
  • 59
  • Did you find a solution to it ? I'm stuck in a similar situation where I have 2 horizontal RV's and a vertical RV inside a nestedScrollView. Items of horizontal RV are getting recycled but items of vertical RV are not :/ – Shubhral Jan 06 '17 at 07:41
  • @Shubhral i posted an answer please have a look, I shared how i figure out the problem. may it helps you – Kapil Rajput Jan 06 '17 at 09:50

1 Answers1

0

From my Experience I found that view recycling is not possible with multiple recyclerview's inside a NestedScrollView.

But I figure out the solution to show multiple Recyclerview's as per my requirement i want to show one Horizontal Recyclerview on top and one Vertical Recyclerview below it so what i did I am sharing may it helps someone.

I removed the NestedScrollView as parent of Recyclerview's and made Horizontal Recyclerview as HeaderView of Vertical Recyclerview.

code :

public class ListHeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
ArrayList<String> data;

public ListHeaderAdapter (ArrayList<String> data) {
    this.data = data;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_ITEM) {
        //inflate your layout and pass it to view holder
        return new VHItem(null);
    } else if (viewType == TYPE_HEADER) {
        //inflate your layout and pass it to view holder
        return new VHHeader(null);
    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
 int pos = position - 1; // for handling of Header view(use pos to fetch data from list)

    if (holder instanceof VHItem) {
        String dataItem = data.get(pos);
        //cast holder to VHItem and set data
    } else if (holder instanceof VHHeader) {
        //cast holder to VHHeader and set data for header.
    }
}

@Override
public int getItemCount() {
    return data.size()+ 1; // for handling of Header view
}

@Override
public int getItemViewType(int position) {
    if (isHeader(position))
        return TYPE_HEADER;

    return TYPE_ITEM;
}

public boolean isHeader(int position) {
    return position == 0;
}


class VHItem extends RecyclerView.ViewHolder {
    TextView title;

    public VHItem(View itemView) {
        super(itemView);
    }
}

class VHHeader extends RecyclerView.ViewHolder {
    Button button;

    public VHHeader(View itemView) {
        super(itemView);
    }
}
}

Note : same logic can be implemented to show Horizontal RecyclerView's at any Position

Kapil Rajput
  • 10,723
  • 7
  • 40
  • 59