效果图:
步骤:
1.分别定义主页,左fragment,右fragment的布局
2.在Left_fragemnt定义一个接口
3.在MainActivity中new一个right_fragment对象
4.MainActivity实现Left_fragment接口,最终调用Right_fragment中的方法
结构图如下:
代码:
//activity_main <fragment android:id="@+id/left_fg" android:name="org.mobiletrain.a7_4fragment.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <LinearLayout android:id="@+id/right_fg" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:orientation="vertical"/> //left_layout <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> //right_layout <TextView android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent"/>
*left_fragment
public class LeftFragment extends Fragment { private List<String> list; //1.创建接口 public interface UpdateRightFg { public void updateRightFg(String fileName); } //2.声明接口变量 private UpdateRightFg updateRightFg; //3.实例化接口变量,该方法在Activity和Fragment产生关联时调用 //参数Activity指Fragment的宿主Activity @Override public void onAttach(Activity activity) { super.onAttach(activity); if (activity instanceof UpdateRightFg) { updateRightFg = (UpdateRightFg) activity; } } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_layout, null); ListView listView = (ListView) view.findViewById(R.id.lv); initData(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { //4.调用接口中的方法 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (updateRightFg != null) { updateRightFg.updateRightFg(list.get(position)); } } }); return view; } private void initData() { list = new ArrayList<>(); list.add("day1.txt"); list.add("day2.txt"); list.add("day3.txt"); list.add("day4.txt"); } }
*MainActivity
//5.在Activity中实现接口 public class MainActivity extends AppCompatActivity implements LeftFragment.UpdateRightFg { private RightFragment rightFg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); rightFg = new RightFragment(); transaction.replace(R.id.right_fg, rightFg).commit(); } @Override public void updateRightFg(String fileName) { rightFg.updateTextView(fileName); } }
*right_fragment
public class RightFragment extends Fragment { private TextView content; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.right_layout, null); content = (TextView) view.findViewById(R.id.content); return view; } public void updateTextView(String fileName) { content.setText(getStringFromAssets(fileName)); } /** * 根据文件名从资产文件夹中读取文件内容 * * @param fileName 文件名 * @return */ public String getStringFromAssets(String fileName) { //获得资产文件夹管理器 AssetManager assets = getActivity().getAssets(); StringBuffer result = new StringBuffer(); InputStream is = null; try { //通过IO流读取资产文件中的内容 is = assets.open(fileName); int len; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { result.append(new String(buf, 0, len)); } } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return result.toString(); } }