gpt4 book ai didi

java - 如何为自定义可扩展 ListView 子项设置选择器

转载 作者:太空狗 更新时间:2023-10-29 15:04:17 25 4
gpt4 key购买 nike

我有一个由两个自定义对象组成的自定义可扩展 ListView 。我想对列表的子项目产生点击效果,以便用户知道他们正在点击哪个项目。问题是,在创建自定义适配器后,我无法获得子点击效果。我的可绘制文件夹中有一个选择器,我正在将它分配给我的可扩展 ListView ,但由于某种原因它无法正常工作。谁能帮我解决这个问题。谢谢

ma​​in.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.grr.MainActivity"
tools:ignore="MergeRootFrame" >

<ExpandableListView
android:id="@+id/expandableListView1"
android:choiceMode="singleChoice"
android:layout_width="match_parent"
android:listSelector="@drawable/list_selector"
android:layout_height="wrap_content" >
</ExpandableListView>

</FrameLayout>

和我的selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/black"
android:state_pressed="true" />
<item android:drawable="@android:color/darker_gray"
android:state_checked="true" />
<item android:drawable="@android:color/white"
android:state_selected="true" />
</selector>

这是我的CustomAdapter

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<MainBodyArea> _listDataHeader;
private HashMap<MainBodyArea, List<SubBodyArea>> _listDataChild;

public ExpandableListAdapter(Context context, List<MainBodyArea> listDataHeader,
HashMap<MainBodyArea, List<SubBodyArea>> 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) {
SubBodyArea temp = this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition);
return temp.getID();
}

@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
SubBodyArea temp = (SubBodyArea) getChild(groupPosition, childPosition);
final String childText = temp.getName();

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_child, null);
}

TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);

txtListChild.setText(childText);
return convertView;
}

@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 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) {
MainBodyArea temp = (MainBodyArea) getGroup(groupPosition);
String headerTitle = temp.getName();
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_header, null);
}

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;
}
}

最佳答案

很简单
在您的 list_child 中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector"
android:orientation="vertical">

<!-- your rest of layout -->
.............
.............
<!-- your rest of layout -->
</LinearLayout>

关于java - 如何为自定义可扩展 ListView 子项设置选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23387805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com