目的:当是竖屏时,用ListView提供的新闻标题列表,点击新闻列表中的某个标题,它显示本标题对应的新闻内容。
当是横屏时,屏幕分为两个部分,左边部分为新闻标题列表,右边为新闻内容。
第一步:新建一个FragmentTest项目,启动后能让它在手机上初次正上运行;
第二步:activity_main.xml中有一个NewTitleFragment碎片(fragment_newtitle.xml),碎片中有ListView;
activity_main.xml文件:
<FrameLayout android:id="@+id/layoutF" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/newTitle" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.jintingbo.fragmenttest.NewTitleFragment" tools:layout="@layout/fragment_newtitle"/> </FrameLayout>
为什么要用FrameLayout把碎片包住呢?因为如果碎片不被包着,另一个碎片就替换不了当前的碎片。
fragment_newtitle.xml文件
<ListView android:id="@+id/newsTitleList" android:layout_width="match_parent" android:layout_height="match_parent" />
这些布局文件对应的活动是:
NewTitleFragment .java
public class NewTitleFragment extends Fragment implements AdapterView.OnItemClickListener { //准备新闻数据 public List<String> title=new ArrayList(); public List<String> newsContent=new ArrayList<>(); @Override public void onAttach(Context context) { super.onAttach(context); //初始化新闻数据 title.add("新闻七");title.add("新闻八");title.add("新闻九"); newsContent.add("中华人民共和国"); newsContent.add("日本,尼日尼亚"); newsContent.add("意大利面条"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //将fragment_newtitle变成碎片视图 View newsFrgToView= inflater.inflate(R.layout.fragment_newtitle, container, false); //从此碎片视图中找到ListView控件 final ListView newsListView=(ListView)newsFrgToView.findViewById(R.id.newsTitleList); //为ListView设置标题的适配器 ArrayAdapter<String> newsAdapte=new ArrayAdapter<String>( getActivity(), //注意这里一定是它所在的活动,用本碎片对象不行 android.R.layout.simple_list_item_1, //单行 title //设置的标题数组 ); newsListView.setAdapter(newsAdapte); //设置标题 newsListView.setOnItemClickListener(this); //设置标题的点击事件 return newsFrgToView; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //获得新闻的内容 String str_position=newsContent.get(position); Log.d("NewTitleFragment",str_position); //为什么不能用下面这句呢?因为此时本碎片还没有在活动之中,所以活动中是找不到rf的 //RightFragment rf=(RightFragment)getActivity().getSupportFragmentManager() //.findFragmentById(R.id.rfLayout); //只好用new去做一个rf了,rf是内容碎片,当点击标题时,就用这个碎片去替换ListView所在的碎片 RightFragment rf=new RightFragment(); //先把新闻内容设置到右碎片,右碎片其实是内容碎片,这里的思路是将数据设置好后再跳转 rf.setData(newsContent.get(position)); //以下是碎片替换的步骤 FragmentManager fm=getFragmentManager(); FragmentTransaction ftr=fm.beginTransaction(); ftr.replace(R.id.layoutF,rf); ftr.addToBackStack(null); ftr.commit(); } }
RightFragment.java文件 public class RightFragment extends Fragment { //接收新闻内容 public String content_str; @Override public void onAttach(Context context) { super.onAttach(context); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View fragmentView= inflater.inflate(R.layout.fragment_right, container, false); //注意下面这句,一定是获取碎片视图中的t1,不能是获取活动中的t1 //所以你要是将下句的fragmentView.findViewById()改为getActivity().findViewById() //肯定就会出错 TextView tv1=(TextView)fragmentView.findViewById(R.id.t1); tv1.setText(content_str); return fragmentView; } @Override public void onDestroyView() { super.onDestroyView(); } //接收传过来的新闻内容 public void setData(String str){ content_str=str; } }