如何将自定义图标和组指示器添加到ExpandableListview android
我正在使用带有自定义如何将自定义图标和组指示器添加到ExpandableListview android,android,expandablelistview,Android,Expandablelistview,我正在使用带有自定义组指示符的ExpandableListview。 我在这方面很成功 但是,现在有一种情况,我需要左侧的自定义图标,右侧的组指示器 我如何做到这一点 请建议我如何更改适配器类 list_group.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:lay
组指示符的ExpandableListview
。
我在这方面很成功
但是,现在有一种情况,我需要左侧的自定义图标
,右侧的组指示器
我如何做到这一点
请建议我如何更改适配器类
list_group.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:background="#FEFFFF">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView android:id="@+id/shape_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/off"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:padding="5dp"
/>
<TextView
android:id="@+id/lblListHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:text="Group Header"
android:textColor="#272727"
android:textSize="17dp" />
<ImageView android:src="@drawable/listview_expander_group"
android:contentDescription="expand image"
android:id="@+id/explist_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</RelativeLayout>
ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private static final int[] EMPTY_STATE_SET = {};
private static final int[] GROUP_EXPANDED_STATE_SET =
{android.R.attr.state_expanded};
private static final int[][] GROUP_STATE_SETS = {
EMPTY_STATE_SET, // 0
GROUP_EXPANDED_STATE_SET // 1
};
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_child, null);
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.list_child_chkbox);
txtTitle.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
Log.e("groupPosition", "kargr "+groupPosition);
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
View ind = convertView.findViewById( R.id.explist_indicator);
if( ind != null ) {
ImageView indicator = (ImageView)ind;
if( getChildrenCount( groupPosition ) == 0 ) {
indicator.setVisibility( View.INVISIBLE );
} else {
indicator.setVisibility( View.VISIBLE );
int stateSetIndex = ( isExpanded ? 1 : 0) ;
Drawable drawable = indicator.getDrawable();
drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
}
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私有静态final int[]空_STATE_SET={};
私有静态final int[]组\扩展\状态\集=
{android.R.attr.state_expanded};
私有静态最终整数[][]组状态集合={
空的\u状态\u集,//0
组\扩展\状态\集//1
};
私人语境(private Context)(私人语境);;
私有列表_listDataHeader;//头标题
//标题标题、子标题格式的子数据
私有HashMap_listDataChild;
公共ExpandableListAdapter(上下文上下文、列表listDataHeader、HashMap listChildData){
这._context=context;
这。_listDataHeader=listDataHeader;
这。_listDataChild=listChildData;
}
@凌驾
公共对象getChild(int-groupPosition、int-ChildPosition){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.get(childpositionon);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getChildView(int groupPosition,final int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
最终字符串childText=(字符串)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.flate(R.layout.list_child,null);
}
TextView txtTitle=(TextView)convertView.findViewById(R.id.list\u child\u chkbox);
txtTitle.setText(childText);
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
Log.e(“groupPosition”、“kargr”+groupPosition);
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回此。\u listDataHeader.get(groupPosition);
}
@凌驾
public int getGroupCount(){
返回此值。_listDataHeader.size();
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共视图getGroupView(int-groupPosition,布尔值isExpanded,
视图(视图、视图组父级){
字符串头文件=(字符串)getGroup(groupPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_组,空);
}
视图ind=convertView.findViewById(R.id.explist\u指示符);
如果(ind!=null){
ImageView指示器=(ImageView)ind;
如果(getChildrenCount(groupPosition)==0){
指示器。设置可见性(视图。不可见);
}否则{
指示器.setVisibility(View.VISIBLE);
int stateSetIndex=(isExpanded?1:0);
Drawable Drawable=indicator.getDrawable();
drawable.setState(组状态集合[stateSetIndex]);
}
}
TextView lblistheader=(TextView)convertView
.findviewbyd(R.id.lblistheader);
lblListHeader.setTypeface(null,Typeface.BOLD);
lblListHeader.setText(标题);
返回视图;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
}
您的代码在我看来是正确的。工作正常吗?正确,但仅适用于“组指示器”。不适用于左侧的自定义图标和右侧的组指示器。。。