android how to use ViewBinding with ItemTouchHelper swipe
Asked Answered
S

1

1

I have a working project with a RecyclerView list of CardView items. It uses an ItemTouchHelper to update the UI upon left-swipe, for deleting a CardView.

How do I convert the ItemTouchHelper code to handle the ViewBinding? What am I missing, as no swipe is occurring after converting to ViewBinding.

Here is the example of working code for the ItemTouchHelper, before converting the project to use ViewBinding:

public class CDAItemTouchHelper extends ItemTouchHelper.SimpleCallback {

    private final RecyclerItemTouchHelperListener listener;

    public CDAItemTouchHelper(int dragDirs, int swipeDirs, RecyclerItemTouchHelperListener listener) {

        super(dragDirs, swipeDirs);
        this.listener = listener;
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {

        if (viewHolder != null) {
            **final View foregroundView = ((CardDetailsAdapter.ItemHolder) viewHolder).viewForeground2;**
            // Detects when there is a UI change for the View.  It then moves the foreground View
            // to show the static background View.
            getDefaultUIUtil().onSelected(foregroundView);
        }
    }
}

Here is the attempt to convert the code for the ItemTouchHelper using ViewBinding:

CdaSimpleListItemBinding cdaSimpleListItemBinding; 

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {

    if (viewHolder != null) {

        if (viewHolder instanceof CardDetailsAdapter.SimpleViewHolder) {

            layoutInflater = LayoutInflater.from(viewHolder.itemView.getContext());
            cdaSimpleListItemBinding = CdaSimpleListItemBinding.inflate(layoutInflater);
            **final View foregroundView = cdaSimpleListItemBinding.viewForeground2;**
            // Detects when there is a UI change for the View.  It then moves the foreground View
            // to show the static background View.
            getDefaultUIUtil().onSelected(foregroundView);
        }
    }
}

CardDetailsActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding5 = ActivityDetailsBinding.inflate(getLayoutInflater());
    View view5 = binding5.getRoot();
    setContentView(view5);
 
    ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new CDAItemTouchHelper(
        0, ItemTouchHelper.LEFT,this);
    new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(mRecyclerView);

CardDetailsAdapter

...

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    binding6 = CdaSimpleListItemBinding.inflate(layoutInflater,parent,false);
    SimpleViewHolder simpleViewHolder;
    simpleViewHolder = new SimpleViewHolder(binding6); 
}

public class SimpleViewHolder extends RecyclerView.ViewHolder {

    final CdaSimpleListItemBinding binding6;

    public SimpleViewHolder(@NonNull CdaSimpleListItemBinding binding) {
        super(binding.getRoot());
        this.binding6 = binding;
    }

cda_simple_list_item.xml

<FrameLayout

    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    card_view:cardBackgroundColor="@android:color/white"    
    android:foreground="?android:attr/selectableItemBackground"
    android:background="#FFFFFF"
    tools:context=".CardDetailsActivity">

    <RelativeLayout
        android:id="@+id/view_background2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/leave_behind_background"  >        
        ...
    </RelativeLayout>

    <RelativeLayout
        **android:id="@+id/view_foreground2"**
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="@color/colorFlLabelFinal"  >    
        ...
    </RelativeLayout>
Sinaloa answered 30/1 at 3:5 Comment(0)
V
2

When you call

cdaSimpleListItemBinding = CdaSimpleListItemBinding.inflate(layoutInflater);

in your ItemTouchHelper you are creating an entirely new view hierarchy, not getting a view from the one you already created.

To get the existing view you should use binding you saved on the ViewHolder already

final View foregroundView = ((CardDetailsAdapter.SimpleViewHolder)viewholder).binding6.viewForeground2;
Vanguard answered 30/1 at 21:18 Comment(1)
Understood, I appreciate the feedback and answer! Also, I needed to make the "binding6" in the ViewHolder "public" so that the ItemTouchHelper methods have access to the binding. Answer accepted and upvoted, cheers.Sinaloa

© 2022 - 2024 — McMap. All rights reserved.