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

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)文章

最新評論

九台市| 休宁县| 辉南县| 临夏市| 黎川县| 商丘市| 双峰县| 湖口县| 禹州市| 宜城市| 永平县| 顺昌县| 齐齐哈尔市| 青阳县| 杭锦旗| 金川县| 原平市| 安化县| 新余市| 仪陇县| 清河县| 上蔡县| 湘西| 巴楚县| 临沧市| 安泽县| 仲巴县| 凤山县| 松江区| 涟源市| 百色市| 普格县| 沧州市| 左权县| 抚远县| 蒙阴县| 靖远县| 齐齐哈尔市| 大方县| 长宁县| 新津县|