Android成长之路-BaseExpandableListAdapter的用法

来源:互联网 发布:cad珠宝设计软件 编辑:程序博客网 时间:2024/05/17 02:56

BaseExpandableListAdapter是ExpandableListAdapter的抽象基类,从一些数据中提供数据和视图给可折叠列表视图。

例子详解:

首先定义一个xml布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ExpandableListView   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:id="@+id/ecpandable"  
  11.         />  
  12.   
  13. </LinearLayout>  

Adapter:


public class TransExpandableListAdapter implements ExpandableListAdapter {
    private Context mContext;

    /**
     * ----------定义数组--------------------------------------------------
     */
    private int[] images = new int[]{
            R.drawable.ic_launcher,
            R.drawable.demo_1,
            R.drawable.demo_2
    };
    private String[] armTypes = new String[]{
            "神族", "虫族", "人族"
    };
    private String[][] arms = new String[][]{
            {"狂战士", "龙骑士", "黑暗圣堂"},
            {"小狗", "飞龙", "自爆妃子"},
            {"步兵", "伞兵", "护士mm"}
    };

    public TransExpandableListAdapter(Context context) {
        this.mContext = context;

    }


@Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getGroupCount() {
        return armTypes.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return arms[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return armTypes[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return arms[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) {
        TextView textView = getTextView();//调用定义的getTextView()方法
        textView.setText(getGroup(groupPosition).toString());//添加数据
        return textView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        LinearLayout ll = new LinearLayout(mContext);
        ll.setOrientation(LinearLayout.VERTICAL);//定义为纵向排列
        ImageView logo = new ImageView(mContext);
        logo.setImageResource(images[groupPosition]);//添加图片
        ll.addView(logo);
        TextView textView = getTextView();//调用定义的getTextView()方法
        textView.setText(getChild(groupPosition, childPosition).toString());//添加数据
        ll.addView(textView);
        return ll;
    }

 @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

@Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }

    //定义一个TextView
    private TextView getTextView() {
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 40);
        TextView textView = new TextView(mContext);
        textView.setLayoutParams(lp);
        textView.setPadding(50, 0, 0, 0);
        textView.setTextSize(16);
        return textView;
    }

}


Activity中:

 private TransExpandableListView mListView;
    private ExpandableListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trans_expandable_guo);
        initView();
    }

    private void initView() {
        mListView = (TransExpandableListView) findViewById(R.id.trans_expandable_listview);
        mAdapter = new TransExpandableListAdapter(this);
        mListView.setAdapter(mAdapter);

    }


效果图:



原创粉丝点击