Reputation: 73
I tried to use startSwipe within adapter, it seems not working. Strangely, when I tried swipe it with finger manually, it worked. Just that startSwipe does not work when i clicked on of the button inside holder. Need help on this~
This is where i called the startSwipe (in onCreateViewHolder method).
final ViewHolder holder = new ViewHolder(rootView);
holder.rightItemLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mItemTouchHelper.startSwipe(holder);
}
});
This is how I setup the ItemTouchHelper:
mItemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
});
How ItemTouchHelper attached to recyclerView:
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
CategoryAdapter adapter = new CategoryAdapter(this, items);
recyclerView.setAdapter(adapter);
adapter.getItemTouchHelper().attachToRecyclerView(recyclerView);
Upvotes: 5
Views: 1470
Reputation: 377
Looks like you are calling mItemTouchHelper.startSwipe(holder) within onCreateViewHolder, but the item might not be fully attached to the RecyclerView at that point. You should call it in onBindViewHolder, where the holder is fully prepared and attached to the RecyclerView.
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.rightItemLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mItemTouchHelper.startSwipe(holder);
}
});
}
The SimpleCallback you provided in the ItemTouchHelper only supports swiping left (ItemTouchHelper.LEFT). Make sure that the item you're trying to swipe is swipable in that direction. You can test swiping in multiple directions by adding both left and right swipe flags:
new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// Handle swipe action
}
}
Further, ensure that the ItemTouchHelper is correctly attached to the RecyclerView. You mentioned using adapter.getItemTouchHelper().attachToRecyclerView(recyclerView);, but double-check the adapter's method where you initialize and attach the ItemTouchHelper. Ensure it is being called correctly in the activity/fragment.
The rightItemLayout click listener inside the view holder might be consuming touch events, which could prevent the startSwipe from working. You can try disabling click intercept for the parent views or the button to see if it helps.
holder.rightItemLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false; // Return false to allow touch event to propagate to ItemTouchHelper
}
});
Hope this solution works.
Upvotes: 1