RecyclerView inside NestedScrollView onBindViewHolder calling for all getItemCount size

我的梦境 提交于 2019-12-04 03:03:58

I'm going to assume that since your are using appbar_scrolling_view_behavior you are trying to do something with AppBarLayout.

If so, you can use RecyclerView as a direct child of CoordinatorLayout and have support for AppBarLayout scrolling without nesting RecyclerView inside of NestedScrollView.

Try this: RecyclerView inside CoordinatorLayout (with AppBarLayout and CollapsingToolbarLayout):

<android.support.design.widget.CoordinatorLayout
    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="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:background="#55FF00FF"
                app:layout_collapseMode="none"/>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

And in your Activity or CustomView:

RecyclerView list;
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
list.setLayoutManager(layoutManager);
list.addItemDecoration(new VerticalSpaceItemDecoration(5));
list.setAdapter(adapter);

Problem caused for the height issue.

1) Edit the NestedScrollView & RecyclerView as following:

 <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">
        .......
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_views"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ..... 
            />

2) Make sure that you have compiled 'com.android.support:recyclerview-v7:23.2.1'

Alexander

But you set android:layout_height for NestedScrollView to wrap_content - here, it's zero by default (because there no content for him at the moment of the declaration). Next, for RecyclerView you set android:layout_height to match_parent - which is at the moment is 0. Thus, all your items have 0 height.

Thus, you have such situation. Solution: use solution above from @dkarmazi https://stackoverflow.com/a/37558761/3546306 or try to change parameter android:layout_height values.

It's right.Because you are using a ScrollView.ScrollView is not recyclable like RecyclerView or ListView.It will show all view contains these out of screen in one time.You should use a other layout instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!