ExpandableListView(手風琴)(1)---BaseExpandableListAdapter

  • 基礎屬性
    groupIndicator 組左側的箭頭
  • 事件

適配器處理

  • 自定義抽象適配器繼承BaseExpandableListAdapter 重寫構造方法
//此處爲public便於子類使用 獲取控件
public  LayoutInflater mInflater;
     Context mContext;
     List<String> group;
     List<List<T>> child;
public  SJXBaseAdapter(Context context){
        mContext=context;
        mInflater=LayoutInflater.from(context);
}

添加數據方法

public void addData(List<String> group,List<List<T>> child){
        this.group=group;
        this.child=child;
}

兩個抽象方法在getView中調用

public abstract View getGrougView(int groupPosition,View convertView);
public abstract View getChildView(int groupPosition,int childPosition,View convertView);
@Override
    public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
        return getGrougView(arg0, arg2);
}
@Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,ViewGroup arg4) {
    return getChildView(arg0, arg1, arg3);
}
  • 自定義適配器繼承上面的適配器
    繼承上述適配器 重寫 構造方法 抽象方法
public LocalAdapter(Context context) {
        super(context);
    }

@Override
    public View getChildView(int groupPosition, int childPosition,
            View convertView) {
        View childView=mInflater.from(context).inflate(佈局文件id, null);
        TextView txtChild=(TextView) childView.findViewById(R.id.txt_child);
        txtChild.setText(child.get(groupPosition).get(childPosition));
        txtChild.setPadding(0, 0, 0, 0);
        return childView;
    }
  • 適配器關聯activity
LocalAdapter ap=new LocalAdapter(this);
ap.addData(list<>,list<list<String>>);
activity.setAdapter(ap);
  • 補充
    適配器中getchild方法可以返回cursor
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章