欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

成都創(chuàng)新互聯(lián)專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、隨縣網(wǎng)絡(luò)推廣、小程序制作、隨縣網(wǎng)絡(luò)營(yíng)銷(xiāo)、隨縣企業(yè)策劃、隨縣品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪(fǎng)、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供隨縣建站搭建服務(wù),24小時(shí)服務(wù)熱線(xiàn):18982081108,官方網(wǎng)址:chinadenli.net

具體內(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)把它分享出去讓更多的人看到。

本文名稱(chēng):ExpandableListView如何實(shí)現(xiàn)手風(fēng)琴效果
本文網(wǎng)址:http://chinadenli.net/article26/poohcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化企業(yè)建站、商城網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、用戶(hù)體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)