关于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;
}