【安卓笔记】使用ListView的Fragment
API LEVEL 11
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
<?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: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 * */ public class 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 public void 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"}, new int[]{R.id.title}); setListAdapter(adapter); } @Override public void 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 public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log.i(TAG, "--------onActivityCreated"); } @Override public void onAttach(Activity activity) { super.onAttach(activity); Log.i(TAG, "----------onAttach"); } }
3)新建Activity,继承自FragmentActivity,将Fragment添加到Activity中。命名为FragmentsActivity。
public class FragmentsActivity extends FragmentActivity { @Override protected void 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 public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.fragments, menu); return true; } }
效果图:
相关推荐
chenjinlong 2020-02-19
83580494 2013-07-19
langjiao 2013-07-16
kiduo0 2013-07-10
gongzhiyao0 2010-11-15
bigdatazx 2010-11-05
Urchindong 2011-08-01
peixiaopao 2011-08-21
MeOrdinary 2014-05-13
magic00 2019-10-21
guizhongyun 2011-09-27
csuhanshuai 2015-03-30
Sunanang 2015-03-30
snailbing 2015-04-23
huohu00 2015-04-22
toperfect 2015-07-01
nickey 2012-01-29
Rgenxiao 2012-01-26
满城风絮 2011-12-03