3

My question is quite simple, why does it lag?

This layout:

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

recyclerview(1,2) are horizontal and (3) is vertical with height as wrap_content. When I get down to rv(3) it doesn't lag, only when showing rv(1,2)

Enough with the basics, why does this lag when they are filled with children and not when empty? It doesnt make any sence to me.

I believe there's alot going on in the background that I'm not aware about. Measures or something? Anyone know what's going on? Or if someone could just point me in the right direction to conquer this problem. Something I do that is discouraged?

To clearify things I include my ViewHolder and Adapter for the RV's:

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

Adapter

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);
 }
}
6
  • 1.What do you mean by "When I get down to rv(3) it doesn't lag, only when showing rv(1,2)"? >> on which scroller you get lag 2.What do you fill in your recyclerviews? Commented Aug 16, 2015 at 22:49
  • When I scroll verticaly in the ScrollView (not in the RecyclerView(1/2/3)) the ui lagg horribly much. When I scroll horizontally in the RecyclerView 1/2 the view does not lag. When I only see RecyclerView 3(when I have scrolled passed the RecyclerView 1&2) the ScrollView does not lag. I fill those with a couple of CardViews, maybe 8-15 each. Rv1&2 is horizontal and the 3rd is vertical. Hope it got clearer now =) Commented Aug 18, 2015 at 8:39
  • Try replace your ScrollView with a Recyclerview. Maybe some garbage collection on your RVs is the reason of lag. It's interesting to know the result. It would be nice if you could provide a clip of the scrollview's behaviour while you are try to scroll. Commented Aug 19, 2015 at 18:21
  • I think I have found out what was causing the lag. The childviews of both Rv1/2 was kind of complex, which in turn made it take a long time to scroll. Even when I only tried using one RV it was laggy so I figured simplifying the childview would make a difference -> it started to lag less! :D So now I'll try to change every RV's childviews to more simple and less information in it. Hope it will help in the long run =) Commented Aug 21, 2015 at 7:18
  • Once I faced your problem and solved by using xml layout instead of programmatically creating them. Just call them onCreatViewHolder with inflater and see the difference.>>>>> Final View view = layoutInflater.inflate(R.layout.custom_rv_item, parent, false); final RVViewHolder rVViewHolder = new RVViewHolder(view);>>>>and then >>> return rVViewHolder---- Let me know if you get what I mean and the result :) Commented Aug 21, 2015 at 20:42

2 Answers 2

1

Did you added this in AndroidManifest.xml. if not you can try this. it solves my problem.

android:hardwareAccelerated="true" 
Sign up to request clarification or add additional context in comments.

Comments

0

Don't use recycler view inside other recycler view as parent view tries to draw whole child view which leads to lags. Try to make one recycler view with different view types.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.