简体   繁体   English

使用NestedScrollView中的Multiple Recyclerview不会发生回收

[英]View Recycling not happens with Multiple Recyclerview inside NestedScrollView

I am stuck with a issue in managing Multiple Recyclerview recycling inside a NestedScrollView . 我遇到了在NestedScrollView中管理Multiple Recyclerview回收的NestedScrollView Let me tell what I am trying to do - 让我告诉我想做什么 -

  1. I have two frame layouts ie frame1 and frame2. 我有两个框架布局,即frame1和frame2。
  2. I have two fragments containing recyclerview , First fragment's recyclerview showing items horizontally while second fragment's recyclerview showing list Vertically. 我有两个包含recyclerview的片段,第一个片段的recyclelerview水平显示项目,而第二个片段的recyclelerview显示垂直列表。
  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 ? 现在我已将两个FrameLayout放在NestedScroolView ,frame1 recyclerview正在回收所有视图,但是frame2 recylerview不回收视图,不知道为什么? it first load all items then shows on screen. 它首先加载所有项目,然后在屏幕上显示。

Some Code : 一些代码:

MainActivity.java 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 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 : 片段A:

@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 : 片段B:

@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 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 . 根据我的经验,我发现在NestedScrollView使用多个recyclerview's无法进行视图回收。

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. 但是我想出了根据我的要求显示多个Recyclerview的解决方案我想在顶部显示一个Horizo​​ntal Recyclerview,在它下面显示一个Vertical Recyclerview,所以我所做的我可以帮助某人。

I removed the NestedScrollView as parent of Recyclerview's and made Horizontal Recyclerview as HeaderView of Vertical Recyclerview . 我删除了NestedScrollView作为Recyclerview's父级,并将Horizo​​ntal Recyclerview作为Vertical Recyclerview HeaderView

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 注意:可以实现相同的逻辑以在任何位置显示Horizo​​ntal RecyclerView's

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NestedScrollView里面没有使用Recyclerview - NestedScrollView not fling with Recyclerview inside RecyclerView 不在 NestedScrollView 内滚动 - RecyclerView not scrolling inside NestedScrollView NestedScrollView 内的 RecyclerView 问题 - RecyclerView inside NestedScrollView issue NestedScrollView内的RecyclerView比RecyclerView滚动得更快 - RecyclerView inside NestedScrollView scroll faster than RecyclerView Android RecyclerView 高度问题内视图 pager2 内 NestedScrollView 与 TabLayout - Android RecyclerView height problem inside view pager2 inside NestedScrollView with TabLayout 在nestedscrollview的recyclerview内部的recyclerview的notifyDataChanged上,外部recyclerview滚动到顶部 - On notifyDataChanged of recyclerview inside a recyclerview in a nestedscrollview the outer recyclerview scrolls to top 检查nestedscrollview中的recyclelerview滚动状态 - Check recyclerview scroll state inside nestedscrollview 嵌套滚动视图中的 recyclerview addOnScrollListener (endless scrolllistener) - recyclerview inside nestedscrollview addOnScrollListener (endless scrolllistener) 如何在 NestedScrollView 中限制 RecyclerView 的高度 - How to limit the height of a RecyclerView inside a NestedScrollView NestedScrollView中的RecyclerView - 不需要的滚动开始 - RecyclerView inside NestedScrollView - unwanted scroll to begin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM