使用ListView的Fragment
1.使用ListFragment可实现包含Listiew的Fragment
2.在Fragment的布局中必须包含id"@android:id/list"(在代码中则是list)的Listview
3.只能使用SimpleAdapter或者SimpleCursorAdapter作为适配器
4.直接使用ListFragment的setListAdapter()来设置适配器【不要给ListView设置】
5.ListView点击事件的监听,重写ListFragment的onListItemClick(ListView l, View v, int position, long id)方法
1)新建ListFragment的布局fragment1.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Titles"
/>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
2)新建Fragment继承自ListFragment,命名为Fragment1。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
/**
* @描述 在Fragment中要使用ListView,就要用ListFragment
* */
publicclass Fragment1 extends ListFragment {
private String TAG = Fragment1.class.getName();
private ListView list ;
private SimpleAdapter adapter;
/**
* @描述 在onCreateView中加载布局
* */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container,false);
list = (ListView) view.findViewById(android.R.id.list);
Log.i(TAG, "--------onCreateView");
return view;
}
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
b = savedInstanceState;
Log.i(TAG, "--------onCreate");
String[] list = {"Class 1","Class 2","class 3","Class 4","Class 5"};
adapter = new SimpleAdapter(getActivity(), getData(list), R.layout.item_list, new String[]{"title"}, newint[]{R.id.title});
setListAdapter(adapter);
}
@Override
publicvoid onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
System.out.println(l.getChildAt(position));
HashMap<String, Object> view= (HashMap<String, Object>) l.getItemAtPosition(position);
System.out.println(view.get("title").toString()+"+++++++++title");
Toast.makeText(getActivity(), TAG+l.getItemIdAtPosition(position), Toast.LENGTH_LONG).show();
System.out.println(v);
System.out.println(position);
}
private List<? extends Map<String, ?>> getData(String[] strs) {
List<Map<String ,Object>> list = new ArrayList<Map<String,Object>>();
for (int i = 0; i < strs.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", strs[i]);
list.add(map);
}
return list;
}
@Override
publicvoid onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "--------onActivityCreated");
}
@Override
publicvoid onAttach(Activity activity) {
super.onAttach(activity);
Log.i(TAG, "----------onAttach");
}
}
3)新建Activity,继承自FragmentActivity,将Fragment添加到Activity中。命名为FragmentsActivity。
publicclass FragmentsActivity extends FragmentActivity {
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragments);
Display display = getWindowManager().getDefaultDisplay();
Fragment1 f1 = new Fragment1();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().add(R.id.fragments, f1).commit();
}
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fragments, menu);
returntrue;
}
}
效果图: