這篇文章將為大家詳細(xì)講解有關(guān)ExpandableListView如何實(shí)現(xiàn)手風(fēng)琴效果,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

具體內(nèi)容如下
1. 效果示例圖



2. 創(chuàng)建方法
(1)第一種方法與ListView等普通控件一樣,直接在布局文件中添加ExpandableListView控件即可。
(2)第二種方法則是創(chuàng)建一個Activity繼承自ExpandableListActivity,而后通過getExpandableListView()方法可獲得一個ExpandableListView對象。
第二種方法僅適用于一個頁面中只有一個ExpandableListView的情況。繼承的Activity不需要再調(diào)用setContentView()方法,在ExpandableListActivity中已經(jīng)關(guān)聯(lián)了一個系統(tǒng)定義的布局文件。
3. 部分屬性和點(diǎn)擊事件
android:groupIndicator、android:childIndicator:組條目和子條目前面的圖標(biāo),默認(rèn)值為箭頭,可設(shè)置自定義圖片資源。若不顯示該圖標(biāo),則設(shè)置為@null。
android:divider、android:childDivider:組和子條目的分隔線。
ExpandableListView的點(diǎn)擊事件有兩個,分別對應(yīng)組和子條目的點(diǎn)擊事件:
設(shè)置組的點(diǎn)擊事件:setOnGroupClickListener(OnGroupClickListener listener)
設(shè)置子條目的點(diǎn)擊事件:setOnChildClickListener(OnChildClickListener listener)
5. 適配器
根據(jù)數(shù)據(jù)源的不同,可使用的適配器有兩個:BaseExpandableListAdapter和CursorTreeAdapter,其中,CursorTreeAdapter用于數(shù)據(jù)源為Cursor對象的情況下,其它情況則使用BaseExpandableListAdapter。
(1)BaseExpandableListAdapter需要重寫的方法:
getGroup():從數(shù)據(jù)源中獲取組的數(shù)據(jù)內(nèi)容。
getGroupCount():獲取組的總數(shù)。
getGroupId():獲取組的ID。
getGroupView():獲取組的視圖。
getChild():從數(shù)據(jù)源中獲取子條目的內(nèi)容。
getChildCount():獲取指定組中的子條目總數(shù),并非全部的子條目。
getChildId():獲取子條目的ID。
getChildView():獲取子條目的視圖
hasStableIds():判斷id對應(yīng)的條目是否已經(jīng)繪制,用于優(yōu)化列表。
isChildSelectable():子條目是否允許點(diǎn)擊,若返回false,則子條目點(diǎn)擊事件無效。
(2)CursorTreeAdapter需要重寫的方法:
CursorTreeAdapter():構(gòu)造方法傳入組的Cursor對象。
getChildrenCursor():傳入組的Cursor對象,獲取相應(yīng)的組的子條目的Cursor對象。
newGroupView():創(chuàng)建組的視圖,返回一個新的視圖。
bindGroupView():在這里綁定組視圖的數(shù)據(jù)內(nèi)容,第一個參數(shù)即newGroupView()方法的返回值。
newChildView():創(chuàng)建子條目的視圖。
bindChildView():綁定子條目視圖的數(shù)據(jù)內(nèi)容。
6. 簡單范例
實(shí)現(xiàn)效果圖中的例子。
布局:
<?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" tools:context="com.studying.expandablelistviewdemo.MainActivity"> <ExpandableListView android:id="@+id/elv_local_data" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
Activity:
public class MainActivity extends Activity {
  private ExpandableListView elv;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    elv = (ExpandableListView) findViewById(R.id.elv_local_data);
    MyBaseExpandableListAdapter adapter = new MyBaseExpandableListAdapter(this, LoadData.getGroupData(), LoadData.getChildData());
    elv.setAdapter(adapter);
  }
}加載測試數(shù)據(jù)用的工具類:
public class LoadData {
  // 組的數(shù)據(jù)內(nèi)容
  public static List<String> getGroupData() {
    List<String> groupDataList = new ArrayList<>();
    groupDataList.add("計(jì)算機(jī)基礎(chǔ)");
    groupDataList.add("安卓開發(fā)");
    return groupDataList;
  }
  // 子條目的數(shù)據(jù)內(nèi)容
  public static List<List<String>> getChildData() {
    List<List<String>> childDataList = new ArrayList<>();
    List<String> group1 = new ArrayList<>();
    group1.add("數(shù)據(jù)結(jié)構(gòu)");
    group1.add("算法");
    group1.add("計(jì)算機(jī)網(wǎng)絡(luò)");
    childDataList.add(group1);
    List<String> group2 = new ArrayList<>();
    group2.add("控件使用");
    group2.add("網(wǎng)絡(luò)操作");
    group2.add("數(shù)據(jù)存儲");
    group2.add("四大組件");
    childDataList.add(group2);
    return childDataList;
  }
}適配器:
public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {
  private Context mContext;
  private List<String> groupName;
  private List<List<String>> childName;
  public MyBaseExpandableListAdapter(Context mContext, List<String> groupName, List<List<String>> childName) {
    this.mContext = mContext;
    this.groupName = groupName;
    this.childName = childName;
  }
  @Override
  public int getGroupCount() {
    return groupName.size();
  }
  @Override
  public long getGroupId(int groupPosition) {
    return groupPosition;
  }
  @Override
  public String getGroup(int groupPosition) {
    return groupName.get(groupPosition);
  }
  @Override
  public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
  
    convertView = View.inflate(mContext, R.layout.item_group_name, null);
    TextView groupName = (TextView) convertView.findViewById(R.id.group_name);
    groupName.setText(getGroup(groupPosition));
    return convertView;
  }
  @Override
  public int getChildrenCount(int groupPosition) {
    return childName.get(groupPosition).size();
  }
  @Override
  public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
  }
  @Override
  public String getChild(int groupPosition, int childPosition) {
    return childName.get(groupPosition).get(childPosition);
  }
  @Override
  public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
  
    convertView = View.inflate(mContext, R.layout.item_child_name, null);
    TextView childName = (TextView) convertView.findViewById(R.id.child_name);
    childName.setText(getChild(groupPosition, childPosition));
    return convertView;
  }
  @Override
  public boolean hasStableIds() {
    return false;
  }
  @Override
  public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
  }
}關(guān)于“ExpandableListView如何實(shí)現(xiàn)手風(fēng)琴效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
                網(wǎng)頁名稱:ExpandableListView如何實(shí)現(xiàn)手風(fēng)琴效果-創(chuàng)新互聯(lián)
                
                URL鏈接:http://chinadenli.net/article14/pgide.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、App開發(fā)、移動網(wǎng)站建設(shè)、建站公司、網(wǎng)站設(shè)計(jì)公司、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容