关于ListFragment的基本写法及注意事项
来源:互联网 发布:58速运 知乎 编辑:程序博客网 时间:2024/05/17 06:43
关于ListFragment的基本写法及注意事项
ListFragment是Fragment的子类,一般我们用ListFragment的时候都是先继承ListFragment重写我们自己的Fragment1、Fragment2。
重写ListFragment的时候特别注意,它有两个重要的方法:
1、onCreateView();
2、onCreate();
格式代码:(以下是代码实例)
我们要写的是一个这样的例子,如下图
首先我们需要写一个总的activity界面,来包含两个ListFragment(Fragment1、Fragment2)
Fragment1里的ListView的ListItem布局用的是系统自带,Fragment2用的是自定义的,自定义代码custom.xml在下面有附上
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><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="com.example.administrator.listfragment.MainActivity"> <fragment android:name="com.example.administrator.listfragment.Fragment1" android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent"> </fragment> <fragment android:name="com.example.administrator.listfragment.Fragment2" android:id="@+id/fragment2" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent"> </fragment></LinearLayout>
接着就是Fragment1、Fragment2的java代码和xml代码了,
Fragment1.java:
package com.example.administrator.listfragment;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.ArrayAdapter;import android.widget.ListView;/** * Created by Administrator on 2016/3/2. */public class Fragment1 extends ListFragment{ private String[] cities = {"北京", "上海", "广州", "深圳","北京", "上海", "广州", "深圳","北京", "上海", "广州", "深圳","北京", "上海", "广州", "深圳","北京", "上海", "广州", "深圳"}; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1,container); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1, cities)); } @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Log.i("tag","左边条目!!!!!!!!!"); }}
fragment1.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@id/android:list"> </ListView></LinearLayout>
Fragment2.java:
package com.example.administrator.listfragment;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;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * Created by Administrator on 2016/3/2. */public class Fragment2 extends ListFragment{ private String[] names = {"name","sex"}; private int[] id = {R.id.text, R.id.image}; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2,container); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setListAdapter(new SimpleAdapter(getActivity(),getdatas(),R.layout.custom,names,id)); } @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Log.i("tag", "!!!!!!!!!!右边条目"); } private List<Map<String, Object>> getdatas() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<>(); map.put("name", "aa"); map.put("sex", R.mipmap.ic_launcher); list.add(map); map = new HashMap<>(); map.put("name", "bb"); map.put("sex", R.mipmap.ic_launcher); list.add(map); map = new HashMap<>(); map.put("name", "cc"); map.put("sex", R.mipmap.ic_launcher); list.add(map); map = new HashMap<>(); map.put("name", "dd"); map.put("sex", R.mipmap.ic_launcher); list.add(map); return list; }}
fragment2.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@id/android:list"> </ListView></LinearLayout>
custom.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp"/> <ImageView android:id="@+id/image" android:layout_height="30dp" android:layout_width="30dp"/></LinearLayout>
总的来说代码大概就是这样了,需要注意的是
1、ListView的命名必须是android:id=”@id/android:list”一定要注意,不能更改
2、onCreateView方法是用来加载listview布局
3、onCreate方法是用来加载listview的listitem布局和数据 和 设置适配器
4、onCreate方法中的this表示默认的android:id的listview , this.setListAdapter也是默认的
5、注意ArrayAdapter和SimpleAdapter的使用和区别
ArrayAdapter和SimpleAdapter代码块:
1、new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1, cities);private String[] cities = {"北京", "上海", "广州", "深圳","北京", "上海", "广州", "深圳","北京", "上海", "广州", "深圳","北京", "上海", "广州", "深圳","北京", "上海", "广州", "深圳"};2、new SimpleAdapter(getActivity(),getdatas(),R.layout.custom,names,id);private String[] names = {"name","sex"};private int[] id = {R.id.text, R.id.image};private List<Map<String, Object>> getdatas() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<>(); map.put("name", "aa"); map.put("sex", R.mipmap.ic_launcher); list.add(map); map = new HashMap<>(); map.put("name", "bb"); map.put("sex", R.mipmap.ic_launcher); list.add(map); map = new HashMap<>(); map.put("name", "cc"); map.put("sex", R.mipmap.ic_launcher); list.add(map); map = new HashMap<>(); map.put("name", "dd"); map.put("sex", R.mipmap.ic_launcher); list.add(map); return list; }
0 0
- 关于ListFragment的基本写法及注意事项
- 标准tableview的写法及注意事项
- ListFragment使用注意事项
- 关于ActionBar Tab与ListFragment的结合
- 出现频率最高的笔试题strcpy写法及注意事项
- oracle存储过程的基本及注意事项
- xml 的基本格式 及 注意事项
- 关于listfragment中使用listview的点击事件不响应
- 关于css及安全配置的注意事项
- 关于泛型类的作用及注意事项
- 关于CardView的使用及注意事项
- 关于CDH5安装的步骤及注意事项
- plsql程序的基本写法及三种循环
- ListFragment setDivider的问题
- ListFragment的使用
- ListFragment的用法
- ListFragment的使用
- ListFragment的传值
- 未能找到元数据文件”,在重新生成项目,并在其他项目中引用时出现
- Thrift入门及Java实例(一)
- R projects at Google Summer of Code 2016
- javascript:void(0)的含义理解
- Android内存优化三:对Bitmap的内存优化
- 关于ListFragment的基本写法及注意事项
- bananapi之DSI
- 将一个数表示成任意两个数的平方和
- Android内存优化四:从代码角度进行优化
- unix 各种返回信号详解
- 完美解决eclipse时创建新项目时出错问题
- 1088. Rational Arithmetic (20)
- Android内存优化五:Android垃圾回收器机制
- 二层交换机、三层交换机和路由器的原理及区别