首页 > os > android > > 正文

Android ListView列表分组开发

发布人:zhoulujun@live.cn    点击:

Android ListView列表分组开发

代码如下               http://zhoulujun.qiniudn.com/
ListGroupExpandableListView.zip

package cn.zhoulujun;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class ListGroupExpandableListView extends ExpandableListActivity {
    private String[] group=new String[]{"同学","同事","家人"};
    private String[][] groupDetail=new String[][]{
            {"罗志彪","刘宋","刘定飞","刘丹"},
            {"潘光浩","梁建坤","张晓峰","张无忌"},
            {"周静","李世婕","李世民","李元芳"}
    };

    ExpandableListView expandableListView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.list_view);
        expandableListView=(ExpandableListView)findViewById(R.id.expandListView);
        ExpandableListAdapter adapter=new ListAdpater(this,group,groupDetail);
        expandableListView.setAdapter(adapter);
        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {

            }
        });
        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {

            }
        });
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(ListGroupExpandableListView.this,group[groupPosition]+":"+groupDetail[groupPosition][childPosition],Toast.LENGTH_SHORT).show();
                return false;
            }
        });


    }




}

package cn.zhoulujun;  import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ImageView; import android.widget.TextView;  /**  * Created by lujun on 2015-07-21.  */ public class ListAdpater extends BaseExpandableListAdapter { private String[] groupName;  private String[][] groupDetail;  private Context context;  LayoutInflater inflater;   public ListAdpater(Context context, String[] groupName, String[][] groupDetail) { this.groupName = groupName;  this.groupDetail = groupDetail;  this.context = context;  inflater=LayoutInflater.from(context);  } @Override  public int getGroupCount() { return groupName.length;  } @Override  public int getChildrenCount(int groupPosition) { return groupDetail[groupPosition].length;  } @Override  public Object getGroup(int groupPosition) { return groupDetail[groupPosition];  } @Override  public Object getChild(int groupPosition, int childPosition) { return groupDetail[groupPosition][childPosition];  } @Override  public long getGroupId(int groupPosition) { return groupPosition;  } @Override  public long getChildId(int groupPosition, int childPosition) { return childPosition;  } @Override  public boolean hasStableIds() { return true;  } @Override  public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { convertView=inflater.inflate(R.layout.list_group_item,null);  TextView groupItemName=(TextView)convertView.findViewById(R.id.listGroupName);  groupItemName.setText(getGroup(groupPosition).toString());  TextView groupNum=(TextView)convertView.findViewById(R.id.listGroupNum);  groupNum.setText(getChildrenCount(groupPosition));  ImageView imageView=(ImageView)convertView.findViewById(R.id.listGroupImage);  imageView.setImageResource(R.drawable.arrow_down);  if(!isExpanded){ imageView.setImageResource(R.drawable.arrow_up);  } return convertView;  } @Override  public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { convertView=inflater.inflate(R.layout.list_child_item,null);  TextView nickTextView=(TextView)convertView.findViewById(R.id.listViewNick);  nickTextView.setText(getChild(groupPosition,childPosition).toString());  return convertView;  } @Override  public boolean isChildSelectable(int groupPosition, int childPosition) { return true;  } } 
xml version="1.0" encoding="utf-8"?> xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:id="@+id/ListGroupItem"  android:layout_width="match_parent"  android:layout_height="match_parent">   android:id="@+id/listGroupImage"  android:layout_width="30dp"  android:layout_height="40dp"  android:layout_alignParentLeft="true"  android:layout_centerVertical="true"  android:layout_marginLeft="10dp"  android:src="@drawable/arrow_up"  android:backgroundTintMode="src_in"/>   android:id="@+id/listGroupName"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_centerVertical="true"  android:layout_alignBaseline="@id/listGroupImage"  android:layout_toRightOf="@+id/listGroupImage"  android:textSize="24sp"  android:text="分组名称"/>   android:id="@+id/listGroupNum"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_alignParentRight="true"  android:layout_alignBottom="@id/listGroupImage"  android:layout_centerVertical="true"  android:layout_marginRight="10dp"  android:text="[10/10]"/>   
xml version="1.0" encoding="utf-8"?> xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="match_parent"  android:layout_height="40dp">   android:id="@+id/listChildAvatar"  android:layout_width="40dp"  android:layout_height="40dp"  android:layout_marginLeft="10dp"  android:layout_alignParentTop="true"  android:layout_alignParentLeft="true"  android:src="@drawable/avatar"  />   android:id="@+id/listViewNick"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_alignParentTop="true"  android:layout_toRightOf="@+id/listChildAvatar"  android:layout_marginLeft="10dp"  android:text="昵称"  android:textSize="16sp"/>   android:id="@+id/listViewTrends"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_alignLeft="@+id/listViewNick"  android:layout_alignParentBottom="true"  android:text="该好友没有最新动态"  android:textSize="12sp"/>