Skip to content
Advertisement

Getting onSwipe event from a nested RecyclerView to the parent Fragment

I’m developping an app in which my main activity hosts different fragments via a SectionsPageAdapter. My question is about one of those fragments that roughtly looks like the image bellow:

Activity layout architecture

My innerRecyclers lists differents items that I want to be able to remove by swiping and make the user validate the deletion via a DialogFragment. I was able to implement the swipe action but what I’m struggly to do is to implement a listener in the OuterRecycler for OnItemTouch so I can use an interface and send the action back to my fragment which will dispay my DialoFragment afterwards.

The code of my OuterRecycler currently looks like that:

    public class OuterRecyclerAdapter extends RecyclerView.Adapter<OuterRecyclerAdapter.InsideRecyclerHolder> {
    
        private final Context mContext;
        private final String mText;
        private final ArrayList<MainViewArrayListMasterList> mDataArrayList;
    
        OuterRecyclerAdapter(Context context, ArrayList<MainViewArrayListMasterList> DataArrayList, String text) {
            mContext = context;
            mDataArrayList = RecyclerArrayListType;
            mText = text;
        }
    
        @NonNull
        @Override
        public OuterRecyclerAdapter.InsideRecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater myInflater = LayoutInflater.from(mContext);
            View myOwnView = myInflater.inflate(R.layout.outer_recycler, parent, false);
    
            return new OuterRecyclerAdapter.InsideRecyclerHolder(myOwnView);
        }
    
        @Override
        public void onBindViewHolder(@NonNull OuterRecyclerAdapter.InsideRecyclerHolder holder, int position) {
    
            MainViewArrayListMasterList currentItem = mDataArrayList.get(position);
    
            holder.CardTitle.setText(String.valueOf(currentItem.getmYear()));
    
            LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
            holder.CardRecycler.setLayoutManager(mLayoutManager);
            InnerRecyclerAdapter historyInnerView_Adapter = new InnerRecyclerAdapter(mContext, currentItem.getmArrayListStored(), mText);
            new ItemTouchHelper(ListHistoryViewSwiped).attachToRecyclerView(holder.CardRecycler);
            holder.CardRecycler.setAdapter(historyInnerView_Adapter);
    
        }
    
        @Override
        public int getItemCount() {
            return mDataArrayList.size();
        }
    
        static class InsideRecyclerHolder extends RecyclerView.ViewHolder {
    
            final TextView CardTitle;
            final RecyclerView CardRecycler;
    
            InsideRecyclerHolder(@NonNull View itemView) {
                super(itemView);
    
                CardTitle = itemView.findViewById(R.id.TitleText);
                CardRecycler = itemView.findViewById(R.id.Inside_Recycler);
            }
        }
    
        ItemTouchHelper.SimpleCallback ListHistoryViewSwiped = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                return false;
            }
    
            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    
            }
        };
    
    }

Is OnItemTouchListener the good way to acheive this ? Or do I have to create a new callback method ?

Advertisement

Answer

I actually circumvented this issue by calling the DialogFragment from my OuterRecycler:

public class OuterRecyclerAdapter extends RecyclerView.Adapter<OuterRecyclerAdapter.InsideRecyclerHolder> {
    
        private final Context mContext;
        private final String mText;
        private final ArrayList<MainViewArrayListMasterList> mDataArrayList;
        
        private FragmentManager mFragmentManager;
    
        OuterRecyclerAdapter(Context context, FragmentManager FragmentManager, ArrayList<MainViewArrayListMasterList> DataArrayList, String text) {
            mContext = context;
            mFragmentManager = FragmentManager;
            mDataArrayList = RecyclerArrayListType;
            mText = text;
        }
    
        @NonNull
        @Override
        public OuterRecyclerAdapter.InsideRecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater myInflater = LayoutInflater.from(mContext);
            View myOwnView = myInflater.inflate(R.layout.outer_recycler, parent, false);
    
            return new OuterRecyclerAdapter.InsideRecyclerHolder(myOwnView);
        }
    
        @Override
        public void onBindViewHolder(@NonNull OuterRecyclerAdapter.InsideRecyclerHolder holder, int position) {
    
            MainViewArrayListMasterList currentItem = mDataArrayList.get(position);
    
            holder.CardTitle.setText(String.valueOf(currentItem.getmYear()));
    
            LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
            holder.CardRecycler.setLayoutManager(mLayoutManager);
            InnerRecyclerAdapter historyInnerView_Adapter = new InnerRecyclerAdapter(mContext, currentItem.getmArrayListStored(), mText);
            new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
                
                @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) {
                    openMyDialog(currentItem.getmArrayListStored().get(viewHolder.getAdapterPosition()));
                }
                
            }).attachToRecyclerView(holder.CardRecycler);
            holder.CardRecycler.setAdapter(historyInnerView_Adapter);
    
        }
    
        @Override
        public int getItemCount() {
            return mDataArrayList.size();
        }
    
        static class InsideRecyclerHolder extends RecyclerView.ViewHolder {
    
            final TextView CardTitle;
            final RecyclerView CardRecycler;
    
            InsideRecyclerHolder(@NonNull View itemView) {
                super(itemView);
    
                CardTitle = itemView.findViewById(R.id.TitleText);
                CardRecycler = itemView.findViewById(R.id.Inside_Recycler);
            }
        }
        
        private void openMyDialog(MainViewArrayListRecycler itemToDelete) {

            MyDialog myDialog = MyDialog.newInstance(itemToDelete);
            myDialog.show(mFragmentManager, "MyDialogDialog");

        }
    
    }
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement