网友通过本文主要向大家介绍了expandablelistview,xexpandablelistview,android仿qq好友列表,android 好友列表,android qq好友列表等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
Android中使用ExpandableListView实现好友分组,expandablelistview
一个视图显示垂直滚动两级列表中的条目。这不同于列表视图,允许两个层次,类似于QQ的好友分组。要实现这个效果的整体思路为:
1.要给ExpandableListView 设置适配器,那么必须先设置数据源。
2.数据源,就是此处的适配器类,此方法继承了BaseExpandableListAdapter,它是ExpandableListView的一个子类。需要重写里面的多个方法。方法的意思,代码中都有详细的注释。数据源中,用到了自定义的View布局,此时根据自己的需求,来设置组和子项的布局样式。getChildView()和getGroupView()方法设置自定义布局。
3.数据源设置好,直接给ExpandableListView.setAdapter()即可实现此收缩功能。
下面是我自己简单做的一个显示效果:
layout中主视图expandable_layout.xml(ExpandableListView布局):
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <ExpandableListView 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:id="@+id/el"> 9 </ExpandableListView> 10 </LinearLayout>
Layout中group_layout.xml(分组组名展示布局):
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:orientation="horizontal" android:layout_width="wrap_content" 5 android:layout_height="wrap_content" 6 android:gravity="center_vertical"> 7 <ImageView 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 app:srcCompat="@mipmap/ic_launcher" 11 android:id="@+id/iv_group" /> 12 <TextView 13 android:text="TextView" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:id="@+id/tv_group" /> 17 </LinearLayout>
Layout中child_layout.xml(子菜单item布局):
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:orientation="horizontal" android:layout_width="wrap_content" 5 android:layout_height="wrap_content" 6 android:gravity="center_vertical" 7 android:paddingLeft="50dp"> 8 <ImageView 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 app:srcCompat="@mipmap/ic_launcher" 12 android:id="@+id/iv_item" /> 13 <TextView 14 android:text="TextView" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:id="@+id/tv_item" /> 18 </LinearLayout>
Layout中alertdialog_layout.xml(AlertDialog自定义显示布局):
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <EditText 6 android:layout_width="match_parent" 7 android:layout_height="wrap_content" 8 android:id="@+id/et" 9 android:hint="请输入想对他说的话"/> 10 </LinearLayout>
Activity中Java实现代码(ExpandableListViewDemo.java):
1 import android.content.DialogInterface; 2 import android.os.Bundle; 3 import android.support.annotation.Nullable; 4 import android.support.v7.app.AlertDialog; 5 import android.support.v7.app.AppCompatActivity; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.BaseExpandableListAdapter; 9 import android.widget.ExpandableListView; 10 import android.widget.ImageView; 11 import android.widget.TextView; 12 import android.widget.Toast; 13 /** 14 * Created by panchengjia on 2016/12/2. 15 */ 16 public class ExpandableListViewDemo extends AppCompatActivity { 17 ExpandableListView el; 18 //定义分组名以及对应的图片数组,需一一对应 19 String[] country={"魏国","蜀国","吴国"}; 20 int[] icon={R.mipmap.wei,R.mipmap.shu,R.mipmap.wu}; 21 //使用二维定义组内成员以及对应头像,同样需要以一一对应 22 String[][] heros={{"司马懿","郭嘉","夏侯惇","甄姬"},{"刘备","赵云","张飞"},{"孙权","周瑜"}}; 23 int[][] icons={{R.mipmap.simayi,R.mipmap.guojia,R.mipmap.xiahoudun,R.mipmap.zhenji}, 24 {R.mipmap.liubei,R.mipmap.zhaoyun,R.mipmap.zhangfei},{R.mipmap.sunquan,R.mipmap.zhouyu}}; 25 @Override 26 protected void onCreate(@Nullable Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.expandable_layout); 29 el= (ExpandableListView) findViewById(R.id.el); 30 //设置点击下拉子菜单的监听事件 31 el.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 32 @Override 33 public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, <