res/layout/activity_main.xml的内容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <fragment class="com.example.demofragment.ListFragment" android:id="@+id/titles" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent"/> </LinearLayout>
res/layout-land/activity_main.xml的内容:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <fragment class="com.example.demofragment.ListFragment" android:id="@+id/titles" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent"/> <FrameLayout android:id="@+id/detail" android:layout_weight="2" android:layout_width="0px" android:layout_height="match_parent"/> </LinearLayout>
package com.example.demofragment; /** * @author cjianquan * */ public final class Data { // public static final String[] TITLES={ "线性布局", "表格布局", "帧布局", "相对布局" }; public static final String[] DETAIL= { "线性布局", "表格布局", "帧布局", "相对布局" }; }
package com.example.demofragment; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; public class ListFragment extends android.app.ListFragment { boolean dualPane; int curCheckPosition =0 ; public void onActivityCreated(Bundle savedInstanceState){ super.onActivityCreated(savedInstanceState); //为列表设置适配器 setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_checked,Data.TITLES)); //获取布局文件中添加的FragmentLayout帧布局管理器 View detailFrame = getActivity().findViewById(R.id.detail); //判断是否在一屏上同时显示列表和详细内容 dualPane = detailFrame != null && detailFrame.getVisibility() == View.VISIBLE; if(savedInstanceState!=null){ //更新当前选择的索引位置 curCheckPosition = savedInstanceState.getInt("curChoice", 0); } if(dualPane){ //如果在一屏上同时显示列表和详细内容 //设置列表为单选模式 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); //显示详细内容 showDetails(curCheckPosition); } } //保存当前选中的列表项的索引值 public void onSaveInstanceSate(Bundle outState){ super.onSaveInstanceState(outState); outState.putInt("curChoice", curCheckPosition); } public void onListItemClick(ListView l,View v,int position,long id){ showDetails(position);//调用showDetails方法显示 详细内容 } void showDetails(int index){ //更新保存当前索引位置的变量的值为当前选中值 curCheckPosition = index; if(dualPane){//当在一屏上同时显示列表和详细内容时 //设置选中列表为选中状态 getListView().setItemChecked(index, true); //获取 用于显示详细内容的fragment DetailFragment details = (DetailFragment)getFragmentManager().findFragmentById(R.id.detail); if(details == null || details.getShownIndex() != index){ //创建一个新的DetailFragment实例用于显示当前选择项对应的详细内容 details = DetailFragment.newInstance(index); //要在activity中管理fragment,需要使用FragmentManager FragmentTransaction ft = getFragmentManager().beginTransaction(); //替换原来显示的详细内容 ft.replace(R.id.detail, details); //设置转换效果 ft.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.commit(); } }else{//在一屏上只能显示列表或详细内容的一个内容时 //使用一个新的activity显示详细内容 Intent intent = new Intent(getActivity(),MainActivity.DetailActivity.class); intent.putExtra("index",index); startActivity(intent); } } }
package com.example.demofragment; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ScrollView; import android.widget.TextView; public class DetailFragment extends Fragment { public static DetailFragment newInstance(int index){ DetailFragment f = new DetailFragment(); Bundle bundle = new Bundle(); bundle.putInt("index", index); f.setArguments(bundle); return f; } public int getShownIndex(){ return getArguments().getInt("index",0); } public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceSate){ if(container == null){ return null; } ScrollView scroller = new ScrollView(getActivity()); TextView text = new TextView(getActivity()); text.setPadding(10, 10, 10, 10); text.setTextSize(30); scroller.addView(text); text.setText(Data.DETAIL[getShownIndex()]); return scroller; } }
package com.example.demofragment; import android.os.Bundle; import android.app.Activity; import android.content.res.Configuration; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public static class DetailActivity extends Activity { protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //判断是否为横屏,如果为横屏,则结束当前Activity,准备使用Fragment显示详细内容 if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){ finish(); return; } if(savedInstanceState == null){ DetailFragment details = new DetailFragment(); details.setArguments(getIntent().getExtras()); getFragmentManager().beginTransaction().add(android.R.id.content, details).commit(); } } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demofragment" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.demofragment.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <span style="color:#ff0000;"><activity android:name="com.example.demofragment.MainActivity$DetailActivity" android:label="详细内容"/></span> </application> </manifest>