fragment之间传值

//eventbus
compile 'org.greenrobot:eventbus:3.0.0'

 

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MenuFragment menuFragment = new MenuFragment();
        final MainFragment mainFragment  = new MainFragment();

        //将上面的两个Fragment添加进来
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_menu, menuFragment, "menuFragment").commit();
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_main, mainFragment, "mainFragment").commit();

        menuFragment.setOnDataTransmissionListener(new MenuFragment.OnDataTransmissionListener() {

            @Override
            public void dataTransmission(String data) {
                mainFragment.setData(data);
            }
        });
    }
}

 

 

public class MainFragment extends Fragment {
    private Button bt_main;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mainment, container, false);
        bt_main = (Button) view.findViewById(R.id.bt_main);

        EventBus.getDefault().register(this);
        return view;
    }

    @Subscribe
    public void onEvent(String data) {
        bt_main.setText(data);
    }
    public void setData(String string) {
        bt_main.setText(string);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
public class MenuFragment extends Fragment {
    List<String> mDatas = new ArrayList<>();
    private OnDataTransmissionListener mListener;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.menument, container, false);


        for(int i = 1; i <= 5; i++) {
            mDatas.add("这是第"+ i + "条数据");
        }

        final ListView lv = (ListView) view.findViewById(R.id.lv_menu);
        lv.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return mDatas.size();
            }

            @Override
            public Object getItem(int position) {
                return mDatas.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.menu_fragemnt_item, parent, false);
                TextView tv = (TextView) contentView.findViewById(R.id.tv_menu_item);
                tv.setText(mDatas.get(position));

                /**
                 * 需求:点击对应的条目,将条目的内容发送到MainFragment中的Button上,更改Button名称(即进行一个数据传递)
                 */

                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        /**
                         * 方法三:使用第三方开源框架EventBus
                         */
                        EventBus.getDefault().post(mDatas.get(position));
                    }
                });

                return contentView;
            }
        });

        return view;
    }

    //接口回调的方法
    public interface OnDataTransmissionListener {
        public void dataTransmission(String data);
    }

    public void setOnDataTransmissionListener(OnDataTransmissionListener mListener) {
        this.mListener = mListener;
    }}

 

main.xml

<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"
    tools:context="dayu.sn.com.activity_ment.MainActivity"
    android:orientation="horizontal">

    <FrameLayout
        android:id="@+id/fl_menu"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:background="#20ff0000"
        android:layout_height="match_parent"/>
    <FrameLayout
        android:id="@+id/fl_main"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:background="#2500ff00"
        android:layout_height="match_parent"/>


</LinearLayout>

mainment.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/bt_main"
        android:background="#ff0"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:text="根据条目改变内容"
        android:layout_centerInParent="true"/>


</RelativeLayout>

menu_fragment_item.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_menu_item"/>
</RelativeLayout>

menument.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/lv_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

tv_menu_item.xml一个布局

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值