Android 嵌套滚动视图内的多个RecyclerView性能问题

Android 嵌套滚动视图内的多个RecyclerView性能问题,android,android-layout,android-recyclerview,Android,Android Layout,Android Recyclerview,在NestedScrollView中添加多个RecyclerView时,我面临性能问题。我在顶部显示了两个水平循环视图,在底部显示了两个垂直循环视图 我在下面加了一行 recyclerView.setNestedScrollingEnabled(false); 这是正确显示所有列表,但真正的问题是我面临性能问题 所有三个RecyclerView在所有响应发出约5秒后显示。这也会阻塞UI,因为它正忙于显示RecyclerView XML 我建议您使用分页。 就您关心的性能而言,这可以作为最佳催化

在NestedScrollView中添加多个RecyclerView时,我面临性能问题。我在顶部显示了两个水平循环视图,在底部显示了两个垂直循环视图

我在下面加了一行

recyclerView.setNestedScrollingEnabled(false);
这是正确显示所有列表,但真正的问题是我面临性能问题

所有三个RecyclerView在所有响应发出约5秒后显示。这也会阻塞UI,因为它正忙于显示RecyclerView

XML


我建议您使用分页。 就您关心的性能而言,这可以作为最佳催化剂

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

如果你有大量的数据要呈现,那么请使用异步任务,因为UI线程的阻塞会被大大减少,因为UI线程会被尽可能地维护得很轻

下面是一个链接,您可以从中获得实施的想法。

首先更改适配器初始化

HorizontalAdapterOne hAdapterOne;
HorizontalAdapterTwo hAdapterTwo;
并将其写入活动中的onCreate方法中

hAdapterOne=new HorizontalAdapterOne(context,new ArrayList<DemoItemOne>);
hAdaptertwo=new HorizontalAdapterTwo(context,new ArrayList<DemoItemTwo>);
或者,您可以使用自定义列表视图代替回收器视图进行垂直列表。检查下面的列表视图类

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

问题是NestedScrollView。您不应该在NestedScrollView中使用RecyclerView,因为RecyclerView不会回收NestedScrollView中的任何内容(您可以在onCreateView和onBindView中使用write log进行测试)。更改容器布局类型,如LinearLayout、RelativeLayout等。

你能分享你的
recyclerView
@Nilu添加的xml和java代码吗。那个么我如何在一个视图中使用三个recyclerview呢?也许你们可以把你们的水平滚动视图放在底部滚动视图的前两项中。
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();
    }
}
hAdapterOne.refreshList(demoListFromAPI)
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;
}
}