简体   繁体   English

单击recyclerView项目时,将对象从一个片段传递到另一个片段

[英]Pass Object from one fragment to another fragment when recyclerView item is clicked

My MainActivity hosts 4 fragments. 我的MainActivity包含4个片段。 All the fragments are recycler views but 3 have a details fragment. 所有片段都是回收者视图,但3包含详细信息片段。 I have the class below to implement recycler view click listener. 我在下面的类中实现了回收站视图单击侦听器。

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);    
        public void onLongItemClick(View view, int position);
    }

    GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && mListener != null) {
                    mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
            return true;
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {}

    @Override
    public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}

In my Series Fragment, I implement it like so 在我的系列片段中,我像这样实现

recyclerView.addOnItemTouchListener(
    new RecyclerItemClickListener(getContext(), recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            mListener.onSeriesSelected(series, position);
            Series series = seriesList.get(position);
            Toast.makeText(getContext(), series.getTitle() + " " + series.getId() + " is selected!", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onLongItemClick(View view, int position) {}
}));

Now, I want to open a SeriesDetail fragment when the recyclerView is clicked and pass the Series object of that recycler view to the SeriesDetail fragment. 现在,我想在单击recyclerView时打开SeriesDetail片段,然后将该回收器视图的Series对象传递给SeriesDetail片段。

I read that the Series model must implement Parcelable which I did with the help of an IntelliJ plugin and that I would need to implement an interface in the Series fragment. 我读到Series模型必须实现在IntelliJ插件的帮助下实现的Parcelable,并且我需要在Series片段中实现接口。

public interface onSeriesSelectedListener {
    void onSeriesSelected(Series series, int position);
}

And override the onAttach method in the fragment which I did like so 并在我喜欢的片段中覆盖onAttach方法

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    // This makes sure that the container activity has implemented
    // the listener interface. If not, it throws an exception
    if (context instanceof onSeriesSelectedListener) {
        mListener = (onSeriesSelectedListener) context;
    } else {
        throw new ClassCastException(context.toString()
 + " must implement onSeriesSelectedListener");
    }
}

And finally, override the interface in the MainActivity which I did like this 最后,重写我这样做的MainActivity中的接口

@Override
public void onSeriesSelected(Series series, int position) {
    Fragment fragment = new SeriesDetailFragment();
    Bundle bundle = new Bundle();
    fragment.setArguments(bundle);
    bundle.putParcelable("seriesDetail", series);
    fragment.setArguments(bundle);
    getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.fragment_container, fragment)
        .addToBackStack(null)
        .commit();
}

Now in the onCreate method of the SeriesDetail fragment, I am supposed to access the Parcelable object like so 现在,在SeriesDetail片段的onCreate方法中,我应该像这样访问Parcelable对象

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = this.getArguments();
    if (bundle != null) {
        Series series = bundle.getParcelable("seriesDetail");
        if (series != null) {
            Toast.makeText(getContext(), series.getTitle() + " is selected!", Toast.LENGTH_LONG).show();
            Log.e("Series", "Title:" + series.getTitle());
        }
    }
}

Yet, I do not get the object at this point. 但是,目前我还没有得到目标。 What I'm I doing wrong? 我做错了什么? Please help me!!! 请帮我!!! I've been at this for hours. 我已经在这里待了几个小时了。 Thank you 谢谢

    recyclerView.addOnItemTouchListener (new RecyclerItemClickListener(getContext(), 
     recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
        Series series = seriesList.get(position);  
        mListener.onSeriesSelected(series, position);                      
    }

    @Override
    public void onLongItemClick(View view, int position) {}
    }));

The listener was firing before the series position could be gotten thereby making Series null in MainActivity. 侦听器在无法获得系列位置之前触发,从而使MainActivity中的Series为空。 Thank you for your help. 谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kotlin:将 object 从 recyclerview 传递到另一个片段以在单击时进行编辑 - Kotlin: Passing object from recyclerview to another fragment to edit when clicked 单击 RecyclerView 项时如何从一个片段移动到另一个片段 - How to move from one fragment to another fragment on click of a RecyclerView item 将从RecyclerView单击的项目位置传递给Fragment父亲 - Pass Item Position Clicked from a RecyclerView to the Fragment father 单击项目时从 gridview 片段移动到另一个片段 - move from gridview fragment when item is clicked to another fragment 将 Fragment 中 RecyclerView 的数据传递到另一个 Fragment - Pass data from RecyclerView in Fragment to another Fragment 将回收物品从一个碎片传递到另一个碎片 - Pass recycler item from one fragment to another 将片段一的RecyclerView项位置传递到片段二的另一个RecyclerView适配器 - Pass RecyclerView item position of Fragment One to another RecyclerView Adapter of Fragment Two Android如何在单击RecyclerView项时从片段发送Intent - Android how to send an Intent from a fragment when a RecyclerView item is clicked 单击项目时将数据从 RecyclerView 传递到 Fragment - Passing Data from RecyclerView to Fragment when Item is Clicked 单击 recyclerView 项目时未打开新片段 - new Fragment is not opening when recyclerView item is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM