欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

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

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

下面是我自己簡(jiǎn)單做的一個(gè)顯示效果:

 

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="請(qǐng)輸入想對(duì)他說(shuō)的話"/>
</LinearLayout>

Activity中Java實(shí)現(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;
  //定義分組名以及對(duì)應(yīng)的圖片數(shù)組,需一一對(duì)應(yīng)
  String[] country={"魏國(guó)","蜀國(guó)","吳國(guó)"};
  int[] icon={R.mipmap.wei,R.mipmap.shu,R.mipmap.wu};
  //使用二維定義組內(nèi)成員以及對(duì)應(yīng)頭像,同樣需要以一一對(duì)應(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è)置點(diǎn)擊下拉子菜單的監(jiān)聽(tīng)事件
    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(不再贅述,詳情請(qǐng)看前期相關(guān)博文,文章末尾有鏈接)
  public void show(View v,String name){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(name);//設(shè)置標(biāo)題名為傳入的字符串(分組內(nèi)點(diǎn)擊對(duì)應(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, "不想說(shuō)了", Toast.LENGTH_SHORT).show();
      }
    });
    builder.show();
  }
  //設(shè)置自定義適配器
  class MyAdapter extends BaseExpandableListAdapter{
    //獲取國(guó)家個(gè)數(shù)
    @Override
    public int getGroupCount() {
      return country.length;
    }
    //獲取每個(gè)國(guó)家對(duì)應(yīng)的人數(shù)
    @Override
    public int getChildrenCount(int groupPosition) {
      return heros[groupPosition].length;
    }
    //獲取對(duì)應(yīng)國(guó)家名
    @Override
    public Object getGroup(int groupPosition) {
      return country[groupPosition];
    }
    //獲取對(duì)應(yīng)國(guó)家對(duì)應(yīng)的任務(wù)
    @Override
    public Object getChild(int groupPosition, int childPosition) {
      return heros[groupPosition][childPosition];
    }
    //獲取選擇的國(guó)家對(duì)應(yīng)數(shù)組下標(biāo)
    @Override
    public long getGroupId(int groupPosition) {
      return groupPosition;
    }
    //獲取選擇的任務(wù)對(duì)應(yīng)數(shù)組下標(biāo)
    @Override
    public long getChildId(int groupPosition, int childPosition) {
      return childPosition;
    }
    //表示人物和國(guó)家ID是否穩(wěn)定的更改底層數(shù)據(jù)
    @Override
    public boolean hasStableIds() {
      return true;
    }
    //獲取一個(gè)視圖顯示國(guó)家名以及對(duì)應(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;
    }
    //獲取一個(gè)視圖顯示國(guó)家對(duì)應(yīng)人物以及對(duì)應(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è)置子選項(xiàng)(對(duì)應(yīng)人物)是可選的
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
      return true;
    }
  }
}

補(bǔ)充說(shuō)明:

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

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

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

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

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

相關(guān)文章

最新評(píng)論