在适配器类里做一个点击监听(EventBus使用截EventBus使用)
viewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int id = rxxp.getCommodityList().get(i).getCommodityId(); EventBus.getDefault().post(id);//EventBus发送 } });到你要接收的Fragment里接收替换
/** * 从适配器传过来id 替代当前Fragment * @param id */ @Subscribe(threadMode = ThreadMode.MAIN) public void getMessage(Integer id) { Log.d(TAG, "getMessage: " + id); String ID = id.toString(); DetailsFrgament detailsFrgament = new DetailsFrgament();//要替换的Frameent类 Bundle bundle = new Bundle();//传值用 bundle.putString("ids",ID); detailsFrgament.setArguments(bundle); getFragmentManager()//开启事务管理 .beginTransaction() .addToBackStack(null)//将当前Fragment加入到返回栈 .replace(R.id.home_frameLayout,detailsFrgament).commit();//home_frameLayout是当前布局里的FrameLayout }我的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".HomeActivity"> <FrameLayout android:id="@+id/home_frameLayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9.5"></FrameLayout> <RadioGroup android:id="@+id/rg_RadioGroup" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@mipmap/bg_homepage_bottom" android:gravity="center" android:orientation="horizontal"> <RadioButton android:id="@+id/rb_home" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:checked="true" android:drawableBottom="@drawable/selector_home" android:gravity="center" /> <RadioButton android:id="@+id/rb_circle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableBottom="@drawable/selector_circle" android:gravity="center"/> </RadioGroup> </LinearLayout>