Android Fragment的使用 八 ListFragment
来源:互联网 发布:淘宝下载ipad版5.1.1 编辑:程序博客网 时间:2024/05/17 20:49
ListFragment是封装了Fragment和ListView,方便Fragment快速形成ListView,和之前的DialogFragment一样,使得ListView变成Fragment有着自己生命周期,能在更多的情况下使用ListView,也使得Fragment能够做出列表这样常见的样式。
使用方法很简单,和ListView一样,上代码:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}activity_main<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" > <fragment android:name="android.com.listfragment.ListFragmentImpl" android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" /> <fragment android:name="android.com.listfragment.ListFragmentSelf" android:id="@+id/fragment2" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>public class ListFragmentImpl extends ListFragment{ private static final String TAG = "ListFragmentImpl"; private ListView selfList; String[] cities = { "Shenzhen", "Beijing", "Shanghai", "Guangzhou", "Wuhan", "Tianjing", "Changsha", "Xi'an", "Chongqing", "Guilin", }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(TAG, "onCreateView"); return inflater.inflate(R.layout.list_fragment_impl, container, false); } @Override public void onCreate(Bundle savedInstanceState) { Log.d(TAG, "onCreate"); super.onCreate(savedInstanceState); // 设置ListFragment默认的ListView,即@id/android:list this.setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, cities)); } public void onListItemClick(ListView parent, View v, int position, long id) { Log.d(TAG, "onListItemClick"); Toast.makeText(getActivity(), "You have selected " + cities[position], Toast.LENGTH_SHORT).show(); }}list_fragment_impl<?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" > <!-- ListFragment对应的android:id值固定为"@id/android:list" --> <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:drawSelectorOnTop="false" /></LinearLayout>public class ListFragmentSelf extends ListFragment{ private static final String TAG = "ListFragmentImpl"; private ListView selfList; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(TAG, "onCreateView"); return inflater.inflate(R.layout.list_fragment_self, container, false); } @Override public void onCreate(Bundle savedInstanceState) { final String[] from = new String[] {"title", "info"}; final int[] to = new int[] {R.id.text1, R.id.text2}; Log.d(TAG, "onCreate"); super.onCreate(savedInstanceState); // 建立SimpleAdapter,将from和to对应起来 SimpleAdapter adapter = new SimpleAdapter( this.getActivity(), getSimpleData(), R.layout.two_textview, from, to); this.setListAdapter(adapter); } public void onListItemClick(ListView parent, View v, int position, long id) { Log.d(TAG, "onListItemClick"); Toast.makeText(getActivity(), "You have selected " + position, Toast.LENGTH_SHORT).show(); } private List<Map<String, Object>> getSimpleData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "Ferris wheel"); map.put("info", "Suzhou Ferris wheel"); list.add(map); map = new HashMap<String, Object>(); map.put("title", "Flower"); map.put("info", "Roser"); list.add(map); map = new HashMap<String, Object>(); map.put("title", "Disk"); map.put("info", "Song Disk"); list.add(map); return list; }}list_fragment_self<?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" > <!-- ListFragment对应的android:id值固定为"@id/android:list" --> <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:drawSelectorOnTop="false" /></LinearLayout>two_textview<?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/text1" android:textSize="12sp" android:textStyle="bold" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/text2" android:textSize="24sp" android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout>
下一次是一个综合的例子,说的是Fragment之间的数据传输。
再见
0 0
- Android Fragment的使用 八 ListFragment
- Android系列之Fragment----ListFragment的使用
- Android Fragmnet-Fragment数据交换以及ListFragment的使用
- Fragment学习之ListFragment的使用
- Fragment初学4——使用Fragment的子类ListFragment
- android----fragment学习中的listFragment
- [Android]Fragment应用/ListFragment/DialogFragment
- Android之Fragment-ListFragment的实践(三)
- Android基础总结八:Fragment的使用
- Android ListFragment使用
- ListFragment的使用
- ListFragment的使用
- ListFragment的使用
- ListFragment的使用
- ListFragment的使用
- android学习八(android碎片fragment的使用)
- Android Fragment学习笔记(2) ----使用ListFragment显示列表(上)
- Android开发系列——listfragment的使用例子
- YUV格式解析
- 51Nod-1274-最长递增路径
- java并发编程:线程安全性
- 应聘时最漂亮的回答
- 深度剖析空间配置器(二)一二级配置器
- Android Fragment的使用 八 ListFragment
- 浏览器的渲染机制
- 修改identity语句
- eclipse备忘录
- 开门测试
- Android 动态壁纸开发(时钟)
- MySQL中的触发机器
- 推荐系统算法
- vue