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 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



    
    


mainment.xml


    

menu_fragment_item.xml



menument.xml


    

tv_menu_item.xml一个布局

 

你可能感兴趣的:(fragment之间传值)