android / views-widgets-samples

Multiple samples showing the best practices in views-widgets on Android.
Apache License 2.0
5.06k stars 3.01k forks source link

Motion layout(alpha4) issue with recyclerview onswipe item #44

Open pom1004 opened 5 years ago

pom1004 commented 5 years ago

I am using alpha4 implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha4' of constraint layout. First I created a single layout and do onswipe animation it is working perfectly with motion layout then I patched the layout with a recyclerview. Now problem comes into picture Issue:

  1. If I am doing onswipe in a particular item of rv then it's getting lagged.
  2. Suppose I am doing swipe on the 1st item of the rv now after full swipe you will find that same swiped has already happen with 13th no item, 25th no item and so on. So after every 12 item same swipe you can check while you are scrolling.

I have posted the whole project in Github and also created a sample video.

For quick gist of motion layout here is the link. And here is the layout desc link.

pom1004 commented 5 years ago

Finally the issue is fixed by one of colleague. @Srini Thanks for the solution.

 @Override
    public int getItemViewType(int position) {

        return position;
    }

Just need to override this method in adapter of recyclerview.

[Note:]Still the lag is there while swiping.

melihaksoy commented 5 years ago

This has nothing to do with MotionLayout. RecyclerView "recycles" layouts, re-uses them. Try to reset constraints when view is binded instead of providing viewtype per position ( which is inefficient - solves your problem because it prevents reusing views ), it should solve problem with continuous animation.

pom1004 commented 5 years ago

But if I reset constraints also is it solve my lagging issue as well? Have you checked the video? And how I will reset constraint can you give me a example please. Are you talking to reset by using constrainset @melihaksoy?

melihaksoy commented 5 years ago

About how to reset - yes, right on point, constraint set would solve it easy.

I don't know if it'd solve the frame drops, I didn't really experiment with MotionLayout in RecyclerView. What you can do is get a systrace to see what's causing frame drops. If it's MotionLayout related then you can create a new issue with the information you have 👍.

pom1004 commented 5 years ago
   @Override
    public void onBindViewHolder(CustomViewHolder holder, int position) {
    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.clone(holder.mainLayout);
    constraintSet.clear(holder.iv_recorded_program.getId());
    constraintSet.clear(holder.rl_recorded_program_info.getId());
    constraintSet.clear(holder.button.getId());

    constraintSet.connect(holder.iv_recorded_program.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
    constraintSet.connect(holder.iv_recorded_program.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
    constraintSet.connect(holder.iv_recorded_program.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
    constraintSet.connect(holder.iv_recorded_program.getId(), ConstraintSet.END, holder.rl_recorded_program_info.getId(), ConstraintSet.START);

    constraintSet.connect(holder.rl_recorded_program_info.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
    constraintSet.connect(holder.rl_recorded_program_info.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
    constraintSet.connect(holder.rl_recorded_program_info.getId(), ConstraintSet.END, holder.button.getId(), ConstraintSet.START);
    constraintSet.connect(holder.rl_recorded_program_info.getId(), ConstraintSet.START, holder.iv_recorded_program.getId(), ConstraintSet.END);

    constraintSet.connect(holder.button.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
    constraintSet.connect(holder.button.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);

    constraintSet.applyTo(holder.mainLayout);

}

In this way I tried to reset my layout also I removed the position but layout lag & repetation issue not solved.