从嵌套的RecyclerView中获取onSwipe事件到父片段

huangapple go评论72阅读模式
英文:

Getting onSwipe event from a nested RecyclerView to the parent Fragment

问题

我正在开发一个应用程序,在其中我的主要活动通过SectionsPageAdapter托管不同的片段。我的问题是关于其中一个大致类似于下面图片的片段:

从嵌套的RecyclerView中获取onSwipe事件到父片段

我的innerRecycler列表包含不同的项目,我希望能够通过滑动删除这些项目,并通过DialogFragment使用户验证删除操作。我已经能够实现滑动操作,但我目前遇到的问题是在OuterRecycler中实现一个OnItemTouch监听器,以便我可以使用接口将操作发送回我的片段,然后在之后显示我的DialogFragment。

我的OuterRecycler的代码目前如下:


    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) {
    
            }
        };
    
    }

OnItemTouchListener是实现此功能的正确方法吗?还是我需要创建一个新的回调方法?

英文:

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:

从嵌套的RecyclerView中获取onSwipe事件到父片段

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&lt;OuterRecyclerAdapter.InsideRecyclerHolder&gt; {
    
        private final Context mContext;
        private final String mText;
        private final ArrayList&lt;MainViewArrayListMasterList&gt; mDataArrayList;
    
        OuterRecyclerAdapter(Context context, ArrayList&lt;MainViewArrayListMasterList&gt; 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 ?

答案1

得分: 0

我通过从我的 OuterRecycler 中调用 DialogFragment 来绕过了这个问题:

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 = DataArrayList;
            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");

		}
    
    }
英文:

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

public class OuterRecyclerAdapter extends RecyclerView.Adapter&lt;OuterRecyclerAdapter.InsideRecyclerHolder&gt; {
    
        private final Context mContext;
        private final String mText;
        private final ArrayList&lt;MainViewArrayListMasterList&gt; mDataArrayList;
		
		private FragmentManager mFragmentManager;
    
        OuterRecyclerAdapter(Context context, FragmentManager FragmentManager, ArrayList&lt;MainViewArrayListMasterList&gt; 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, &quot;MyDialogDialog&quot;);

		}
    
    }

huangapple
  • 本文由 发表于 2020年10月7日 18:01:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64241705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定