实现的功能很简单,也是最基本的,上下分别是两个Fragment,上面的Fragment中是一个listview,当点击item时,下面的Fragment显示对应的文字详细信息
具体的实现步骤如下:
①创建工程FragmentExam,目录视图如下(把之前的FragmentPreference的demo也加到了一起):
②main.xml文件布局,垂直方向上两个Fragment,用<Fragment>标签声明
<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"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="#7ecef4">
<fragment
android:name="com.example.fragementexam.FragementList"
android:id="@+id/frag_list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=""/>
<fragment
android:name="com.example.fragementexam.FragementDetails"
android:id="@+id/frag_detail"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=""/>
</LinearLayout>
③FragmentList.java的代码,它继承了ListFragment,注意在onCreateView方法中使用inflater的inflate方法将布局页面引进:
package com.example.fragementexam; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class FragementList extends ListFragment {
private String[] values = new String[] { "侏儒", "人类", "暗夜精灵", "矮人", "德莱尼",
"狼人" };
private int[] images = new int[] { R.drawable.gnome,
R.drawable.human, R.drawable.nightelf,
R.drawable.dwarf, R.drawable.draenei,
R.drawable.werewolf }; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { return inflater.inflate(R.layout.frag_list, container, false);
} @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = ; i < values.length; i++) {
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("values", values);
listItem.put("images", images);
listItems.add(listItem);
}
SimpleAdapter adapter = new SimpleAdapter(getActivity(), listItems,
R.layout.list_item, new String[] { "values", "images" },
new int[] { R.id.txt_title, R.id.img });
setListAdapter(adapter); } @Override
public void onListItemClick(ListView l, View v, int position, long id) {
// String item = (String) getListAdapter().getItem(position);
FragementDetails frag = (FragementDetails) getFragmentManager()
.findFragmentById(R.id.frag_detail);
if (frag != null && frag.isInLayout()) {
switch (position) {
case :
frag.setText(getString(R.string.Gnome));
break;
case :
frag.setText(getString(R.string.Human));
break;
case :
frag.setText(getString(R.string.NightElf));
break;
case :
frag.setText(getString(R.string.Dwarf));
break;
case :
frag.setText(getString(R.string.Draenei));
break;
case :
frag.setText(getString(R.string.Werewolf));
break;
}
} Log.i("PDA", "position = " + position);
} }
④FragementDetails.java的代码,这个比较简单,里面有一个设置TextView内容的方法,其布局页面也仅仅是一个TextView
package com.example.fragementexam; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class FragementDetails extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.frag_detail, container,false);
} public void setText(String item){
TextView txt = (TextView) getView().findViewById(R.id.txt_detail);
txt.setText(item);
} }
其他的部分就是一些数组,String的定义了,这个demo虽然简单,却将Android Fragment方面常用到的做了一个综述,如果自己写明白了这个的话,今后遇到类似的项目应该要好应付的多