滚动多个recyclerviews

时间:2015-07-07 08:46:07

标签: android android-recyclerview lag

我的问题很简单,为什么会滞后?

这种布局:

    <ScrollView>
         <RelativeLayout>
             <RecyclerView1/>
             <RecyclerView2/>
             <RecyclerView3/>
         </RelativeLayout>
    </ScrollView>

recyclerview(1,2)是水平的,(3)是垂直的,高度是wrap_content。当我下到rv(3)时,它不会滞后,只有在显示rv(1,2)

基本知识足够了,为什么在孩子满身而不是空的时候这会滞后?它对我没有任何意义。

我相信在我不了解的背景中会有很多事情发生。措施还是什么?有谁知道发生了什么?或者,如果有人能够指出我正确的方向来克服这个问题。我这样做的事情是不鼓励的?

要清除我包含RV的ViewHolder和适配器的内容:

ViewHolder

public class ObjHolder extends RecyclerView.ViewHolder {

 private static String              TAG         = "[ObjHolder]";

 public Obj                         mObj;
 public CardView                    mContainer;
 public TextView                    mAttr1;
 public TextView                    mAttr2;
 public TextView                    mAttr3;
 public TextView                    mAttr4;
 public TextView                    mAttr5;

 public OnObjClickListener  mCallback   = null;

 public ObjHolder(View v) {
    super(v);

    mContainer = (CardView) v;

    mAttr1 = (TextView) mContainer.findViewById(R.id.obj_details_attr1);
    mAttr2 = (TextView) mContainer.findViewById(R.id.obj_details_attr2);
    mAttr3 = (TextView) mContainer.findViewById(R.id.obj_details_attr3);
    mAttr4 = (TextView) mContainer.findViewById(R.id.obj_details_attr4);
    mAttr5 = (TextView) mContainer.findViewById(R.id.obj_details_attr5);

    mContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DEBUG) Log.d(TAG, "onClick()");
            if (mCallback != null) mCallback.onObjClick(mObj);
        }
    });
 }

 public void setOnObjClickListener(OnObjClickListener onObjClickListener) {
    mCallback = onObjClickListener;
 }
}

适配器

public class ObjAdapter extends AbstractCursorRecyclerViewAdapter<RecyclerView.ViewHolder> implements OnObjClickListener {

 private static final String    TAG     = "[ObjAdapter]";

 private OnObjClickListener mCallback   = null;

 public ObjAdapter( Context context, Cursor cursor) {
    super(context, cursor);
 }

 @Override
 public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, Cursor cursor) {

    if (viewHolder instanceof ObjHolder) {

        ObjHolder objHolder = (ObjHolder) viewHolder;

        final Obj obj = new Obj(cursor);

        objHolder.mAttr1.setText(obj.attr1);
        objHolder.mAttr2.setText(obj.attr2);
        objHolder.mAttr3.setText(obj.attr3);
        objHolder.mAttr4.setText(obj.attr4);
        objHolder.mAttr5.setText(obj.attr5);
        objHolder.mObj = obj;

        objHolder.setOnObjClickListener(this);

        MyDatabase db = MyDatabase.getInstance(getContext());
        Cursor childCursor = db.getChildren(obj);

        // Count children who matters to present in the card
        int attribMatch = 0;
        try {
            while (childCursor.moveToNext()) {
                if (childCursor.getString(1) != null) {
                    int newStatusValue = Integer.parseInt(childCursor.getString(1));

                    if (newStatusValue >= 5) attribMatch++;
                }
            }
        }
        finally {
            childCursor.close();
        }

        if (Integer.parseInt(obj.statusValue) >= 10) objHolder.mContainer.setForeground(ContextCompat.getDrawable(getContext(), R.drawable.bg_warning_border));

        objHolder.mAttrib1.setText(""
                                    + attribMatch);

        if (attribMatch > 0) objHolder.mAttrib2.setTextColor(getContext().getResources().getColor(R.color.red));

    }
 }

 @Override
 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
                                        int viewType) {
    return new ObjHolder(
                LayoutInflater.from(parent.getContext()).inflate(R.layout.obj_details, parent, false));
 }

 public void setOnObjClickListener(OnObjClickListener onObjClickListener) {
    mCallback = onObjClickListener;
 }

 @Override
 public void onObjClick(Obj obj) {
    if (mCallback != null) mCallback.onObjClick(obj);
 }
}

2 个答案:

答案 0 :(得分:0)

您是否在AndroidManifest.xml中添加了此内容?如果没有,你可以试试这个。它解决了我的问题。

android:hardwareAccelerated="true" 

答案 1 :(得分:0)

不要在其他回收站视图中使用回收站视图,因为父视图会尝试绘制导致滞后的整个子视图。尝试使用不同的视图类型创建一个回收站视图。