//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一个布局