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

时间:2017-11-17 20:56:02

标签: android android-fragments parcelable

我的MainActivity托管4个片段。所有片段都是回收者视图,但3个片段有详细信息片段。我在下面的课程中实现了回收站视图点击监听器。

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

在我的系列片段中,我像这样实现它

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

现在,我想在单击recyclerView时打开一个SeriesDetail片段,并将该Recycler视图的Series对象传递给 SeriesDetail片段。

我读到系列模型必须实现Parcelable,我在IntelliJ插件的帮助下完成,我需要在Series片段中实现一个接口。

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

并覆盖我喜欢的片段中的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");
    }
}

最后,覆盖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();
}

现在在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());
        }
    }
}

然而,我现在还没有得到这个对象。我做错了什么?请帮我!!!我已经在这几个小时了。谢谢

1 个答案:

答案 0 :(得分:0)

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

在获得系列位置之前,侦听器正在触发,从而使MainActivity中的Series为null。谢谢你的帮助。