最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年08月24日 08:31:16   作者:Joah  
這篇文章主要為大家詳細(xì)介紹了ExpandableListView實(shí)現(xiàn)手風(fēng)琴效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了ExpandableListView實(shí)現(xiàn)手風(fēng)琴效果的具體代碼,供大家參考,具體內(nèi)容如下

1. 效果示例圖

2. 創(chuàng)建方法

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

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

第二種方法僅適用于一個(gè)頁面中只有一個(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:組和子條目的分隔線。

ExpandableListView的點(diǎn)擊事件有兩個(gè),分別對應(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對象的情況下,其它情況則使用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)建組的視圖,返回一個(gè)新的視圖。

bindGroupView():在這里綁定組視圖的數(shù)據(jù)內(nèi)容,第一個(gè)參數(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ù)存儲(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;
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android獲取照片、裁剪圖片、壓縮圖片

    Android獲取照片、裁剪圖片、壓縮圖片

    這篇文章主要為大家詳細(xì)介紹了Android獲取照片、裁剪圖片、壓縮圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 解決EditText、ListView以及GridView同時(shí)使用,輸入法自動(dòng)跳出來的方法

    解決EditText、ListView以及GridView同時(shí)使用,輸入法自動(dòng)跳出來的方法

    本篇文章是對在Android中EditText、ListView以及GridView同時(shí)使用,輸入法自動(dòng)跳出來的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 如何利用adb卸載手機(jī)預(yù)裝軟件(系統(tǒng)軟件)

    如何利用adb卸載手機(jī)預(yù)裝軟件(系統(tǒng)軟件)

    對于Android手機(jī)通常有很多不必要的預(yù)置軟件,但是又無法卸載,占用桌面有很難受,所以本次使用adb工具來實(shí)現(xiàn)從電腦命令來卸載或停用軟件,下面這篇文章主要給大家介紹了關(guān)于如何利用adb卸載手機(jī)預(yù)裝軟件(系統(tǒng)軟件)的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • Android中自定義一個(gè)View的方法詳解

    Android中自定義一個(gè)View的方法詳解

    這篇文章主要介紹了Android中自定義一個(gè)View的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android中自定義View的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-07-07
  • 解決Android Studio Design界面不顯示layout控件的問題

    解決Android Studio Design界面不顯示layout控件的問題

    這篇文章主要介紹了解決Android Studio Design界面不顯示layout控件的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Kotlin圖文講解多語言支持實(shí)現(xiàn)方法

    Kotlin圖文講解多語言支持實(shí)現(xiàn)方法

    這篇文章主要介紹了Kotlin多語言支持實(shí)現(xiàn)方法,在Android開發(fā)中,我們?nèi)绾沃С侄嗾Z言APP呢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值
    2023-02-02
  • Android原生繪圖工具Paint詳細(xì)

    Android原生繪圖工具Paint詳細(xì)

    這篇文章要給大家分享的是Android原生繪圖工具Paint,android中提供了類似的工具Canvas和Paint,分別對應(yīng)畫布和畫筆,本文就來介紹Androi中的Paint,感興趣的小伙伴一起來學(xué)習(xí)下面文章內(nèi)容
    2021-09-09
  • Android利用RecyclerView實(shí)現(xiàn)全選、置頂和拖拽功能示例

    Android利用RecyclerView實(shí)現(xiàn)全選、置頂和拖拽功能示例

    列表控件可以說是我們絕大部分App中都會(huì)使用的,為了提升交互樂趣,我們經(jīng)常需要在列表中加入置頂、拖拽等操作,下面這篇文章主要介紹了Android利用RecyclerView如何實(shí)現(xiàn)全選、置頂和拖拽功能的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-04-04
  • Android神兵利器之Image Asset Studio的實(shí)現(xiàn)

    Android神兵利器之Image Asset Studio的實(shí)現(xiàn)

    這篇文章主要介紹了Android神兵利器之Image Asset Studio的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 一個(gè)簡單的toolabar結(jié)合drawlayout使用方法

    一個(gè)簡單的toolabar結(jié)合drawlayout使用方法

    這篇文章主要為大家詳細(xì)介紹了一個(gè)簡單的toolabar結(jié)合drawlayout的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10

最新評(píng)論

同江市| 湘乡市| 温州市| 奉节县| 汉源县| 蒙自县| 左权县| 循化| 福安市| 西充县| 都昌县| 衡阳县| 兴山县| 葫芦岛市| 土默特右旗| 伊宁县| 绿春县| 怀宁县| 额敏县| 德化县| 高淳县| 正宁县| 临武县| 保康县| 南汇区| 万源市| 平顺县| 通城县| 桓仁| 许昌市| 青河县| 嵊州市| 洛浦县| 宿州市| 博客| 孟州市| 永仁县| 阳曲县| 茶陵县| 贵定县| 景东|