嵌套滚动视图内的Recyclerview加载所有数据,而不是滚动时逐个调用图像

Luf*_*ffy 14 android android-recyclerview

在我的应用程序中,我正在使用tabHost Activiy.在tabhost第一个选项卡中,我正在使用TabActivity,我正在加载超过500个图像.

使用嵌套的SCroll视图:

我在recyclerview中使用了嵌套滚动视图.当加载主页时,它最初加载所有500个图像,然后显示主页.因此它会导致内存错误.

不使用嵌套的SCroll视图:

如果我删除嵌套滚动视图,一切都运行良好.滚动时逐个加载图像.它不会导致内存不足错误.

我的要求:

我需要滚动放置在recyclerview顶部的相对布局.所以我使用嵌套滚动视图.但它对我不起作用.

tab_home_layout:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.NestedScrollView
        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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
     >

<LinearLayout
    android:id="@+id/tab_home_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

        <RelativeLayout
            android:id="@+id/home_layout_top_2_recycler"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@drawable/layout_border"
            >

            <ImageView
                android:id="@+id/img_user_home_tab_recycler"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:layout_centerVertical="true"
                android:contentDescription="@string/cont_desc"
                android:src="@drawable/profile_pic_blue" />

            <TextView
                android:id="@+id/tv_user_mind_home_tab_recycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@+id/img_user_home_tab_recycler"
                android:hint="@string/whats_on"
                android:textColor="@color/Black"
                android:textSize="@dimen/txt_size" />
        </RelativeLayout>

    <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_list_tab_home_recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            android:visibility="visible" />

     </LinearLayout>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

下面我愿意分享我到目前为止所尝试的内容.

  • 我试过这个SO帖子.但是我在tabActivity里面使用主页.所以我不能使用工具栏+协调器布局.所以这个解决方案对我没用.

  • 然后我尝试使用多个布局 进行recyclerview.But这对我不起作用.因为relativelayout是静态的.如果我从webservice获得任何条件,我可以使用多个布局recyclerview.But我需要只是滚动视图.

  • 我计划设置adapter.But我的web服务图像被加载从第0 position.So我不能设置在零位置在适配器静态的RelativeLayout的第0位静态RelativeLayout的.

是否有任何替代解决方案来解决这个问题.谢谢.

raz*_*zle 13

您可以CoordinatorLayout在任何您喜欢的地方使用,类似于其他布局LinearLayout,甚至是RelativeLayout.如果你想要RelativeLayout滚动以响应你的RecyclerView,只需将它们放在一个CoordinatorLayout带有AppBarLayout.这是你的布局修改:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <RelativeLayout
            android:id="@+id/home_layout_top_2_recycler"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@drawable/layout_border"
            app:layout_scrollFlags="scroll">

            <ImageView
                android:id="@+id/img_user_home_tab_recycler"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:layout_centerVertical="true"
                android:contentDescription="@string/cont_desc"
                android:src="@drawable/profile_pic_blue" />

            <TextView
                android:id="@+id/tv_user_mind_home_tab_recycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@+id/img_user_home_tab_recycler"
                android:hint="@string/whats_on"
                android:textColor="@color/Black"
                android:textSize="@dimen/txt_size" />
            </RelativeLayout>

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_list_tab_home_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:visibility="visible"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

将其更改为自己的喜好,但一定要设置layout_heightRelativeLayout比其他的东西wrap_content.

如果这CoordinatorLayout是在另一个内部CoordinatorLayout,请使用NestedCoordinatorLayout答案作为您的内部CoordinatorLayout.

  • 你没有在你的 `RelativeLayout` 中包含 `app:layout_scrollFlags="scroll"`。这很重要。还要将 `app:layout_behavior="@string/appbar_scrolling_view_behavior"` 添加到你的 `RecyclerView`。 (2认同)

Pha*_*inh 5

重要的是onBindViewHolder你应该从位置1加载webservice中的图像.
这是一个例子,这种情况下,Header你的静态RelativeLayout

public class HeaderAdapter extends RecyclerView.Adapter < RecyclerView.ViewHolder > {
     private static final int TYPE_HEADER = 0;
     private static final int TYPE_ITEM = 1;
     String[] data;

     public HeaderAdapter(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) {
         if (holder instanceof VHItem) {
             String dataItem = getItem(position);
             //cast holder to VHItem and set data

             // ***********    
             // LOAD YOUR WEBSERVICE IMAGE FROM **position - 1**
             // => for row 1 you will get the image 0 ...
             // ***********


         } else if (holder instanceof VHHeader) {
             //cast holder to VHHeader and set data for header.
         }
     }

     //increasing getItemcount to 1. This will be the row of header.
     @Override
     public int getItemCount() {
         return data.length + 1;
     }

     @Override
     public int getItemViewType(int position) {
         if (isPositionHeader(position))
             return TYPE_HEADER;

         return TYPE_ITEM;
     }

     // condition for header
     private boolean isPositionHeader(int position) {
         return position == 0;
     }

     // getItem -1 because we have add 1 header
     private String getItem(int position) {
         return data[position - 1];
     }

     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);
         }
     }
 }
Run Code Online (Sandbox Code Playgroud)