簡體   English   中英

如何為自定義可展開列表視圖子項設置選擇器

[英]How to set selector for custom expandable listview child

我有一個由兩個自定義對象組成的自定義可擴展列表視圖。 我希望對列表的子項具有單擊效果,以便用戶知道他們正在單擊哪個項目。 問題是,在創建自定義適配器后,我無法獲得點擊效果。 我在drawable文件夾中有一個選擇器,我將它分配給我的可擴展列表視圖,因為某些原因它無法正常工作。 任何人都可以幫我解決這個問題。 謝謝

main.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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM