问题描述
我在管理 NestedScrollView
内的多个 Recyclerview
回收方面遇到问题.让我告诉我我想做什么-
- 我有两种框架布局,即frame1和frame2.
- 我有两个包含recyclerview的片段,第一个片段的recyclerview水平显示项目,而第二个片段的recyclerview纵向显示列表.
- 现在我已经将两个
FrameLayout
放在一个NestedScroolView
内,frame1 recyclerview正在正确地回收所有视图,但是frame2 recylerview没有在回收该视图,不知道为什么吗?首先加载所有项目,然后在屏幕上显示.
某些代码:
MainActivity.java
FragmentTransaction事务;@Override受保护的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
< LinearLayoutxmlns: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.NestedScrollViewandroid:layout_width ="match_parent"android:layout_height ="match_parent"android:fillViewport ="true"app:layout_behavior ="@ string/appbar_scrolling_view_behavior">< LinearLayoutandroid:layout_width ="match_parent"android:layout_height ="wrap_content"android:orientation ="vertical">< FrameLayoutandroid:id ="@ + id/frame1"android:layout_width ="match_parent"android:layout_height ="185dp"/>< FrameLayoutandroid:id ="@ + id/frame2"android:layout_width ="match_parent"android:layout_height ="wrap_content"/></LinearLayout></android.support.v4.widget.NestedScrollView></LinearLayout>
FragmentA:
@Override公共视图onCreateView(LayoutInflater充气器,ViewGroup容器,捆绑的saveInstanceState){查看v = inflater.inflate(R.layout.recycler_view,container,false);mDataListView =(RecyclerView)v.findViewById(R.id.data_list_view);mDataListView .setHasFixedSize(true);最后的GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),getActivity().getResources().getInteger(R.integer.playlist_categories_columns),GridLayoutManager.VERTICAL,false);mDataListView .setNestedScrollingEnabled(false);}}));返回v;}
FragmentB:
@Override公共视图onCreateView(LayoutInflater充气器,ViewGroup容器,捆绑的saveInstanceState){查看v = inflater.inflate(R.layout.recycler_view,container,false);mDataListView =(RecyclerView)v.findViewById(R.id.data_list_view);mDataListView .setHasFixedSize(true);最后的GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),1,GridLayoutManager.HORIZONTAL,false);mDataListView .setLayoutManager(gridLayoutManager);mDataListView .setNestedScrollingEnabled(false);}}));返回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"/>
我希望我对我的问题很清楚.
根据我的经验,我发现在 NestedScrollView
中使用多个 recyclerview's
不能进行视图回收.
但是我想出了解决方案,根据我的要求显示多个Recyclerview,我想在其顶部显示一个卧式Recyclerview,在其下方显示一个Vertical Recyclerview,所以我分享的内容可能对某人有帮助.
我删除了 NestedScrollView
作为 Recyclerview's
的父级,并将Horizontal Recyclerview
用作Vertical 的
. HeaderView
Recyclerview
代码:
公共类ListHeaderAdapter扩展了RecyclerView.Adapter< RecyclerView.ViewHolder>.{私有静态最终int TYPE_HEADER = 0;私有静态最终int TYPE_ITEM = 1;ArrayList< String>数据;public ListHeaderAdapter(ArrayList< String>数据){this.data =数据;}@Override公共RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){如果(viewType == TYPE_ITEM){//膨胀您的布局并将其传递给视图持有者返回新的VHItem(null);}否则,如果(viewType == TYPE_HEADER){//膨胀您的布局并将其传递给视图持有者返回新的VHHeader(null);}}@Overridepublic void onBindViewHolder(RecyclerView.ViewHolder持有人,int位置){int pos =位置-1;//用于处理Header视图(使用pos从列表中获取数据)if(VHItem的所有者实例){字符串dataItem = data.get(pos);//将持有人投射到VHItem并设置数据} else if(VHHeader的holder实例){//将持有者广播到VHHeader并设置标题的数据.}}@Overridepublic int getItemCount(){返回data.size()+ 1;//用于处理Header视图}@Overridepublic int getItemViewType(int position){如果(isHeader(position))返回TYPE_HEADER;返回TYPE_ITEM;}public boolean isHeader(int position){返回位置== 0;}VHItem类扩展了RecyclerView.ViewHolder {TextView标题;公共VHItem(查看itemView){超级(itemView);}}VHHeader类扩展了RecyclerView.ViewHolder {按钮按钮;公共VHHeader(查看itemView){超级(itemView);}}}
注意:可以实施相同的逻辑以在任何位置显示水平 RecyclerView的
I am stuck with a issue in managing Multiple Recyclerview
recycling inside a NestedScrollView
. Let me tell what I am trying to do -
- I have two frame layouts i.e. frame1 and frame2.
- I have two fragments containing recyclerview , First fragment's recyclerview showing items horizontally while second fragment's recyclerview showing list Vertically.
- Now I have put both
FrameLayout
inside aNestedScroolView
, 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.
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
这篇关于在NestedScrollView中使用Multiple Recyclerview不会发生视图回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!