本文介绍了嵌套滚动视图中的Recyclerview加载所有数据,而不是在滚动时一一调用图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application, I'm using tabHost Activiy.In the tabhost first tab,I'm using TabActivity,I'm loading more than 500 images.

使用嵌套SCroll视图:

我在recyclerview中使用了嵌套的滚动视图.加载主页时,它将首先加载所有500张图像,然后显示主页,从而导致内存不足而出错.

I have used nested scroll view inside recyclerview.When loading the home page, it is loading all 500 images initially, then showing the home page.So that it cause memory out of error.

不使用嵌套的SCroll视图:

如果我删除嵌套的滚动视图,则一切正常.滚动时会逐张加载图像.不会造成内存不足错误.

If I remove nested scroll view, everything is working good.it loads image one by one when scrolling.it doesn't cause out of memory error.

我的要求:

我需要滚动放置在recyclerview顶部的相对布局.因此我使用了嵌套滚动视图.但这对我不起作用.

I need to scroll the relative layout placed top of the recyclerview.So that I used nested scroll view.But it doesn't worked for me.

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>

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

Below I'm willing to share what I have tried so far.

  • 我尝试了 SO发布.但是我在tabActivity内使用首页,因此无法使用工具栏+协调器布局.因此此解决方案无效对我来说.

  • I tried this SOPost.ButI'm using home page inside tabActivity.So I can't use toolbar +coordinator layout.So thissolution wasn't workedfor me.

然后我尝试使用多个布局for recyclerview.但这对我不起作用.因为relativelayout是静态的.如果我从Web服务的意思是,我可以使用多个布局的recyclerview.但是我需要只是滚动视图.

Then I tried to use multiplelayoutfor recyclerview.But that doesn't worked for me.Because thatrelativelayout is a static one.If I'm getting any condition fromwebservice means, I can use multiple layout recyclerview.But I needto just scroll the views.

我计划将静态相对布局设置为的第0个位置适配器.但是我的网络服务图像从第0个位置加载.所以我无法在适配器的第0个位置设置静态相对布局.

I planned to set the static relativelayout in 0th position ofadapter.But my webservices images were loading from 0th position.So Ican't set the static relativelayout in adapter at 0th position.

有没有其他解决方案可以解决此问题.谢谢.

is there any alternate solution to solve this issue.Thank You.

推荐答案

您可以在与任何其他布局(例如LinearLayout甚至RelativeLayout)相似的任意位置使用CoordinatorLayout.如果希望RelativeLayout滚动以响应RecyclerView,只需将它们放在带有AppBarLayoutCoordinatorLayout中.这是修改后的布局:

You can use CoordinatorLayout just about anywhere you like similar to other layouts like LinearLayout or even RelativeLayout. If you want your RelativeLayout to scroll in response to your RecyclerView, simply place them inside a CoordinatorLayout with an AppBarLayout. Here's your layout modified:

<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>

将其更改为您的首选项,但请确保将RelativeLayoutlayout_height设置为除wrap_content之外的其他内容.

Change it to your preference but be sure to set the layout_height of the RelativeLayout to something other than wrap_content.

如果此CoordinatorLayout在另一个CoordinatorLayout内部,请使用此 answer 中的NestedCoordinatorLayout CoordinatorLayout.

If this CoordinatorLayout is inside another CoordinatorLayout, use the NestedCoordinatorLayout from this answer as your inside CoordinatorLayout.

这篇关于嵌套滚动视图中的Recyclerview加载所有数据,而不是在滚动时一一调用图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:52