简体   繁体   中英

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

My MainActivity hosts 4 fragments. All the fragments are recycler views but 3 have a details fragment. 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.

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.

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

And override the onAttach method in the fragment which I did like so

@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

@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

@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. Thank you for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM