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

Android UI控件ExpandableListView基本用法詳解

 更新時間:2016年09月08日 10:14:20   作者:生命壹號  
這篇文章主要為大家詳細介紹了Android UI控件ExpandableListView基本用法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

ExpandableListView介紹 

ExpandableListView的引入 

ExpandableListView可以顯示一個視圖垂直滾動顯示兩級列表中的條目,這不同于列表視圖(ListView)。ExpandableListView允許有兩個層次:一級列表中有二級列表。
 比如在手機設(shè)置中,對于分類,有很好的效果。手機版QQ也是這樣的效果。

 

使用ExpandableListView的整體思路 

(1)給ExpandableListView設(shè)置適配器,那么必須先設(shè)置數(shù)據(jù)源。

 (2)數(shù)據(jù)源,就是此處的適配器類ExpandableAdapter,此方法繼承了BaseExpandableListAdapter,需要重寫里面的10個方法。
 數(shù)據(jù)源中,用到了自定義的View布局,此時根據(jù)自己的需求,來設(shè)置組和子項的布局樣式。
 getChildView()和getGroupView()方法設(shè)置自定義布局。 

(3)數(shù)據(jù)源設(shè)置好,直接給ExpandableListView.setAdapter()即可實現(xiàn)此收縮功能。 

ExpandableListView的完整代碼實現(xiàn)
 (1)activity_main.xml:在里面放置一個ExpandableListView控件 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.smyhvae.expandablelistviewdemo.MainActivity">


 <ExpandableListView
  android:id="@+id/expandableListView"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  />

</RelativeLayout> 

(2)item_group.xml:一級列表的item的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#cccccc"
    android:orientation="horizontal">

 <TextView
  android:id="@+id/tv_group"
  android:layout_width="wrap_content"
  android:layout_height="30dp"
  android:gravity="center"
  android:text="group text"
  android:textColor="#000000"
  />

</LinearLayout> 

(3)item_child.xml:二級列表的item的布局

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

 <ImageView
  android:id="@+id/iv_child"
  android:layout_width="30dp"
  android:layout_height="30dp"
  android:src="@mipmap/ic_launcher"/>

 <TextView
  android:id="@+id/tv_child"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="item text"
  android:textColor="#000000"/>

</LinearLayout> 

(4)MainActivity.java: 

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

 //View
 private ExpandableListView expandableListView;

 //Model:定義的數(shù)據(jù)
 private String[] groups = {"A", "B", "C"};

 //注意,字符數(shù)組不要寫成{{"A1,A2,A3,A4"}, {"B1,B2,B3,B4,B5"}, {"C1,C2,C3,C4"}}
 private String[][] childs = {{"A1", "A2", "A3", "A4"}, {"A1", "A2", "A3", "B4"}, {"A1", "A2", "A3", "C4"}};


 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

  expandableListView.setAdapter(new MyExpandableListView());

 }


 //為ExpandableListView自定義適配器
 class MyExpandableListView extends BaseExpandableListAdapter {

  //返回一級列表的個數(shù)
  @Override
  public int getGroupCount() {
   return groups.length;
  }

  //返回每個二級列表的個數(shù)
  @Override
  public int getChildrenCount(int groupPosition) { //參數(shù)groupPosition表示第幾個一級列表
   Log.d("smyhvae", "-->" + groupPosition);
   return childs[groupPosition].length;
  }

  //返回一級列表的單個item(返回的是對象)
  @Override
  public Object getGroup(int groupPosition) {
   return groups[groupPosition];
  }

  //返回二級列表中的單個item(返回的是對象)
  @Override
  public Object getChild(int groupPosition, int childPosition) {
   return childs[groupPosition][childPosition]; //不要誤寫成groups[groupPosition][childPosition]
  }

  @Override
  public long getGroupId(int groupPosition) {
   return groupPosition;
  }

  @Override
  public long getChildId(int groupPosition, int childPosition) {
   return childPosition;
  }

  //每個item的id是否是固定?一般為true
  @Override
  public boolean hasStableIds() {
   return true;
  }

  //【重要】填充一級列表
  @Override
  public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

   if (convertView == null) {
    convertView = getLayoutInflater().inflate(R.layout.item_group, null);
   } else {

   }
   TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group);
   tv_group.setText(groups[groupPosition]);
   return convertView;
  }

  //【重要】填充二級列表
  @Override
  public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

   if (convertView == null) {
    convertView = getLayoutInflater().inflate(R.layout.item_child, null);
   }

   ImageView iv_child = (ImageView) convertView.findViewById(R.id.iv_child);
   TextView tv_child = (TextView) convertView.findViewById(R.id.tv_child);

   //iv_child.setImageResource(resId);
   tv_child.setText(childs[groupPosition][childPosition]);

   return convertView;
  }

  //二級列表中的item是否能夠被選中?可以改為true
  @Override
  public boolean isChildSelectable(int groupPosition, int childPosition) {
   return true;
  }
 }


} 

注:請自行完成ConvertView和ViewHolder的優(yōu)化。 

工程文件:(Android Studio 2.1)http://xiazai.jb51.net/201609/yuanma/AndroidExpandableListView(jb51.net).rar

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

相關(guān)文章

  • Android TabHost如何實現(xiàn)頂部選項卡

    Android TabHost如何實現(xiàn)頂部選項卡

    這篇文章主要介紹了Android TabHost如何實現(xiàn)頂部選項卡,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • Android開發(fā)flow常見API的使用示例詳解

    Android開發(fā)flow常見API的使用示例詳解

    這篇文章主要為大家介紹了Android開發(fā)flow常見API的使用示例詳解,希望能夠幫助大家更好的掌握flow使用,熟練的應(yīng)用于各種場景,祝大家多多進步,早日升職加薪
    2022-08-08
  • android實現(xiàn)來電靜音示例(監(jiān)聽來電)

    android實現(xiàn)來電靜音示例(監(jiān)聽來電)

    這篇文章主要介紹了手機來電鈴聲響起后,通過此代碼實現(xiàn)靜音而非掛斷的方法的相關(guān)資料
    2014-03-03
  • 詳解Android的四大應(yīng)用程序組件

    詳解Android的四大應(yīng)用程序組件

    這篇文章主要介紹了Android的應(yīng)用程序組件的相關(guān)資料,幫助大家更好的理解和使用Android,感興趣的朋友可以了解下
    2021-01-01
  • Android之ProgressBar即時顯示下載進度詳解

    Android之ProgressBar即時顯示下載進度詳解

    這篇文章主要為大家詳細介紹了Android之ProgressBar即時顯示下載進度,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 通過實例簡單講解Android App中的Activity組件

    通過實例簡單講解Android App中的Activity組件

    這篇文章主要介紹了通過Android App中的Activity組件,包括Activity的定義和繼承以及啟動等基本知識,需要的朋友可以參考下
    2016-04-04
  • Studio 編譯報錯:compileSdkVersion ''android-24'' requires JDK 1.8 or later to compile.的解決辦法

    Studio 編譯報錯:compileSdkVersion ''android-24'' requires JDK 1.

    今天小編就為大家分享一篇關(guān)于Studio編譯報錯:compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • 詳解Android應(yīng)用中DialogFragment的基本用法

    詳解Android應(yīng)用中DialogFragment的基本用法

    Android App中建議使用DialogFragment作為對話框的容器,DialogFragment類提供了創(chuàng)建對話框并管理其外觀需要的所有控件,本文主要內(nèi)容便為詳解Android應(yīng)用中DialogFragment的基本用法,而不再需要調(diào)用Dialog的方法需要的朋友可以參考下
    2016-05-05
  • Android劉海屏、水滴屏全面屏適配小結(jié)

    Android劉海屏、水滴屏全面屏適配小結(jié)

    這篇文章主要介紹了Android劉海屏、水滴屏全面屏適配小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-06-06
  • Gradle?Build?Cache引發(fā)的Task緩存編譯問題

    Gradle?Build?Cache引發(fā)的Task緩存編譯問題

    這篇文章主要為大家介紹了Gradle?Build?Cache引發(fā)的Task緩存編譯問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06

最新評論

佛教| 临沧市| 洞口县| 舒城县| 甘谷县| 太保市| 棋牌| 德惠市| 泰来县| 峨山| 马关县| 溧水县| 嘉义市| 梓潼县| 调兵山市| 抚顺市| 芒康县| 金溪县| 顺义区| 平凉市| 丹江口市| 泗洪县| 元谋县| 大石桥市| 合水县| 讷河市| 辉县市| 独山县| 浮山县| 平乐县| 徐水县| 章丘市| 红安县| 塔城市| 沁水县| 南丰县| 竹山县| 中山市| 加查县| 景洪市| 通江县|