国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

ExpandableListView實現手風琴效果

2019-12-12 02:11:54
字體:
來源:轉載
供稿:網友

本文實例為大家分享了ExpandableListView實現手風琴效果的具體代碼,供大家參考,具體內容如下

1. 效果示例圖

2. 創建方法

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

(2)第二種方法則是創建一個Activity繼承自ExpandableListActivity,而后通過getExpandableListView()方法可獲得一個ExpandableListView對象。

第二種方法僅適用于一個頁面中只有一個ExpandableListView的情況。繼承的Activity不需要再調用setContentView()方法,在ExpandableListActivity中已經關聯了一個系統定義的布局文件。

3. 部分屬性和點擊事件

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

android:divider、android:childDivider:組和子條目的分隔線。

ExpandableListView的點擊事件有兩個,分別對應組和子條目的點擊事件:

設置組的點擊事件:setOnGroupClickListener(OnGroupClickListener listener)

設置子條目的點擊事件:setOnChildClickListener(OnChildClickListener listener)

5. 適配器

根據數據源的不同,可使用的適配器有兩個:BaseExpandableListAdapter和CursorTreeAdapter,其中,CursorTreeAdapter用于數據源為Cursor對象的情況下,其它情況則使用BaseExpandableListAdapter。

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

getGroup():從數據源中獲取組的數據內容。

getGroupCount():獲取組的總數。

getGroupId():獲取組的ID。

getGroupView():獲取組的視圖。

getChild():從數據源中獲取子條目的內容。

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

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

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

hasStableIds():判斷id對應的條目是否已經繪制,用于優化列表。

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

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

CursorTreeAdapter():構造方法傳入組的Cursor對象。

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

newGroupView():創建組的視圖,返回一個新的視圖。

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

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

bindChildView():綁定子條目視圖的數據內容。

6. 簡單范例

實現效果圖中的例子。

布局:

<?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);  }}

加載測試數據用的工具類:

public class LoadData {  // 組的數據內容  public static List<String> getGroupData() {    List<String> groupDataList = new ArrayList<>();    groupDataList.add("計算機基礎");    groupDataList.add("安卓開發");    return groupDataList;  }  // 子條目的數據內容  public static List<List<String>> getChildData() {    List<List<String>> childDataList = new ArrayList<>();    List<String> group1 = new ArrayList<>();    group1.add("數據結構");    group1.add("算法");    group1.add("計算機網絡");    childDataList.add(group1);    List<String> group2 = new ArrayList<>();    group2.add("控件使用");    group2.add("網絡操作");    group2.add("數據存儲");    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;  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 体育| 北流市| 山丹县| 土默特右旗| 罗甸县| 卢龙县| 旌德县| 兴和县| 汽车| 珠海市| 玛纳斯县| 清丰县| 彭州市| 鞍山市| 庆元县| 桐城市| 上虞市| 吉林市| 七台河市| 长春市| 桓台县| 凤阳县| 桓仁| 临武县| 张家川| 丹江口市| 建德市| 阿克陶县| 宣城市| 青岛市| 那曲县| 新蔡县| 海原县| 密山市| 泰宁县| 阜新| 永福县| 黄平县| 芦山县| 龙岩市| 延安市|