0

I want to implement an android swipe layout that updates the visibility of buttons when swiped with an item touch helper. I was able to get the id of, it but is not working when changing visibility when swiped

Subroutine.class

private void setItemTouchHelper(SubroutineParentItemAdapter.ParentItemViewHolder holder, SubroutineChildItemAdapter childAdapterItem){
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {

        @Override
        public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
            return makeMovementFlags(0, ItemTouchHelper.END | ItemTouchHelper.START);
        }

        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public boolean isLongPressDragEnabled() {
            return false;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {

            Button Remove = viewHolder.itemView.findViewById(R.id.remove_btn);

            switch (direction){
                case ItemTouchHelper.END:
                case ItemTouchHelper.START:
                    //Remove id is same as on adapter but not working when changed visibility 2131231725
                    if (Remove.getVisibility() == View.VISIBLE) {
                        Remove.setVisibility(View.GONE);
                    } else if (Remove.getVisibility() == View.GONE){
                        Remove.setVisibility(View.VISIBLE);
                    }
                    childAdapterItem.notifyDataSetChanged();
                    break;
            }
        }
    });
    itemTouchHelper.attachToRecyclerView(holder.childRecycleView);
}

Adapter.class

On view holder the button where initialized

public static class ChildItemViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout itemLayout;
        TextView Title, Description;
        Button Upvote, Downvote, MarkAsDone, Remove;

        public ChildItemViewHolder(@NonNull View itemView) {
            super(itemView);
            itemLayout = itemView.findViewById(R.id.subroutine_child_item_layout);
            Title = itemView.findViewById(R.id.subroutine);
            Description = itemView.findViewById(R.id.home_item_on_click_habit_description);

            Upvote = itemView.findViewById(R.id.btn_upvote_subroutine);
            Downvote = itemView.findViewById(R.id.btn_downvote_subroutine);
            MarkAsDone = itemView.findViewById(R.id.mark_as_done);
            Remove = itemView.findViewById(R.id.remove_btn);
        }

I tried to access the component with:

 Button Remove = viewHolder.itemView.findViewById(R.id.remove_btn);

I wanna know how to reference or get the adapter class initialized components eg text view, button, etc. And use it on subroutine.class wherein the adapter is initialized and item touch helper is implemented.

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • You can change the remove button's visibility from `VISIBLE` to `GONE but can't do the vice versa? Your approach looks ok in the `Subroutine.onSwiped` function. – Kozmotronik Dec 04 '22 at 10:36
  • Thank you, but any idea how I can reference the button view from adapter class to swipe function? `Button Remove = viewHolder.itemView.findViewById(R.id.remove_btn);` This thing doest not work :( – Gilbert Almazan Dec 04 '22 at 13:59
  • 1
    Have you tried casting the viewholder to `ChildItemViewHolder`. The ID of the Remove button is same in everywhere. But you need to get reference to the created one when swipe occurs. Have you initialized the `ItemTouchHelper` with your adapter correctly? Share all relevant code so that we see whether the initialization correct. Maybe the `onSwipe` is never called at all who knows? – Kozmotronik Dec 04 '22 at 15:20
  • Yes the onSwipe listener is initialized properly and responding on swipe motions by attaching the recyclerview `itemTouchHelper.attachToRecyclerView(holder.childRecycleView);` – Gilbert Almazan Dec 04 '22 at 15:59
  • Casting the viewHolder works with `RecyclerView.Adapter` `public class SubroutineChildItemAdapter extends RecyclerView.Adapter` but when I used it on a `ListAdapter` for the RV adapter, it did not work. `public class SubroutineModifyParentItemAdapter extends ListAdapter` - List Adapter was used because it handles data change and animation automatically. – Gilbert Almazan Dec 04 '22 at 16:16
  • I'll write a follow up to this thread but focus on casting viewHolder from a adapter that use ListAdapter – Gilbert Almazan Dec 04 '22 at 16:22
  • https://stackoverflow.com/questions/74678681/why-casting-a-viewholder-to-a-adapter-that-extend-to-listadapter-does-not-work the follow up thread – Gilbert Almazan Dec 04 '22 at 16:40

0 Answers0