其實對於分組的ListView和我們平時用的ListView沒有多大差別,就是需要在適配器中的getView方法中做下判斷。只要理解了這個,下面就好說了,下面我們看下實現代碼。
首先是main.xml佈局:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#ffffff"
- android:orientation="vertical" >
- <ListView
- android:id="@+id/listView_list"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- </ListView>
- </LinearLayout>
因爲listview要加載兩種不同的item,所以要實現兩個item佈局,addexam_list_item.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:padding="5dip">
- <ImageView
- android:id="@+id/addexam_list_icon"
- android:background="@drawable/ic_launcher"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <TextView
- android:id="@+id/addexam_list_item_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_marginLeft="10dp"
- android:text="測試數據"/>
- </LinearLayout>
分組標籤對應的佈局addexam_list_item_tag.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#666666"
- android:paddingLeft="10dp"
- android:gravity="center_vertical"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/addexam_list_item_text"
- android:layout_width="wrap_content"
- android:layout_height="20dip"
- android:textColor="#ffffff"
- android:text="金融考試"
- android:gravity="center_vertical"/>
- </LinearLayout>
佈局文件我們已經實現了,下面看下在程序中我們是怎麼處理的吧!
- public class TestActivity extends Activity {
- /** Called when the activity is first created. */
- private List<String> list=null;
- private List<String> groupkey=new ArrayList<String>();
- private List<String> aList = new ArrayList<String>();
- private List<String> bList = new ArrayList<String>();
- private ListView listview;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- listview=(ListView) findViewById(R.id.listView_list);
- initData();
- MyAdapter adapter=new MyAdapter();
- listview.setAdapter(adapter);
- }
- public void initData(){
- list = new ArrayList<String>();
- groupkey.add("A組");
- groupkey.add("B組");
- for(int i=0; i<5; i++){
- aList.add("A組"+i);
- }
- list.add("A組");
- list.addAll(aList);
- for(int i=0; i<8; i++){
- bList.add("B組"+i);
- }
- list.add("B組");
- list.addAll(bList);
- }
- private class MyAdapter extends BaseAdapter{
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return list.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return list.get(position);
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public boolean isEnabled(int position) {
- // TODO Auto-generated method stub
- if(groupkey.contains(getItem(position))){
- return false;
- }
- return super.isEnabled(position);
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- View view=convertView;
- if(groupkey.contains(getItem(position))){
- view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item_tag, null);
- }else{
- view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item, null);
- }
- TextView text=(TextView) view.findViewById(R.id.addexam_list_item_text);
- text.setText((CharSequence) getItem(position));
- return view;
- }
- }
- }
代碼好像挺簡單,更我們平時使用lsitview也沒多大區別,下面看看能不能實現呢
運行一下: