亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 中使用ExpandableListView 實現(xiàn)分組的實例

 更新時間:2016年12月03日 08:59:27   投稿:lqh  
這篇文章主要介紹了Android 中使用ExpandableListView 實現(xiàn)分組的實例的相關(guān)資料,這里提供實例代碼及實現(xiàn)效果圖,需要的朋友可以參考下

 Android 中使用ExpandableListView 實現(xiàn)分組

一個視圖顯示垂直滾動兩級列表中的條目。這不同于列表視圖,允許兩個層次,類似于QQ的好友分組。要實現(xiàn)這個效果的整體思路為:

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

2.數(shù)據(jù)源,就是此處的適配器類,此方法繼承了BaseExpandableListAdapter,它是ExpandableListView的一個子類。需要重寫里面的多個方法。方法的意思,代碼中都有詳細(xì)的注釋。數(shù)據(jù)源中,用到了自定義的View布局,此時根據(jù)自己的需求,來設(shè)置組和子項的布局樣式。getChildView()和getGroupView()方法設(shè)置自定義布局。

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

下面是我自己簡單做的一個顯示效果:

 

layout中主視圖expandable_layout.xml(ExpandableListView布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ExpandableListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/el">
  </ExpandableListView>
</LinearLayout>

Layout中g(shù)roup_layout.xml(分組組名展示布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="horizontal" android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center_vertical">
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@mipmap/ic_launcher"
    android:id="@+id/iv_group" />
  <TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_group" />
</LinearLayout>

Layout中child_layout.xml(子菜單item布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="horizontal" android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center_vertical"
  android:paddingLeft="50dp">
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@mipmap/ic_launcher"
    android:id="@+id/iv_item" />
  <TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_item" />
</LinearLayout>

Layout中alertdialog_layout.xml(AlertDialog自定義顯示布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/et"
    android:hint="請輸入想對他說的話"/>
</LinearLayout>

Activity中Java實現(xiàn)代碼(ExpandableListViewDemo.java):

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
 * Created by panchengjia on 2016/12/2.
 */
public class ExpandableListViewDemo extends AppCompatActivity {
  ExpandableListView el;
  //定義分組名以及對應(yīng)的圖片數(shù)組,需一一對應(yīng)
  String[] country={"魏國","蜀國","吳國"};
  int[] icon={R.mipmap.wei,R.mipmap.shu,R.mipmap.wu};
  //使用二維定義組內(nèi)成員以及對應(yīng)頭像,同樣需要以一一對應(yīng)
  String[][] heros={{"司馬懿","郭嘉","夏侯惇","甄姬"},{"劉備","趙云","張飛"},{"孫權(quán)","周瑜"}};
  int[][] icons={{R.mipmap.simayi,R.mipmap.guojia,R.mipmap.xiahoudun,R.mipmap.zhenji},
      {R.mipmap.liubei,R.mipmap.zhaoyun,R.mipmap.zhangfei},{R.mipmap.sunquan,R.mipmap.zhouyu}};
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.expandable_layout);
    el= (ExpandableListView) findViewById(R.id.el);
    //設(shè)置點擊下拉子菜單的監(jiān)聽事件
    el.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
      @Override
      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        //獲取分組成員的姓名
        TextView tv = (TextView) v.findViewById(R.id.tv_item);
        String name = tv.getText().toString();
        //調(diào)用show方法(自定義AlertDialog)
        show(v,name);
        return false;
      }
    });
    el.setAdapter(new MyAdapter());//設(shè)置自定義適配器
  }
  //定義show方法調(diào)出AlertDialog(不再贅述,詳情請看前期相關(guān)博文,文章末尾有鏈接)
  public void show(View v,String name){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(name);//設(shè)置標(biāo)題名為傳入的字符串(分組內(nèi)點擊對應(yīng)的人物名)
    View msg = getLayoutInflater().inflate(R.layout.altertdialog_layout,null);
    builder.setView(msg);
    builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(ExpandableListViewDemo.this, "已發(fā)送", Toast.LENGTH_SHORT).show();
      }
    });
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(ExpandableListViewDemo.this, "不想說了", Toast.LENGTH_SHORT).show();
      }
    });
    builder.show();
  }
  //設(shè)置自定義適配器
  class MyAdapter extends BaseExpandableListAdapter{
    //獲取國家個數(shù)
    @Override
    public int getGroupCount() {
      return country.length;
    }
    //獲取每個國家對應(yīng)的人數(shù)
    @Override
    public int getChildrenCount(int groupPosition) {
      return heros[groupPosition].length;
    }
    //獲取對應(yīng)國家名
    @Override
    public Object getGroup(int groupPosition) {
      return country[groupPosition];
    }
    //獲取對應(yīng)國家對應(yīng)的任務(wù)
    @Override
    public Object getChild(int groupPosition, int childPosition) {
      return heros[groupPosition][childPosition];
    }
    //獲取選擇的國家對應(yīng)數(shù)組下標(biāo)
    @Override
    public long getGroupId(int groupPosition) {
      return groupPosition;
    }
    //獲取選擇的任務(wù)對應(yīng)數(shù)組下標(biāo)
    @Override
    public long getChildId(int groupPosition, int childPosition) {
      return childPosition;
    }
    //表示人物和國家ID是否穩(wěn)定的更改底層數(shù)據(jù)
    @Override
    public boolean hasStableIds() {
      return true;
    }
    //獲取一個視圖顯示國家名以及對應(yīng)的圖標(biāo)(ListView適配器優(yōu)化)
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
      ViewHolder vh;
      if(convertView==null){
        convertView=getLayoutInflater().inflate(R.layout.group_layout,null);
        vh=new ViewHolder();
        vh.tv= (TextView) convertView.findViewById(R.id.tv_group);
        vh.iv= (ImageView) convertView.findViewById(R.id.iv_group);
        convertView.setTag(vh);
      }
      vh= (ViewHolder) convertView.getTag();
      vh.tv.setText(country[groupPosition]);
      vh.iv.setImageResource(icon[groupPosition]);
      return convertView;
    }
    //獲取一個視圖顯示國家對應(yīng)人物以及對應(yīng)的圖標(biāo)(ListView適配器優(yōu)化)
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
      ViewHolder vh;
      if(convertView==null){
        convertView=getLayoutInflater().inflate(R.layout.child_layout,null);
        vh=new ViewHolder();
        vh.tv= (TextView) convertView.findViewById(R.id.tv_item);
        vh.iv= (ImageView) convertView.findViewById(R.id.iv_item);
        convertView.setTag(vh);
      }
      vh= (ViewHolder) convertView.getTag();
      vh.tv.setText(heros[groupPosition][childPosition]);
      vh.iv.setImageResource(icons[groupPosition][childPosition]);
      return convertView;
    }
    class ViewHolder{
      ImageView iv;
      TextView tv;
    }
    //設(shè)置子選項(對應(yīng)人物)是可選的
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
      return true;
    }
  }
}

補充說明:

java實現(xiàn)代碼中用到自定義適配器ListView的優(yōu)化,優(yōu)化步驟如下:

(1)使用固定寬高(match_parent)的ListView,有助于在填充item(ListView中每行的布局)時避免重復(fù)渲染ListView組件,導(dǎo)致重復(fù)多次調(diào)用getView方法。

(2)Convertview用來重復(fù)使用已被隱藏的item對象,從而避免重復(fù)創(chuàng)建每個item的view對象。

(3)使用ViewHolder優(yōu)化提高容器中查找組件的效率。

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • Win10下Android App安裝配置開發(fā)環(huán)境

    Win10下Android App安裝配置開發(fā)環(huán)境

    這篇文章主要為大家詳細(xì)介紹了Win10下Android App安裝配置開發(fā)環(huán)境,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android編程使用Service實現(xiàn)Notification定時發(fā)送功能示例

    Android編程使用Service實現(xiàn)Notification定時發(fā)送功能示例

    這篇文章主要介紹了Android編程使用Service實現(xiàn)Notification定時發(fā)送功能,涉及Android服務(wù)Service控制通知的發(fā)送功能相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • android實現(xiàn)記事本app

    android實現(xiàn)記事本app

    這篇文章主要為大家詳細(xì)介紹了android實現(xiàn)記事本app的相關(guān)代碼 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Flutter倒計時/計時器的實現(xiàn)代碼

    Flutter倒計時/計時器的實現(xiàn)代碼

    這篇文章主要介紹了Flutter倒計時/計時器的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Android開場動畫類完整實現(xiàn)代碼

    Android開場動畫類完整實現(xiàn)代碼

    這篇文章主要介紹了Android開場動畫類完整實現(xiàn)代碼,是非常實用的功能,需要的朋友可以參考下
    2014-07-07
  • Android ActivityManagerService啟動流程詳解

    Android ActivityManagerService啟動流程詳解

    這篇文章主要介紹了Android ActivityManagerService啟動流程,AMS,即ActivityManagerService,是安卓java framework的一個服務(wù),運行在system_server進(jìn)程。此服務(wù)十分重要,因為它管理著安卓的四大組件,是安卓APP開發(fā)者最常接觸到的一個服務(wù)
    2023-02-02
  • Android 中 EventBus 的使用之多線程事件處理

    Android 中 EventBus 的使用之多線程事件處理

    這篇文章主要介紹了Android 中 EventBus 的使用之多線程事件處理的相關(guān)資料,需要的朋友可以參考下
    2015-11-11
  • Android selector背景選擇器的使用詳解

    Android selector背景選擇器的使用詳解

    本篇文章是對Android中selector背景選擇器的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • html5在android中的使用問題及技巧解讀

    html5在android中的使用問題及技巧解讀

    本文將詳細(xì)介紹下html5在android中的使用:特效按鈕的進(jìn)展/在html5中有關(guān)于觸摸屏的事件,感興趣的你可以參考下,或許對你有所幫助
    2013-03-03
  • Android網(wǎng)絡(luò)請求-sign參數(shù)的設(shè)置方式

    Android網(wǎng)絡(luò)請求-sign參數(shù)的設(shè)置方式

    這篇文章主要介紹了Android網(wǎng)絡(luò)請求-sign參數(shù)的設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03

最新評論