【问题标题】:RecyclerView inside NestedScrollView causes RecyclerView to inflate all elementsNestedScrollView 内的 RecyclerView 导致 RecyclerView 膨胀所有元素
【发布时间】:2017-11-11 05:43:42
【问题描述】:

我在将 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;
}

我看过这篇关于这个问题的帖子:
How to put RecyclerView inside NestedScrollView?

但遗憾的是,它并没有提到问题的最终解决方案。

澄清: RecyclerView 可以完美滚动,它会在正确的时间显示,但问题是它会立即渲染其所有子元素,这意味着可能有数百个元素,即使屏幕一次最多只显示 5-6 个。

如果需要更多信息,请随时提出问题。

------- 编辑 -------
在多次尝试其他解决方案失败后,我最终使用了 Jeeva Nandhan 的解决方案。
在问这个问题之前,我知道这是一个可能的解决方案,但我有 11 种不同的可能视图需要适合 RecyclerView,所以我希望避免它。
使用不同的 ViewTypes 后,它工作得很好。由于 ViewType 数量众多,我担心它会非常低效,但它非常光滑。

【问题讨论】:

标签: android xml android-recyclerview scrollview nestedscrollview


【解决方案1】:

如果你有大量数据要显示,第一次只显示一些数据而不是滚动使用 loadMoreListener 来获取下一个数据。

【讨论】:

    【解决方案2】:

    我也遇到过这个问题...这是因为 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 行为中工作...

    希望这有帮助:)

    【讨论】:

    • 对不起,我找不到你
    • 解释添加标题将如何改善任何事情。
    猜你喜欢
    • 1970-01-01
    • 2022-09-30
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 2019-04-26
    • 2017-02-15
    • 2017-05-28
    相关资源
    最近更新 更多