关于ExpandableListView的一个小例子

喜欢显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到android的ExpandableListView,今天研究了一下这个的用法,也参考了很多资料动手写了一个小demo,实现了基本的功能,但界面优化方面做得还不够好,有待改进,素材采用了Q版三国杀武将的图片,很有爱哈哈,下面直接上效果图以及源代码~!

main.xml的布局很简单啦,只是一个ExpandableListView 就OK了

但值得简单说下的是android:cacheColorHint="#00000000",这个设置可以去除拖动view时背景变成黑色的效果

android:listSelector="#00000000" ,可以去除选中时的黄色底色

复制代码
1<?xmlversion="1.0"encoding="utf-8"?>
2<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
3android:layout_width="fill_parent"
4android:layout_height="fill_parent"
5android:orientation="vertical">
6<ExpandableListView
7android:id="@+id/list"
8android:layout_width="fill_parent"
9android:layout_height="fill_parent"
10android:background="#ffffff"
11android:cacheColorHint="#00000000"
12android:listSelector="#00000000"
13>
14 </ExpandableListView>
15 </LinearLayout>
16
复制代码


java代码:

复制代码
package com.eyu.activity_test;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ExpandableList extends Activity{

protected void onCreate(BundlesavedInstanceState){
// TODOAuto-generatedmethodstub
super .onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);

final ExpandableListAdapteradapter= new BaseExpandableListAdapter(){
// 设置组视图的图片
int []logos= new int []{R.drawable.wei,R.drawable.shu,R.drawable.wu};
// 设置组视图的显示文字
private String[]generalsTypes= new String[]{"魏","蜀","吴"};
// 子视图显示文字
private String[][]generals= new String[][]{
{"夏侯惇","甄姬","许褚","郭嘉","司马懿","杨修"},
{"马超","张飞","刘备","诸葛亮","黄月英","赵云"},
{"吕蒙","陆逊","孙权","周瑜","孙尚香"}

};
// 子视图图片
public int [][]generallogos= new int [][]{
{R.drawable.xiahoudun,R.drawable.zhenji,
R.drawable.xuchu,R.drawable.guojia,
R.drawable.simayi,R.drawable.yangxiu},
{R.drawable.machao,R.drawable.zhangfei,
R.drawable.liubei,R.drawable.zhugeliang,
R.drawable.huangyueying,R.drawable.zhaoyun},
{R.drawable.lvmeng,R.drawable.luxun,R.drawable.sunquan,
R.drawable.zhouyu,R.drawable.sunshangxiang}};

// 自己定义一个获得文字信息的方法
TextViewgetTextView(){
AbsListView.LayoutParamslp= new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,64);
TextViewtextView= new TextView(
ExpandableList. this );
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setPadding(36,0,0,0);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
}


// 重写ExpandableListAdapter中的各个方法
@Override
public int getGroupCount(){
// TODOAuto-generatedmethodstub
return generalsTypes.length;
}

@Override
public ObjectgetGroup( int groupPosition){
// TODOAuto-generatedmethodstub
return generalsTypes[groupPosition];
}

@Override
public long getGroupId( int groupPosition){
// TODOAuto-generatedmethodstub
return groupPosition;
}

@Override
public int getChildrenCount( int groupPosition){
// TODOAuto-generatedmethodstub
return generals[groupPosition].length;
}

@Override
public ObjectgetChild( int groupPosition, int childPosition){
// TODOAuto-generatedmethodstub
return generals[groupPosition][childPosition];
}

@Override
public long getChildId( int groupPosition, int childPosition){
// TODOAuto-generatedmethodstub
return childPosition;
}

@Override
public boolean hasStableIds(){
// TODOAuto-generatedmethodstub
return true ;
}

@Override
public ViewgetGroupView( int groupPosition, boolean isExpanded,
ViewconvertView,ViewGroupparent){
// TODOAuto-generatedmethodstub
LinearLayoutll= new LinearLayout(
ExpandableList. this );
ll.setOrientation(0);
ImageViewlogo= new ImageView(ExpandableList. this );
logo.setImageResource(logos[groupPosition]);
logo.setPadding(50,0,0,0);
ll.addView(logo);
TextViewtextView=getTextView();
textView.setTextColor(Color.BLACK);
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);

return ll;
}

@Override
public ViewgetChildView( int groupPosition, int childPosition,
boolean isLastChild,ViewconvertView,ViewGroupparent){
// TODOAuto-generatedmethodstub
LinearLayoutll= new LinearLayout(
ExpandableList. this );
ll.setOrientation(0);
ImageViewgenerallogo= new ImageView(
ExpandableList. this );
generallogo
.setImageResource(generallogos[groupPosition][childPosition]);
ll.addView(generallogo);
TextViewtextView=getTextView();
textView.setText(getChild(groupPosition,childPosition)
.toString());
ll.addView(textView);
return ll;
}

@Override
public boolean isChildSelectable( int groupPosition,
int childPosition){
// TODOAuto-generatedmethodstub
return true ;
}

};

ExpandableListViewexpandableListView=(ExpandableListView)findViewById(R.id.list);
expandableListView.setAdapter(adapter);


// 设置item点击的监听器
expandableListView.setOnChildClickListener( new OnChildClickListener(){

@Override
public boolean onChildClick(ExpandableListViewparent,Viewv,
int groupPosition, int childPosition, long id){

Toast.makeText(
ExpandableList. this ,
"你点击了"+adapter.getChild(groupPosition,childPosition),
Toast.LENGTH_SHORT).show();

return false ;
}
});
}
}

好的,以下是一个 ExpandableListView 二级分栏的例子: 首先在布局文件中定义 ExpandableListView 和 Adapter: ```xml <ExpandableListView android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="match_parent"/> <!-- 定义自定义的 ExpandableListAdapter --> ``` 然后在 Activity 中设置 Adapter 并为 ExpandableListView 设置点击事件: ```java public class MainActivity extends AppCompatActivity { private ExpandableListView expandableListView; private ExpandableListAdapter adapter; private List<String> listDataHeader; private HashMap<String, List<String>> listDataChild; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 准备数据 prepareListData(); // 设置 Adapter adapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); expandableListView.setAdapter(adapter); // 设置点击事件 expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(getApplicationContext(), listDataHeader.get(groupPosition) + " : " + listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show(); return false; } }); } // 准备数据 private void prepareListData() { listDataHeader = new ArrayList<>(); listDataChild = new HashMap<>(); // 添加一级分栏 listDataHeader.add("Fruits"); listDataHeader.add("Vegetables"); // 添加二级分栏 List<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Mango"); List<String> vegetables = new ArrayList<>(); vegetables.add("Carrot"); vegetables.add("Tomato"); vegetables.add("Potato"); // 将二级分栏添加到对应的一级分栏下 listDataChild.put(listDataHeader.get(0), fruits); listDataChild.put(listDataHeader.get(1), vegetables); } } ``` 最后是 ExpandableListAdapter 的实现: ```java public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context context; private List<String> listDataHeader; private HashMap<String, List<String>> listDataChild; public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild) { this.context = context; this.listDataHeader = listDataHeader; this.listDataChild = listDataChild; } @Override public int getGroupCount() { return this.listDataHeader.size(); } @Override public int getChildrenCount(int groupPosition) { return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size(); } @Override public Object getGroup(int groupPosition) { return this.listDataHeader.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_group, null); } TextView lblListHeader = convertView.findViewById(R.id.lblListHeader); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_item, null); } TextView txtListChild = convertView.findViewById(R.id.lblListItem); txtListChild.setText(childText); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } ``` 其中 `list_group.xml` 和 `list_item.xml` 分别是一级分栏和二级分栏的布局文件,可以根据需要自行定义。 以上就是 ExpandableListView 二级分栏的例子,希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值