ItemTouchHelper not being applied to every View in Adapter - java

In implementing swipe-to-delete functionality I accidentally made my forground view of the viewholder transparent. In doing so I've uncovered a bug with my ItemTouchHandler not changing the background or hiding the text when it is supposed to.
It appears to me that my ItemTouchHelper is not being applied to every view in my ReycleViewAdapter and/or the views are not being updated or they all share a single parent of some sort, after many hours of attempted debugging I'm at my wit's end, I've included a handy gif of the issue.
The ItemTouchHelper's onDrawChild() and onDrawChildOver() methods are passed to my fragment in order to handle the actions
Code:
AllLists_fragment.java:
public class showAllLists_fragment extends android.support.v4.app.Fragment implements RecycleItemTouchHelper.RecyclerItemTouchHelperListener {
private static final String TAG = "showAllLists";
private RecyclerView mAllItemsView = null;
private DataCommunication mData = null;
private ItemTouchHelper.SimpleCallback itemTouchHelper = null;;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.show_all_lists_frag, container, false);
this.mAllItemsView = (RecyclerView) view.findViewById(R.id.allItems);
this.itemTouchHelper = new RecycleItemTouchHelper(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT, this);
new ItemTouchHelper(this.itemTouchHelper).attachToRecyclerView(this.mAllItemsView);
this.mAllItemsView.setAdapter(this.mData.getAdapter());
this.mAllItemsView.setLayoutManager(new LinearLayoutManager(getActivity()){
//
#Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
super.onLayoutChildren(recycler, state);
} catch (IndexOutOfBoundsException e) {
Log.d(TAG, "Inconsistent Exception Caught -See stack trace");
e.printStackTrace();
}
}
});
viewShadow();
return view;
}
#TargetApi(21)
public void viewShadow(){
this.mAllItemsView.setTranslationZ(100);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
this.mData = (DataCommunication) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement DataCommunication");
}
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
this.mData.getAdapter().notifyDataSetChanged();
this.mData.refresh();
}
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
int noSwipeSize = mData.getAllToDo().size();
if (direction == ItemTouchHelper.LEFT) {
mData.deleteTask(mData.getAdapter().getResults().get(viewHolder.getAdapterPosition()));
mData.getAdapter().notifyItemRangeRemoved(viewHolder.getAdapterPosition(), noSwipeSize);
} else if (direction == ItemTouchHelper.RIGHT) {
mData.completeTask(mData.getAdapter().getResults().get(viewHolder.getAdapterPosition()));
mData.getAdapter().notifyItemRangeRemoved(viewHolder.getAdapterPosition(), noSwipeSize);
}
}
#Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
final View foregroundView = ((com.lab1.ac01220.bloomv2.ViewHolder) viewHolder).getForeground();
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
if(dX < 0){
this.mData.getViewHolder().getDeleteText().setVisibility(View.VISIBLE);
this.mData.getViewHolder().getCompleteText().setVisibility(View.INVISIBLE);
this.mData.getViewHolder().getBackground().setBackgroundColor(getResources().getColor(R.color.colorAccent));
} else if(dX >0){
this.mData.getViewHolder().getCompleteText().setVisibility(View.VISIBLE);
this.mData.getViewHolder().getDeleteText().setVisibility(View.INVISIBLE);
this.mData.getViewHolder().getBackground().setBackgroundColor(getResources().getColor(R.color.colorComplete));
}
}
ItemTouchHelper.SimpleCallback.getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,actionState, isCurrentlyActive);
}
#Override
public void onChildDrawOver(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
final View foregroundView = ((com.lab1.ac01220.bloomv2.ViewHolder) viewHolder).getForeground();
ItemTouchHelper.SimpleCallback.getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY,actionState, isCurrentlyActive);
}
}
RecycleitemTouchHelper.java
public RecycleItemTouchHelper(int dragDirs, int swipeDirs, RecyclerItemTouchHelperListener listener) {
super(dragDirs, swipeDirs);
this.listener = listener;
}
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
this.listener.onSwiped(viewHolder, direction, viewHolder.getAdapterPosition());
}
#Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
this.listener.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
#Override
public void onChildDrawOver(Canvas c, RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
this.listener.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
#Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
final View foregroundView = ((ViewHolder) viewHolder).getForeground();
getDefaultUIUtil().clearView(foregroundView);
}
#Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
return super.convertToAbsoluteDirection(flags, layoutDirection);
}
public interface RecyclerItemTouchHelperListener {
void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);
void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive);
void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive);
}
}
RecycleViewAdapter.java:
public class RecycleViewAdapter extends RecyclerView.Adapter<ViewHolder>{
private LayoutInflater inflater = null;
private List<GlobalLists> results = null;
private DataCommunication mData = null;
public RecycleViewAdapter(Context context, List<GlobalLists> results){
this.inflater = LayoutInflater.from(context);
this.results = results;
this.mData = (DataCommunication)context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_row, parent, false);
ViewHolder holder = new ViewHolder(view);
this.mData.setViewHolder(holder);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
GlobalLists item = this.results.get(position);
holder.setRowTitle(item.getTitle());
//Grab the first 14 characters of the contents if the row is not null
if(item.getContents() != null){
StringBuffer currentContent = new StringBuffer(item.getContents().substring(0, Math.min(item.getContents().length(),28)));
if(item.getContents().length() > 28) currentContent.append("...");
holder.setRowContent(currentContent.toString());
}
}
public void reloadAdapterData(List<GlobalLists> refresh){
this.results = refresh;
}
#Override
public int getItemCount() {
return this.results == null || this.results.isEmpty() ? 0 : this.results.size();
}
public List<GlobalLists> getResults(){
return this.results;
}
}

The problem was that I was simply setting the first ViewHolder to be a global variable and only anacting changes on that. Instead I should have been getting the current View being draw in onChildDraw and performing functions on that:
Was:
#Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
final View foregroundView = ((com.lab1.ac01220.bloomv2.ViewHolder) viewHolder).getForeground();
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
if(dX < 0){
this.mData.getViewHolder().getDeleteText().setVisibility(View.VISIBLE);
this.mData.getViewHolder().getCompleteText().setVisibility(View.INVISIBLE);
this.mData.getViewHolder().getBackground().setBackgroundColor(getResources().getColor(R.color.colorAccent));
} else if(dX >0){
this.mData.getViewHolder().getCompleteText().setVisibility(View.VISIBLE);
this.mData.getViewHolder().getDeleteText().setVisibility(View.INVISIBLE);
this.mData.getViewHolder().getBackground().setBackgroundColor(getResources().getColor(R.color.colorComplete));
}
}
ItemTouchHelper.SimpleCallback.getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,actionState, isCurrentlyActive);
}
Should be:
#Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
com.lab1.ac01220.bloomv2.ViewHolder current = (com.lab1.ac01220.bloomv2.ViewHolder) viewHolder;
final View foregroundView = ((com.lab1.ac01220.bloomv2.ViewHolder) viewHolder).getForeground();
if (isCurrentlyActive) {
if(dX < 0){
current.getDeleteText().setVisibility(View.VISIBLE);
current.getCompleteText().setVisibility(View.INVISIBLE);
current.getBackground().setBackground(getResources().getDrawable(R.drawable.ic_rounded_rectangle_accent));
} else if(dX >0){
current.getCompleteText().setVisibility(View.VISIBLE);
current.getDeleteText().setVisibility(View.INVISIBLE);
current.getBackground().setBackground(getResources().getDrawable(R.drawable.ic_rounded_rectangle_complete));
}
}
ItemTouchHelper.SimpleCallback.getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,actionState, isCurrentlyActive);
}

Related

Why i remove some item from the adapter viewPager2, viewPager can not call registerOnPageChangeCallback?

I added viewpager2 in my screen and set adapter on it, see adapter structure in below
public class NewTravelListAdapter extends RecyclerView.Adapter<NewTravelListAdapter.VH> {
MainActivityCallback newTravelCallback;
public Picasso picasso;
private BottomSheetBehavior mBottomSheetBehavior;
public ArrayList<NewTravelRequestPopUpModel> newTravelRequestPopUpModel;
public OptionsAdapter optionsAdapter;
private NewTravelSuggestPopupViewInterface.GetSize getSize;
Context context;
int viewHeight = 0;
private CountDownCustumTimer circle_progress_bar_timer;
ChangeItemTravelListInterface changeItemTravelListInterface;
VH holder;
public DiffUtil.ItemCallback<NewTravelRequestPopUpModel> diffUtil = new DiffUtil.ItemCallback<NewTravelRequestPopUpModel>(){
#Override
public boolean areItemsTheSame(#NonNull NewTravelRequestPopUpModel oldItem, #NonNull NewTravelRequestPopUpModel newItem) {
return false;
}
#Override
public boolean areContentsTheSame(#NonNull NewTravelRequestPopUpModel oldItem, #NonNull NewTravelRequestPopUpModel newItem) {
return false;
}
};
public AsyncListDiffer<NewTravelRequestPopUpModel> differ = new AsyncListDiffer<NewTravelRequestPopUpModel>(this , diffUtil);
public NewTravelListAdapter(#NonNull MainActivityCallback newTravelCallback, Picasso picasso,
ArrayList<NewTravelRequestPopUpModel> newTravelRequestPopUpModel,
NewTravelSuggestPopupViewInterface.GetSize getSize ,
ChangeItemTravelListInterface changeItemTravelListInterface){
this.newTravelCallback = newTravelCallback;
this.picasso = picasso;
this.getSize=getSize;
this.newTravelRequestPopUpModel = newTravelRequestPopUpModel;
optionsAdapter = new OptionsAdapter(picasso, newTravelRequestPopUpModel.get(0).optionsModelArrayList);
this.changeItemTravelListInterface = changeItemTravelListInterface;
}
#NonNull
#Override
public VH onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
ItemNewTravelSuggestPopupLayoutBinding binding = ItemNewTravelSuggestPopupLayoutBinding.inflate(inflater , parent , false);
return new VH(binding);
}
#Override
public void onBindViewHolder(#NonNull VH holder, int position) {
NewTravelRequestPopUpModel item = differ.getCurrentList().get(position);
this.holder = holder;
View view = holder.binding.newTravelSuggestPopup;
view.post(() -> {
viewHeight = view.getHeight()+20;
getSize.getHeight((view.getHeight()+50));
});
}
#Override
public int getItemCount() {
return differ.getCurrentList().size();
}
public void setList(ArrayList<NewTravelRequestPopUpModel> newTravelRequestPopUpModel){
differ.submitList(newTravelRequestPopUpModel);
notifyDataSetChanged();
}
public void removeItem(int pos){
notifyItemRemoved(pos);
changeItemTravelListInterface.itemRemoved(pos);
}
public static class VH extends RecyclerView.ViewHolder{
ItemNewTravelSuggestPopupLayoutBinding binding;
public VH(ItemNewTravelSuggestPopupLayoutBinding binding ) {
super(binding.newTravelSuggestPopup);
this.binding= binding;
}
}
}
Then I set adapter above to the viewPAger2 and listen with registerOnPageChangeCallback to viewPager2 scroll.
adapterList = new NewTravelListAdapter(newTravelCallback , picasso , newTravelRequestPopUpModel,
getSize , this);
setList(newTravelRequestPopUpModel);
binding.viewPager.setAdapter(adapterList);
binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
selectedTravel = newTravelRequestPopUpModel.get(position);
ChangeItem(selectedTravel , position);
newTravelCallback.changeRequest(selectedTravel , height);
}
#Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}
});
Now my problem is that after calling the ‍‍‍‍‍‍‍removeItem‍‍‍ method in the NewTravelListAdapter adapter
Removed the item from the adapter, but I can't listen to the adapter change

How to reset the background buttons (swipe menu) of the recyclerview programmatically?

I have a recyclerview which has a swipe menu (background buttons) on my android app.
The article that I referred to is: https://codeburst.io/android-swipe-menu-with-recyclerview-8f28a235ff28
This article was quite helpful and I succeeded to embed the basic function of swipe menu features.
However, after the filter function for the list of the recyclerview was embedded, I noticed that I have to figure out a way how to reset/clear the swipe menu manually/programmatically because after the list of the cardviews are filtered, the swipe menu is lingered in the background of each list but I have no clues how to solve this issue. (as shown in the attached pictures)
The things that I tried are:
notifyDataSetChanged by Adapter - not work
notifyItemChanged by Adapter - not work
reset the ItemTouchHelper (put null and reattach) - not work
recyclerview.performClick to reset the state of the ItemTouchHelper - not work
According to the article, it seems clicking a recyclerview resets the state of the ItemTouchHelper.
Could you please teach me how to click a recyclerview programmatically or any other solutions if available?
The following code is a fraction of the source from the article that I assumed relevant to solve this issue...
private void setTouchUpListener(final Canvas c,
final RecyclerView recyclerView,
final RecyclerView.ViewHolder viewHolder,
final float dX, final float dY,
final int actionState, final boolean isCurrentlyActive) {
recyclerView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
SwipeController.super.onChildDraw(c, recyclerView, viewHolder, 0F, dY, actionState, isCurrentlyActive);
recyclerView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
setItemsClickable(recyclerView, true);
swipeBack = false;
buttonShowedState = ButtonsState.GONE;
}
return false;
}
});
}
After the swipe menu emerged by swiping
Swipe menus remained after filtering the list
I found out that adding the layout-change listener on the recyclerview could remove the swipe menu.
#Override
public int getMovementFlags(#NotNull final RecyclerView recyclerView, #NotNull final RecyclerView.ViewHolder viewHolder) {
myRecyclerView = recyclerView;
myRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
v.removeOnLayoutChangeListener(this);
clearSwipeMenu();
}
});
}
....
#Override
public void onChildDraw(#NotNull Canvas c, #NotNull RecyclerView recyclerView, #NotNull RecyclerView.ViewHolder viewHolder,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ACTION_STATE_SWIPE) {
if (buttonShowedState != ButtonsState.GONE) {
if (buttonShowedState == ButtonsState.LEFT_VISIBLE) dX = Math.max(dX, buttonWidth);
if (buttonShowedState == ButtonsState.RIGHT_VISIBLE) dX = Math.min(dX, -buttonWidth);
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
} else {
setTouchListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
if (buttonShowedState == ButtonsState.GONE) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
currentItemViewHolder = viewHolder;
}
....
public void clearSwipeMenu() {
if(currentItemViewHolder!=null){
clearView(myRecyclerView, currentItemViewHolder);
currentItemViewHolder = null;
}
}

How do I delete Items from recycler view and Firebase database?

I have a recycler view connected to my Firebase database. I have implemented the ItemTouchHelper. SImpleCallback method so that I am able to slide and the items off to delete them. My codes work, the only issue is that I am not deleting the data from the database. I can't seem to find anything method that can help my situation since I haven't used DocumentReference.
Can anybody help me with this, How do I delete the items from the database when I click the delete icon in the on child draw?
personal fragment with the ItemTouch Helper
public class personalFragment extends Fragment {
private FirebaseAuth mAuth;
private String currentUserID;
private RecyclerView recyclerView;
private FirebaseRecyclerAdapter adapter;
private DatabaseReference mDatabase;
private TextView expense_txt;
LinearLayout mExpense;
#SuppressLint("SourceLockedOrientationActivity")
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View myView = inflater.inflate(R.layout.fragment_personal, container, false);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
mExpense = myView.findViewById(R.id.recycler_layout);
mDatabase = FirebaseDatabase.getInstance().getReference("Expenses").child(uid);
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
recyclerView = myView.findViewById(R.id.recycler_expense);
expense_txt = myView.findViewById(R.id.expenses_txt);
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true);
recyclerView.setHasFixedSize(true);
DisplayItemOnRecycler();
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
sumExpenses();
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
return myView;
}
public void deleteItem(){
}
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
if(direction == ItemTouchHelper.LEFT ){
Toast.makeText(getActivity(), "Deleting", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onChildDraw(#NonNull Canvas c, #NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
new RecyclerViewSwipeDecorator.Builder(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
.addBackgroundColor(ContextCompat.getColor(personalFragment.this.getActivity(), R.color.Delete))
.addActionIcon(R.drawable.ic_delete_black_24dp)
.create()
.decorate();
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
};
private void DisplayItemOnRecycler(){
Query query = FirebaseDatabase.getInstance()
.getReference("Expenses")
.child(currentUserID)
.limitToLast(100);
FirebaseRecyclerOptions<PersonalExpenses> options =
new FirebaseRecyclerOptions.Builder<PersonalExpenses>()
.setQuery(query, PersonalExpenses.class)
.build();
adapter = new FirebaseRecyclerAdapter<PersonalExpenses, MyViewHolder>(options) {
#Override
protected void onBindViewHolder(final MyViewHolder holder, int position, PersonalExpenses personalExpenses) {
holder.setDate(personalExpenses.getDate());
holder.setDescription(personalExpenses.getDescription());
holder.setAmount(personalExpenses.getAmount());
holder.setType(personalExpenses.getType());
holder.setCurrency(personalExpenses.getCurrency());
}
#Override
public MyViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.expense_recycler, parent, false);
return new MyViewHolder(view);
}
};
}
private void sumExpenses(){
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
int totalsum = 0;
for(DataSnapshot mySnapshot: dataSnapshot.getChildren()){
PersonalExpenses personal = mySnapshot.getValue(PersonalExpenses.class);
totalsum += Integer.parseInt(personal.getAmount());
String stTotalsum = String.valueOf((totalsum));
expense_txt.setText(stTotalsum);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onStart() {
super.onStart();
adapter.startListening();
}
#Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private final View mView;
public MyViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
protected void setDate(String date) {
TextView mDate = mView.findViewById(R.id.date_income);
mDate.setText(date);
}
protected void setType(String type) {
TextView mType = mView.findViewById(R.id.type_txt);
mType.setText(type);
}
protected void setDescription(String description) {
TextView mNote = mView.findViewById(R.id.note_txt);
mNote.setText(description);
}
protected void setAmount(String amount) {
TextView mAmount = mView.findViewById(R.id.amount_income);
String strammount = String.valueOf(amount);
mAmount.setText(strammount);
}
protected void setCurrency(String currency) {
TextView mCurrency = mView.findViewById(R.id.currency);
mCurrency.setText(currency);
}
}
}
This is the ItemTouchHelper method in the fragment above
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
if(direction == ItemTouchHelper.LEFT ){
Toast.makeText(getActivity(), "Deleting", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onChildDraw(#NonNull Canvas c, #NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
new RecyclerViewSwipeDecorator.Builder(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
.addBackgroundColor(ContextCompat.getColor(personalFragment.this.getActivity(), R.color.Delete))
.addActionIcon(R.drawable.ic_delete_black_24dp)
.create()
.decorate();
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
};

how to create RecyclerView drag and drop (swap 2 item positions version)

I have ItemTouchHelper class that uses swiping and drag and drop for performing actions. But i want to change drag and drop behavior. It should swap positions of 2 elements, the first one I dragged and the other one where it is dropped on. i want to exchange the items of the dragged & dropped positions. not to change positions of all items among both of them.
How to do it
this is my class for drag and drop
public class ItemTouchHelper extends
androidx.recyclerview.widget.ItemTouchHelper.Callback {
private Drawable icon;
private Context context;
private ColorDrawable background;
private final ItemTouchHelperListener dragDropListener;
public ItemTouchHelper(Context context, Drawable icon,
ItemTouchHelperListener dragDropListener) {
this.icon = icon;
this.dragDropListener = dragDropListener;
this.context = context;
this.background = new ColorDrawable(context.getResources().getColor(R.color.deleteItem));
}
#Override
public void onChildDraw(#NonNull Canvas c, #NonNull RecyclerView recyclerView,
#NonNull RecyclerView.ViewHolder viewHolder,
float dX, float dY,
int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX,
dY, actionState, isCurrentlyActive);
View itemView = viewHolder.itemView;
int iconMargin = (itemView.getHeight() - icon.getIntrinsicHeight()) / 2;
int iconTop = itemView.getTop() + (itemView.getHeight() - icon.getIntrinsicHeight()) / 2;
int iconBottom = iconTop + icon.getIntrinsicHeight();
if (dX > 0) {
background = new ColorDrawable(context.getResources().getColor(R.color.deleteItem));
iconMargin = (itemView.getHeight() - icon.getIntrinsicHeight()) / 2;
iconTop = itemView.getTop() + (itemView.getHeight() - icon.getIntrinsicHeight()) / 2;
iconBottom = iconTop + icon.getIntrinsicHeight();
int iconRight = itemView.getLeft() + iconMargin + icon.getIntrinsicWidth();
int iconLeft = itemView.getLeft() + iconMargin;
icon.setBounds(iconLeft, iconTop, iconRight, iconBottom);
background.setBounds(itemView.getLeft(), itemView.getTop(),
itemView.getLeft() + ((int) dX),
itemView.getBottom());
} else if (dX < 0) {
int iconRight = itemView.getRight() - iconMargin;
int iconLeft = itemView.getRight() - iconMargin - icon.getIntrinsicWidth();
icon.setBounds(iconLeft, iconTop, iconRight, iconBottom);
background.setBounds(itemView.getRight(), itemView.getTop(),
itemView.getRight() + ((int) dX),
itemView.getBottom());
} else {
background.setBounds(0, 0, 0, 0);
icon.setBounds(0, 0, 0, 0);
}
background.draw(c);
icon.draw(c);
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder,
int direction) {
dragDropListener.deleteElementDialog(viewHolder.getAdapterPosition());
}
#Override
public int getMovementFlags(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder) {
int dragFlags = UP | DOWN;
int swipeFlags = START | END;
return makeMovementFlags(dragFlags, swipeFlags);
}
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder,
#NonNull RecyclerView.ViewHolder target) {
dragDropListener.onRowMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
#Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder,
int actionState) {
if (actionState != ACTION_STATE_IDLE && actionState != ACTION_STATE_SWIPE) {
dragDropListener.onRowSelected(viewHolder);
}
super.onSelectedChanged(viewHolder, actionState);
}
#Override
public void clearView(#NonNull RecyclerView recyclerView,
#NonNull RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, Objects.requireNonNull(viewHolder));
dragDropListener.onRowClear(Objects.requireNonNull(viewHolder));
}
#Override
public boolean isLongPressDragEnabled() {
return true;
}
}
and this is my ItemTouchHelperListener:
public void setItemTouchHelperListener() {
ItemTouchHelperListener itemTouchHelperListener = new
ItemTouchHelperListener() {
#Override
public void onRowMoved(int fromPosition, int toPosition) {
presenter.rowMoved(fromPosition, toPosition);
}
#Override
public void onRowSelected(RecyclerView.ViewHolder myViewHolder) {
if (myViewHolder instanceof ElementsAdapter.ElementsViewHolder) {
elementsAdapter.rowSelected((ElementsAdapter.ElementsViewHolder) myViewHolder);
presenter.rowSelected(myViewHolder.getAdapterPosition());
}
}
#Override
public void onRowClear(RecyclerView.ViewHolder myViewHolder) {
if (myViewHolder instanceof ElementsAdapter.ElementsViewHolder) {
elementsAdapter.rowClear((ElementsAdapter.ElementsViewHolder) myViewHolder);
presenter.rowClear(myViewHolder.getAdapterPosition());
}
}
#Override
public void deleteElementDialog(int adapterPosition) {
createDeleteDialog(adapterPosition);
}
};
You can achieve this by registering both the dragged item using onMove() method, and the dropped item using clearView() method; then modify the data source of your RecyclerView adapter; so you can use a temp item that stores the dragged item; then set the dropped-by item with the dragged one; and finally put the temp item on the dropped one.
Then utilize RecyclerView adapter notifyItemChanged() for both items to update the layout with this change
Note: here I disabled the swiping as your question mainly on the drag & drop
final int[] oldPos = new int[1];
final int[] newPos = new int[1];
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(
ItemTouchHelper.UP |
ItemTouchHelper.DOWN |
ItemTouchHelper.LEFT |
ItemTouchHelper.RIGHT,
0) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
oldPos[0] = viewHolder.getAdapterPosition();
newPos[0] = target.getAdapterPosition();
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
}
#Override
public void clearView(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
moveItem(oldPos[0], newPos[0]);
}
});
private void moveItem(int oldPos, int newPos) {
Item temp = mItems.get(oldPos);
mItems.set(oldPos, mItems.get(newPos));
mItems.set(newPos, temp);
mAdapter.notifyItemChanged(oldPos);
mAdapter.notifyItemChanged(newPos);
}
The result
I totally agree with #Zain answer but there is one problem i.e suppose you want to replace the 1st item with 3rd then you will click and hold the 3rd item and drag it over the 1st item. Once you dropped, you will notice the 3rd item will get again shifted to its original position and after that, both items will get updated properly. It doesn't look good.
I've just slightly modified the #Zain answer.
private int fromPos = -1;
private int toPos = -1;
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new
ItemTouchHelper.SimpleCallback(
ItemTouchHelper.UP | ItemTouchHelper.DOWN |
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT, 0) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView,
#NonNull RecyclerView.ViewHolder viewHolder,
#NonNull RecyclerView.ViewHolder target) {
toPos = target.getAdapterPosition();
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder,
int direction) {}
#Override
public void onSelectedChange(#NonNull RecyclerView.ViewHolder
viewHolder, int actionState) {
switch(actionState){
case ItemTouchHelper.ACTION_STATE_DRAG:{
fromPos = viewHolder.getAdapterPosition();
break;
}
case ItemTouchHelper.ACTION_STATE_IDLE: {
//Execute when the user dropped the item after dragging.
if(fromPos != -1 && toPos != -1
&& fromPos != toPos) {
moveItem(fromPos, toPos);
fromPos = -1;
toPos = -1;
}
break;
}
}
private void moveItem(int oldPos, int newPos) {
Item temp = mItems.get(oldPos);
mItems.set(oldPos, mItems.get(newPos));
mItems.set(newPos, temp);
mAdapter.notifyItemChanged(oldPos);
mAdapter.notifyItemChanged(newPos);
}

RecyclerView drag and drop not working with several view types

I am trying to use a RecyclerView with a vertical LinearLayoutManager in order to display a list items. This list can contain several different item types (differents layouts), and it can be reordered by the user using drag and drop.
For the item types, as documented in Android documentation, I have overridden the getItemType method in order to handle different types of views in the recycler and handle it in the onCreateViewHolder and onBindViewHolder. This works like a charm.
For the drag and drop reorder, I have used a ItemTouchHelper.Callback (inspired by this sample project). This also works well.
The problem happen when I try to use different items types AND the drag and drop behaviour. As long as the drag occurs between items of the same type, this works well, but when i'm draggin a view of type A over a view of type B, the drag stop and the view returned to it's original position.
Here is my code:
MyFragment.java
public class MyFragment extends Fragment implements MyAdapter.Listener {
private MyViewModel mViewModel;
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private ItemTouchHelper mItemTouchHelper;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment, container, false);
mRecyclerView = root.findViewById(R.id.recyclerView);
mAdapter = new MyAdapter(this);
mRecyclerView.setAdapter(mAdapter);
mItemTouchHelper = new ItemTouchHelper(new MyDragHelperCallback(mAdapter));
mItemTouchHelper.attachToRecyclerView(mRecyclerView);
mViewModel = ViewModelProviders.of(getActivity()).get(MyViewModel.class);
mViewModel.addObserver(this, new Observer<List<Item>>() {
#Override
public void onChanged(#Nullable List<Item> items) {
mAdapter.updateList(items);
}
});
mAdapter.updateList(mViewModel.getList());
return root;
}
#Override
public void onStartDragRequest(#NonNull RecyclerView.ViewHolder viewHolder) {
mItemTouchHelper.startDrag(viewHolder);
}
}
MyDragHelperCallback.java
public class MyDragHelperCallback extends ItemTouchHelper.Callback {
private static final int DRAG_MOVEMENT_FLAGS = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
#NonNull
private MyDragListener mListener;
public MyDragHelperCallback(#NonNull MyDragListener listener) {
mListener = listener;
}
#Override
public boolean isLongPressDragEnabled() {
return true;
}
#Override
public boolean isItemViewSwipeEnabled() {
return false;
}
#Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (!(recyclerView.getLayoutManager() instanceof LinearLayoutManager)) {
throw new IllegalArgumentException("Should only be used with a LinearLayoutManager");
}
return makeMovementFlags(DRAG_MOVEMENT_FLAGS, 0);
}
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
mListener.onItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// Nothing to do
}
}
MyDragListener.java
public interface MyDragListener {
void onItemMoved(int from, int to);
}
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyHolder> implements MyDragListener {
#Nullable
private List<Item> mList;
public void updateList(#Nullable List<Item> list) {
mList = list;
notifyDataSetChanged();
}
#Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 0) {
return new MyHolderBis(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bis, parent, false));
}
return new MyHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
}
#Override
public void onBindViewHolder(final MyHolder holder, int position) {
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
mListener.onStartDragRequest(holder);
return true;
}
});
// bind views to data
}
#Override
public int getItemCount() {
return mList != null ? mList.size() : 0;
}
#Override
public int getItemViewType(int position) {
return mList.get(position).getType();
}
#Override
public void onItemMoved(int from, int to) {
notifyItemMoved(from, to);
}
}
Is there something I missing ?
Or is this simply to possible to achieve ?
Thanks for all your answers !

Categories