溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

ExpandableListView如何實(shí)現(xiàn)手風(fēng)琴效果

發(fā)布時(shí)間:2021-06-30 10:50:49 來(lái)源:億速云 閱讀:179 作者:小新 欄目:移動(dòng)開(kāi)發(fā)

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

具體內(nèi)容如下

1. 效果示例圖

ExpandableListView如何實(shí)現(xiàn)手風(fēng)琴效果

ExpandableListView如何實(shí)現(xiàn)手風(fēng)琴效果

ExpandableListView如何實(shí)現(xiàn)手風(fēng)琴效果

2. 創(chuàng)建方法

(1)第一種方法與ListView等普通控件一樣,直接在布局文件中添加ExpandableListView控件即可。

(2)第二種方法則是創(chuàng)建一個(gè)Activity繼承自ExpandableListActivity,而后通過(guò)getExpandableListView()方法可獲得一個(gè)ExpandableListView對(duì)象。

第二種方法僅適用于一個(gè)頁(yè)面中只有一個(gè)ExpandableListView的情況。繼承的Activity不需要再調(diào)用setContentView()方法,在ExpandableListActivity中已經(jīng)關(guān)聯(lián)了一個(gè)系統(tǒng)定義的布局文件。

3. 部分屬性和點(diǎn)擊事件

android:groupIndicator、android:childIndicator:組條目和子條目前面的圖標(biāo),默認(rèn)值為箭頭,可設(shè)置自定義圖片資源。若不顯示該圖標(biāo),則設(shè)置為@null。

android:divider、android:childDivider:組和子條目的分隔線(xiàn)。

ExpandableListView的點(diǎn)擊事件有兩個(gè),分別對(duì)應(yīng)組和子條目的點(diǎn)擊事件:

設(shè)置組的點(diǎn)擊事件:setOnGroupClickListener(OnGroupClickListener listener)

設(shè)置子條目的點(diǎn)擊事件:setOnChildClickListener(OnChildClickListener listener)

5. 適配器

根據(jù)數(shù)據(jù)源的不同,可使用的適配器有兩個(gè):BaseExpandableListAdapter和CursorTreeAdapter,其中,CursorTreeAdapter用于數(shù)據(jù)源為Cursor對(duì)象的情況下,其它情況則使用BaseExpandableListAdapter。

(1)BaseExpandableListAdapter需要重寫(xiě)的方法:

getGroup():從數(shù)據(jù)源中獲取組的數(shù)據(jù)內(nèi)容。

getGroupCount():獲取組的總數(shù)。

getGroupId():獲取組的ID。

getGroupView():獲取組的視圖。

getChild():從數(shù)據(jù)源中獲取子條目的內(nèi)容。

getChildCount():獲取指定組中的子條目總數(shù),并非全部的子條目。

getChildId():獲取子條目的ID。

getChildView():獲取子條目的視圖

hasStableIds():判斷id對(duì)應(yīng)的條目是否已經(jīng)繪制,用于優(yōu)化列表。

isChildSelectable():子條目是否允許點(diǎn)擊,若返回false,則子條目點(diǎn)擊事件無(wú)效。

(2)CursorTreeAdapter需要重寫(xiě)的方法:

CursorTreeAdapter():構(gòu)造方法傳入組的Cursor對(duì)象。

getChildrenCursor():傳入組的Cursor對(duì)象,獲取相應(yīng)的組的子條目的Cursor對(duì)象。

newGroupView():創(chuàng)建組的視圖,返回一個(gè)新的視圖。

bindGroupView():在這里綁定組視圖的數(shù)據(jù)內(nèi)容,第一個(gè)參數(shù)即newGroupView()方法的返回值。

newChildView():創(chuàng)建子條目的視圖。

bindChildView():綁定子條目視圖的數(shù)據(jù)內(nèi)容。

6. 簡(jiǎn)單范例

實(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);
  }
}

加載測(cè)試數(shù)據(jù)用的工具類(lèi):

public class LoadData {

  // 組的數(shù)據(jù)內(nèi)容
  public static List<String> getGroupData() {
    List<String> groupDataList = new ArrayList<>();
    groupDataList.add("計(jì)算機(jī)基礎(chǔ)");
    groupDataList.add("安卓開(kāi)發(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ù)存儲(chǔ)");
    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)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI