问题描述
在我的应用程序中,我使用的是 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.
使用嵌套滚动视图:
我在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.
不使用嵌套滚动视图:
如果我删除嵌套滚动视图,一切正常.滚动时它会一张一张地加载图像.它不会导致内存不足错误.
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.
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.
然后我尝试使用 multiple布局为 recyclerview. 但这对我不起作用.因为相对布局是一个静态的.如果我得到任何条件webservice 意味着,我可以使用多个布局 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.
推荐答案
你可以在你喜欢的任何地方使用 CoordinatorLayout
类似于 LinearLayout
甚至 等其他布局>相对布局
.如果您希望您的 RelativeLayout
滚动以响应您的 RecyclerView
,只需将它们放在带有 AppBarLayout
的 CoordinatorLayout
中.这是您修改后的布局:
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>
根据您的喜好更改它,但一定要将 RelativeLayout
的 layout_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
内,请使用此 作为您的内部 CoordinatorLayout
.
If this CoordinatorLayout
is inside another CoordinatorLayout
, use the NestedCoordinatorLayout
from this answer as your inside CoordinatorLayout
.
这篇关于嵌套滚动视图中的 Recyclerview 加载所有数据,而不是滚动时调用一张一张的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!