从适配器(类)通过EventBus跳转到Fragment并传值 超简单

在适配器类里做一个点击监听(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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值