ListFragment就是一个包含ListView的Fragment,它可以通过数据源(数组或游标)显示一系列的信息。
MainActivity.java代码:
public class MainActivity extends FragmentActivity {
private Button button;
private FragmentManager manager;
private FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*获取按钮*/
button = (Button) this.findViewById(R.id.button1);
/*获取manager*/
manager = this.getSupportFragmentManager();
/*设置按钮的监听事件*/
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*创建事物*/
transaction = manager.beginTransaction();
/*创建ArticleListFragment*/
ArticleListFragment fragment = new ArticleListFragment();
/*添加*/
transaction.add(R.id.center, fragment, "article");
/*提交事物*/
transaction.commit();
}
});
}
}
ArticleListFragment.java代码:
public class ArticleListFragment extends ListFragment {
private ArrayAdapter<String> adapter;
private FragmentManager manager;
private FragmentTransaction transaction;
public ArticleListFragment() {
// TODO Auto-generated constructor stub
}
/**
* 这个方法适合做一些数据初始化的工作,如连接数据库, 这段代码中setListAdapter(adapter);之所以写在这里 是因为我们现在这个类没有用到布局(onCreateView在这个项目中没用到)
* 如果这个项目中我们用到了动态布局,setListAdapter必须写在onCreateView中 因为只有在onCreateView方法中视图才加载,写在onCreate方法中是错的,因为这个方法 还没有加载完我们定义的视图
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 创建一个数据集合 */
List<String> data = new ArrayList<String>();
/* 遍历往集合中添加数据 */
for (int i = 0; i < 10; i++) {
data.add("廖泽民" + i);
}
/* 创建一个ArrayAdapter集合 */
adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, data);
/* 设置适配器 */
setListAdapter(adapter);
/* 获取manager */
manager = this.getFragmentManager();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
/**
* ListFragment提供了一个非常方便的方法onListItemClick 我们可以对ListView中的条目进行操作
*/
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
/* 获取条目 */
String item = adapter.getItem(position);
/* 创建事物 */
transaction = manager.beginTransaction();
/* 创建DetailFragment(注:这里我们可以直接new一个DetailFragment对象,因为我们之前没有通过事物将其添加到某个布局中,所以可以直接new) */
DetailFragment fragment = new DetailFragment();
/* 创建Bundle并添加数据 */
Bundle args = new Bundle();
args.putString("item", item);
/* 为fragment设置数据 */
fragment.setArguments(args);
/* 替换 */
transaction.replace(R.id.right, fragment, "rightFragment");
/* 把当前fragment添加到回退栈 */
transaction.addToBackStack("rightFragment");
/* 提交事物 */
transaction.commit();
}
}
DetailFragment.java代码:
public class DetailFragment extends Fragment {
public DetailFragment() {
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/*动态获取布局*/
View view = inflater.inflate(R.layout.detail, null);
/*从动态布局中获取TextView*/
TextView textView = (TextView) view.findViewById(R.id.textView1);
/*从ArticleListFragment中获取传递过来的参数*/
Bundle bundle = this.getArguments();
textView.setText(bundle.getString("item"));
return view;
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/left"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#CCCCCC"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show" />
</LinearLayout>
<LinearLayout
android:id="@+id/center"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#CCDDFF"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/right"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#CCFFDD"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
detail.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="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp" />
</LinearLayout>
demo结果展示: