Sha*_*got 5 android android-layout android-recyclerview
在 NestedScrollView 中添加多个 RecyclerView 时,我遇到了性能问题。我在顶部显示两个水平 RecyclerView,在底部显示垂直 RecyclerView。
我在下面添加了
recyclerView.setNestedScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)
这正确显示了所有列表,但真正的问题是我正面临性能问题。
所有三个 RecyclerView 在所有响应的约 5 秒后显示。这也会阻塞 UI,因为它忙于 RecyclerView 出现。
XML
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/llTabs"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.turacomobile.gb.utils.CustomSFUIMediumTextView
android:id="@+id/tvMyBrands"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="My Streamz (Brands)"
android:textColor="@color/black" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvMyBrands"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="@dimen/dimen_5dp"
android:paddingRight="@dimen/dimen_5dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.turacomobile.gb.utils.CustomSFUIMediumTextView
android:id="@+id/tvRecommendedBrands"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Recommended Streamz (Brands)"
android:textAppearance="@style/TabLayoutTextStyleSearch"
android:textColor="@color/black" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvRecommendedBrands"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="@dimen/dimen_5dp"
android:paddingRight="@dimen/dimen_5dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.turacomobile.gb.utils.CustomSFUIMediumTextView
android:id="@+id/tvSearchMyStreamz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Food from My Streamz Brands"
android:textColor="@color/black" />
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)
爪哇
rvSearchStreamz = findViewById(R.id.my_recycler_view);
rvMyBrands = findViewById(R.id.rvMyBrands);
rvRecommendedBrands = findViewById(R.id.rvRecommendedBrands);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
rvSearchStreamz.setLayoutManager(llm);
rvSearchStreamz.setNestedScrollingEnabled(false);
rvSearchStreamz.setHasFixedSize(false);
LinearLayoutManager llmMy = new LinearLayoutManager(this);
llmMy.setOrientation(LinearLayoutManager.HORIZONTAL);
rvMyBrands.setLayoutManager(llmMy);
rvMyBrands.setNestedScrollingEnabled(false);
rvMyBrands.setHasFixedSize(false);
LinearLayoutManager llmRecommended = new LinearLayoutManager(this);
llmRecommended.setOrientation(LinearLayoutManager.HORIZONTAL);
rvRecommendedBrands.setLayoutManager(llmRecommended);
rvRecommendedBrands.setNestedScrollingEnabled(false);
rvRecommendedBrands.setHasFixedSize(false);
Run Code Online (Sandbox Code Playgroud)
问题是 NestedScrollView。您不应该在 NestedScrollView 中使用 RecyclerView,因为 RecyclerView 不会回收 NestedScrollView 中的任何内容(您可以在 onCreateView 和 onBindView 中使用 write log 对其进行测试)。更改容器布局类型,如 LinearLayout、RelativeLayout 等。
小智 0
我建议您使用分页。就您关心的性能而言,这可以作为最好的催化剂。
在分页中,我们只下载特定数量的内容并将其设置到 recyclerview 中。
UI 线程的阻塞并大大减少,如果您有大量数据要渲染,也请使用异步任务,因为 UI 线程要保持尽可能轻
下面是您可以了解实施方案的链接。 https://medium.com/@etiennelawlor/pagination-with-recyclerview-1cb7e66a502b
| 归档时间: |
|
| 查看次数: |
5263 次 |
| 最近记录: |