NestedScrollView性能问题中的多个RecyclerView

时间:2018-01-03 07:10:27

标签: android android-layout android-recyclerview

我在NestedScrollView中添加多个RecyclerView时遇到性能问题。我在底部显示两个水平,在底部显示垂直的RecyclerView。

我已添加以下行

recyclerView.setNestedScrollingEnabled(false);

这显示了所有列表,但真正的问题是我面临性能问题。

所有三个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>

JAVA

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

3 个答案:

答案 0 :(得分:2)

问题是NestedScrollView。您不应该在NestedScrollView中使用RecyclerView,因为RecyclerView不会在NestedScrollView中回收任何东西(您可以使用onCreateView和onBindView中的写入日志来测试它)。更改容器布局类型,如LinearLayout,RelativeLayout等。

答案 1 :(得分:0)

我建议你去分页。 就性能而言,这可以作为最好的催化剂。

在分页中,我们只下载并将特定数量的内容设置到recyclerview中。

阻止UI线程并大幅减少也请使用异步任务,如果要渲染大量数据,则要尽可能保持光线的维护

下面是您可以了解实施的链接。 https://medium.com/@etiennelawlor/pagination-with-recyclerview-1cb7e66a502b

答案 2 :(得分:0)

首先更改适配器初始化

HorizontalAdapterOne hAdapterOne;
HorizontalAdapterTwo hAdapterTwo;

将此内容写入Activity

中的onCreate方法
hAdapterOne=new HorizontalAdapterOne(context,new ArrayList<DemoItemOne>);
hAdaptertwo=new HorizontalAdapterTwo(context,new ArrayList<DemoItemTwo>);

在适配器中添加方法

public void refreshList(ArrayList<DemoModel> demoList){
    if(demoList!=null  && demoList.size()>0){
        this.demoActualList.addAll(demoList);
        notifyDataSetChanged();
    }
}

从API获取数据后,从您的活动中调用此方法

hAdapterOne.refreshList(demoListFromAPI)

或者您可以使用自定义列表视图而不是使用Recycler View进行垂直列表。检查以下列表查看类

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
public class ExpandableHeightListView extends ListView {
boolean expanded = true;
public ExpandableHeightListView(Context context) {
    super(context);
}
public ExpandableHeightListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public ExpandableHeightListView(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);
}
public boolean isExpanded() {
    return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // HACK! TAKE THAT ANDROID!
    if (isExpanded()) {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    } else {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public void setExpanded(boolean expanded) {
    this.expanded = expanded;
}
}