RecyclerView嵌套在NestedScrollView中会导致所有元素都被加载。

6

我在将RecyclerView放在NestedScrollView中时遇到了问题,这会导致RecyclerView适配器的所有元素都被渲染出来。

这是一个相当大的问题,因为RecyclerView显示的列表可能包含数百个元素。

目前,这导致了相当多的延迟(显然),因为它必须一次性渲染所有视图,并且不能像RecyclerView通常那样重用任何已充气的视图。

这是我当前的XML(删除了一些冗余以将其最小化):

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:overScrollMode="never">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="90dp">

            <!-- Some content -->

        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!-- Some more content -->

        </LinearLayout>

        <!-- Product list -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="12dp"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:overScrollMode="never"/>

        </LinearLayout>

    </LinearLayout>

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

这是我创建的Fragment中的onCreateView()方法,它会填充包含NestedScrollView和RecyclerView的视图:

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.category_content_fragment, container, false);
    ButterKnife.bind(this, root);

    List<Product> products = new ArrayList<>(); //This is populated by other means, not relevant to the issue

    productsRecyclerView.setNestedScrollingEnabled(false);
    productsRecyclerView.setHasFixedSize(true);
    productsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    ProductsContentAdapter productsContentAdapter = new ProductsContentAdapter(products);
    productsRecyclerView.setAdapter(productsContentAdapter);

    return root;
}

我看到了这篇文章,与问题相关:
如何将RecyclerView放在NestedScrollView中?

但不幸的是,它没有提到最终解决方案。
澄清一下: RecyclerView可以完美滚动,也能在正确的时间显示,但问题是它立即渲染所有子项,可能包含多个元素,即使屏幕最多只显示5-6个。
如果需要更多信息,请随时提问。
------- 编辑 ------- 尝试了许多其他解决方案后,我最终使用了Jeeva Nandhan的解决方案。 在提问之前,我知道这是一个可能的解决方案,但我有11个不同的可能需要放入RecyclerView的视图,所以我想避免使用它。 使用不同的ViewTypes之后,它运行得非常完美。我担心由于高数量的ViewTypes会效率低下,但它非常流畅。

https://dev59.com/m6Lia4cB1Zd3GeqPjXIX - Ashutosh Tripathi
尝试使用分页而不是一次性加载所有视图。 - Hemil Kumbhani
1
嗯...为什么它可以使用不同的视图类型工作? - l33t
2个回答

1
我也遇到了这个问题...这是因为ScrollViewRecyclerView在加载数据时有所不同,因为在这种情况下ScrollView充当父级,并且我们在代码中使用了以下行。
setNestedScrollingEnabled(false);

这将使得基于Recyclerview数据的滚动速度变慢并出现卡顿问题。
解决此问题的一种方法是向Recyclerview添加标题。
下面我会清晰地解释它。
假设这个recyclerview在我们的活动中。
<android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />

适配器类将会是这样的,我们会在其中添加头部。
public class SampleAdapter extends RecyclerView.Adapter {


    private final int BODY = 1;
    private final int HEADER = 2;

    private List<String> data = null;

    SampleAdapter(List<String> data) {
        this.data = data;
    }


    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return HEADER;
        }

        return BODY;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {

            case HEADER:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.inflate_header_layout, parent, false);
                return new HeaderViewHolder(view);

            default:
                //Setting the Body view...
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.inflate_details, parent, false);
                return new BodyViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof BodyViewHolder) {
            //Set the body content....
            if (data != null && data.size() > 0) {
                /** Since we have added one cell for header,
                 * we need to decrement the position and render the body view.
                 *
                 */
                int bodyPosition = position - 1;

            }
        } else if (holder instanceof HeaderViewHolder) {
            //Set the header content...
        }
    }

    @Override
    public int getItemCount() {
        //Sice we are going to add header, we are supposed increase the count by one...
        return data.size() + 1;
    }
}

通过这种方式,就不需要使用NestedScrollView,所有视图都将在RecyclerView的行为中工作... 希望这对你有帮助 :)

抱歉,我无法理解您的意思。 - Jeevanandhan
解释一下如何通过添加头部来改善任何事情。 - Jonas Borggren

-1
如果你有大量的数据需要显示,首次只显示一部分数据,然后在滚动时使用LoadMoreListener来获取下一批数据。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接