本文主要是介绍ExpandableListView修改group图标,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、通过修改xml文件中groupIndicator更换为@drawable/**
设置selector
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 下拉状态 -->
<item android:state_expanded="false"
android:drawingCacheQuality="auto"
android:drawable="@drawable/xiala"/>
<!-- 收起状态 -->
<item android:state_expanded="true"
android:drawingCacheQuality="auto"
android:drawable="@drawable/shouqi"/>
</selector>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 禁用系统自带图标android:groupIndicator="@null" -->
<ExpandableListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:groupIndicator="@null"
android:id="@+id/mExpandableListView"
/>
</LinearLayout>
2、group.xml
<LinearLayout
xmlns:android=" http://schemas.android.com/apk/res/android"
android:orientation = "horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width = "25dip"
android:layout_height = "25dip"
android:layout_marginTop="10dip"
android:id="@+id/mImageView"
/>
<TextView
android:id = "@+id/group_tv"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:paddingLeft = "30px"
android:paddingBottom = "10px"
android:textSize = "26sp"/>
</LinearLayout>
<LinearLayout
xmlns:android=" http://schemas.android.com/apk/res/android"
android:orientation = "horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_gravity = "center_vertical"
android:id = "@+id/child_iv"
android:layout_width = "70px"
android:layout_height = "70px"
android:paddingLeft = "30px"
android:paddingTop = "2px"
android:paddingBottom = "5px"
android:src = "@drawable/icon"/>
<TextView
android:layout_gravity = "center_vertical"
android:id = "@+id/child_tv"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:paddingLeft = "30px"
android:paddingTop = "10px"
android:paddingBottom = "5px"
android:textSize = "30sp"/>
</LinearLayout>
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ExpandableListView elv = (ExpandableListView)findViewById(R.id.mExpandableListView);
List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
Map<String, String> group1 = new HashMap<String, String>();
group1.put("group", "group1");
Map<String, String> group2 = new HashMap<String, String>();
group2.put("group", "group2");
groups.add(group1);
groups.add(group2);
List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
Map<String, String> child1Data1 = new HashMap<String, String>();
child1Data1.put("child", "child1Data1");
Map<String, String> child1Data2 = new HashMap<String, String>();
child1Data2.put("child", "child1Data2");
child1.add(child1Data1);
child1.add(child1Data2);
List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
Map<String, String> child2Data1 = new HashMap<String, String>();
child2Data1.put("child", "child2Data1");
child2.add(child2Data1);
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
childs.add(child1);
childs.add(child2);
elv.setAdapter(viewAdapter);
}
class ExpandableAdapter extends BaseExpandableListAdapter
{
private Context context;
List<Map<String, String>> groups;
List<List<Map<String, String>>> childs;
* 构造函数:
* 参数1:context对象
* 参数2:一级列表数据源
* 参数3:二级列表数据源
*/
public ExpandableAdapter(Context context, List<Map<String, String>> groups, List<List<Map<String, String>>> childs)
{
this.groups = groups;
this.childs = childs;
this.context = context;
}
{
return childs.get(groupPosition).get(childPosition);
}
{
return childPosition;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent)
{
@SuppressWarnings("unchecked")
String text = ((Map<String, String>) getChild(groupPosition, childPosition)).get("child");
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.child, null);
TextView tv = (TextView) linearLayout.findViewById(R.id.child_tv);
tv.setText(text);
ImageView imageView = (ImageView)linearLayout.findViewById(R.id.child_iv);
imageView.setImageResource(R.drawable.icon);
}
{
return childs.get(groupPosition).size();
}
{
return groups.get(groupPosition);
}
{
return groups.size();
}
{
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
String text = groups.get(groupPosition).get("group");
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.group, null);
TextView textView = (TextView)linearLayout.findViewById(R.id.group_tv);
textView.setText(text);
ImageView imageView = (ImageView) linearLayout.findViewById(R.id.mImageView);
if(getChildrenCount(groupPosition) == 0){//该组下没有子项
imageView.setVisibility(View.GONE);
}else{
if(isExpanded == true){//展开状态
imageView.setImageResource(R.drawable.xiala);
}else{//收起状态
imageView.setImageResource(R.drawable.shouqi);
}
}
}
{
return false;
}
{
return false;
}
}
这篇关于ExpandableListView修改group图标的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!