如何在一个布局中实现多个recyclerviews? [英] How to implement multiple recyclerviews in one layout?

查看:102
本文介绍了如何在一个布局中实现多个recyclerviews?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个窗格,其中有两个 RecyclerView (让我们说"MyItems","AllItems").我创建了垂直的 LinearLayout ,其中有 TextView 作为标题和 RecyclerView .像这样:

I want to create a pane in which there are two RecyclerViews (let's say 'MyItems', 'AllItems'). I have created vertical LinearLayout, in which there are TextView as title and RecyclerView. Something like this:

 <LinearLayout ... >

    <TextView
        android:text="My Items"
        ... />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_items"
        ... />

    <TextView
        android:text="All Items"
        ... />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/all_items"
        ... />

</LinearLayout>

但是,使用这种方法,只有recyclerViews可以独立滚动,但是我需要整个布局只能滚动(因此,它首先滚动到第一部分,然后滚动到第二部分).我尝试将其包装在 ScrollView NestedScrollView 中,但最接近的是滚动没有流畅的动画.

However with this approach, only recyclerViews are scrollable independently, but I need the whole layout to be scrollable only (so firstly it scrolls through first section, then second). I tried to wrap it up in ScrollView and NestedScrollView, but the closest I got was scrolling without smooth animation.

我的问题是,这种方法有效吗?如果是,是否有办法在 NestedScrollView 中添加平滑滚动?或者我应该使用另一种方法来实现这一点,例如创建包含两个项目的 ListView ,其布局包含 TextView RecyclerView ?

My question is, is this approach valid, and if so, is there a way to add smooth scrolling in NestedScrollView? Or should I implement this using another approach, e.g. create ListView that contains two items with layout containing TextView and RecyclerView?

ListView

  • 列出项目1

  • List item 1

  • 标题1
  • RecyclerView 1

列出项目2

  • 标题2
  • RecyclerView2

从性能的角度来看,我认为这种方法不好.我对吗?我只需要为此找到最佳实践.谢谢.

I believe this approach is not good from performance side of view. Am I right? I just need to find the best practice for this. Thank you.

推荐答案

请不要使用嵌套滚动.由于将两个回收站的高度都设置为最大值,因此将无法达到回收站视图的目的,并将所有内容保留在内存中.而是继续进行以下两个选择:

Please don't use nested scrolling. It will defeat the purpose of recycler view and will keep everything inside the memory as the height will be set to maximum for both the recyclers. Instead proceed with the following two choices:

1.如果您没有特定的背景,请使用类似于以下内容的适配器创建一个RecyclerView:

1.If you don't have a specific background, create a single RecyclerView with adapter similar to the following:

public class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    ArrayList<Integer> data = new ArrayList<>();
    private final int VIEW_TYPE_TEXTVIEW = 0;
    private final int VIEW_TYPE_ITEM_1 = 1;
    private final int VIEW_TYPE_ITEM_2 = 2;
    private final LayoutInflater inflater;
    private final ArrayList<Integer> data;

    public MyRecyclerAdapter(Context ctx, ArrayList<Integer> data){
        this.context = ctx;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType == VIEW_TYPE_TEXTVIEW){
            View view = inflater.inflate(R.layout.simple_textview, parent, false);
            return new TextViewHolder(view);
        }else if(viewType == VIEW_TYPE_ITEM_1){
            View view = inflater.inflate(R.layout.item_top_recycler, parent, false);
            return new Item1Holder(view);
        }else{
            View view = inflater.inflate(R.layout.item_bottom_recycler, parent, false);
            return new Item2Holder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof TextViewHolder){
            ((TextViewHolder) holder).textView.setText(...);
        }else if(holder instanceof Item1Holder){
            ((Item1Holder) holder).itemTextView.setText(...);
        }else if(holder instanceof Item2Holder){
            ((Item2Holder) holder).itemTextView.setText(...);
        }
    }

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

    class TextViewHolder extends RecyclerView.ViewHolder {


        TextView textView;

        public HeaderHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv);
        }
    }
    class Item1Holder extends RecyclerView.ViewHolder {


        TextView itemTextView;

        public HeaderHolder(View itemView) {
            super(itemView);
            itemTextView = itemView.findViewById(R.id.tv);
        }
    }
    class Item2Holder extends RecyclerView.ViewHolder {


        TextView itemTextView;

        public HeaderHolder(View itemView) {
            super(itemView);
            itemTextView = itemView.findViewById(R.id.tv);
        }
    }
}

然后按如下所示设置适配器:

Then set your adapter like following:

ArrayList<Integer> data = new ArrayList<>();
//Adding first textview
data.add(0);
//Adding 10 elements of first RecyclerView
for(int i = 0; i<10; i++){
    data.add(1);
}
//Adding second textview
data.add(0);
//Adding 10 elements of second RecyclerView
for(int i = 0; i<10; i++){
    data.add(2);
}

adapter = new MyRecyclerAdapter(this, data);
navView.setAdapter(adapter);

这样,您也可以使用RecyclerView来包含您的textview.此方法将为您提供最佳的优化.确保在getItemViewType()中为上层recyclerView,下层RecyclerView和TextView返回适当的VIEW_TYPE.

This way, you can use the RecyclerView to contain your textview too. This method will give you the best optimization. Make sure that you return the appropriate VIEW_TYPE in getItemViewType() for your upper recyclerView, lower RecyclerView and the TextViews.

第二种方法是让一个RecyclerView包含4个项目:

The second method is to have one RecyclerView containing 4 items:

  • TextView
  • LinearLayout
  • TextView
  • LinearLayout

然后使用项目动态填充这些LinearLayouts.这样可以确保在视线范围之外,至少有一个Linearlayout可以回收.即使这样,第一种方法还是比这更好的方法.

Then populate these LinearLayouts with items dynamically. This will ensure that at least one of the Linearlayout is recycled when out of view. Even then, the first approach will be a far better approach than this.

这篇关于如何在一个布局中实现多个recyclerviews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆