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>