【问题标题】:Add same clickable child to top of every group expandablelistview - android将相同的可点击子项添加到每个组的顶部 expandablelistview - android
【发布时间】:2016-09-18 20:14:05
【问题描述】:

好的,所以我一直在寻找,我已经能够将相同的子项添加到每个组,但我无法让它响应点击。现在它就像一个子标题,我需要它像其他子项一样。

目前我的列表是从位于另一个文件中的 HashMap 填充的。因为如果不完全重建它就无法添加到 HashMap 的中间,我认为最好的选择是在适配器中访问列表时将子节点添加到某个位置。我只是不确定在哪里或如何。

从 getChildView() 中可以看出,如果孩子的位置为 0,我会添加重复的子布局 (expandedListItemChildHeader)。我还为 getChildrenCount() 的大小添加了 1 个值。

这是我的适配器代码:

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;

public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
                                   HashMap<String, List<String>> expandableListDetail) {
    this.context = context;
    this.expandableListTitle = expandableListTitle;
    this.expandableListDetail = expandableListDetail;
}

@Override
public Object getChild(int listPosition, int expandedListPosition) {

    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .get(expandedListPosition);
}


@Override
public long getChildId(int listPosition, int expandedListPosition) {

    return expandedListPosition;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //if we're in the first position then add the list_item_header
    if(expandedListPosition == 0)
    {
        convertView = layoutInflater.inflate(R.layout.list_item_header, null);
        TextView expandedListTextView = (TextView) convertView.findViewById(R.id.expandedListItemChildHeader);
        expandedListTextView.setText("All Devices");

    }

    //otherwise add the list_item
    if (expandedListPosition > 0 && expandedListPosition < getChildrenCount(listPosition)) {
        final String expandedListText = (String) getChild(listPosition, expandedListPosition - 1);
        convertView = layoutInflater.inflate(R.layout.list_item, null);
        TextView expandedListTextView = (TextView) convertView.findViewById(R.id.expandedListItem);
        expandedListTextView.setText(expandedListText);
    }

    return convertView;
}

@Override
public int getChildrenCount(int listPosition) {
    //add a value of one to the size of the count of children per group to make
    //room for the child header

    return (this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .size() + 1);
}

@Override
public Object getGroup(int listPosition) {
    return this.expandableListTitle.get(listPosition);
}

@Override
public int getGroupCount() {
    return this.expandableListTitle.size();
}

@Override
public long getGroupId(int listPosition) {
    return listPosition;
}

@Override
public View getGroupView(int listPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String listTitle = (String) getGroup(listPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_group, null);
    }
    TextView listTitleTextView = (TextView) convertView
            .findViewById(R.id.listTitle);
    listTitleTextView.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
    listTitleTextView.setText(listTitle);

    //Use this conditional to change direction of group indicator icons
    //the ImageView in the xml layout is invisible by default.
    ImageView groupIndicatorView = (ImageView) convertView.findViewById(R.id.expand_GroupIndicator);
    if (getChildrenCount(listPosition) == 0 ) {
        groupIndicatorView.setVisibility( View.INVISIBLE );
    }
    else {
        groupIndicatorView.setVisibility( View.VISIBLE );
        groupIndicatorView.setImageResource( isExpanded ? R.drawable.ic_expand_less_black_24dp : R.drawable.ic_expand_more_black_24dp );
    }

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
    return true;
}

以下是我的 MainActivity 中的可扩展列表代码,它位于 onCreate() 方法中:(注意:下面示例代码顶部列出的变量在 onCreate() 方法之外,我只是将其包含在参考。

ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;


    expandableListView = (ExpandableListView) findViewById(R.id.navDrawer_userListView);
    expandableListDetail = ExpandableListDataPump.getData();
    expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
    expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
    expandableListView.setAdapter(expandableListAdapter);

    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        //initialize int var
        int previousGroup = 0;

        @Override
        public void onGroupExpand(int groupPosition) {
            //this conditional enables only one drop down group to open at a time
            if(groupPosition != previousGroup)
                expandableListView.collapseGroup(previousGroup);
            previousGroup = groupPosition;

            Toast.makeText(
                    getApplicationContext(),
                    expandableListTitle.get(groupPosition)
                            + " List Expanded.",
                    Toast.LENGTH_SHORT
            ).show();
        }
    });

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {

            Toast.makeText(getApplicationContext(),
                    expandableListTitle.get(groupPosition) + " List Collapsed.",
                    Toast.LENGTH_SHORT
            ).show();

        }
    });

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {

            v.setSelected(true);

            //Change title on toolbar to match group selection
            getSupportActionBar().setTitle(expandableListTitle.get(groupPosition) + "'s Devices");
            //Change subtitle on toolbar to match item selection
            //must offset child position by 1 to have room for child header
            getSupportActionBar().setSubtitle((String)expandableListAdapter.getChild(groupPosition, childPosition - 1));
            Toast.makeText(
                    getApplicationContext(),
                    expandableListTitle.get(groupPosition)
                            + " -> "
                            + expandableListDetail.get(
                            expandableListTitle.get(groupPosition)).get(
                            childPosition - 1), Toast.LENGTH_SHORT
            ).show();

            return false;
        }
    });

这是我的布局 从 list_item_header.xml 开始

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="7dp">

        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@null"
            android:id="@+id/toggle_allDevices"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:paddingLeft="2dp"
            android:tint="@color/drawerContent"/>

        <TextView
            android:id="@+id/expandedListItemChildHeader"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="@drawable/expandable_text_selector"
            android:paddingLeft="65dp"
            android:paddingTop="16dp"
            android:textSize="16dp"
            android:paddingBottom="16dp" />

    </RelativeLayout>

</LinearLayout>

这是我的 list_item.xml 布局

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="7dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@null"
            android:id="@+id/deviceTypeIcon"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:paddingLeft="14dp"
            android:tint="@color/drawerContent"/>

        <TextView
            android:id="@+id/expandedListItem"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="@drawable/expandable_text_selector"
            android:paddingLeft="65dp"
            android:paddingTop="16dp"
            android:textSize="16dp"
            android:paddingBottom="16dp" />

    </RelativeLayout>

</LinearLayout>

list_group.xml 布局

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

    <View
        android:layout_width="match_parent"
        android:layout_height="0.25dp"
        android:layout_alignParentTop="true"
        android:background="@color/drawerContent" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="7dp">

        <TextView
            android:id="@+id/listTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:textColor="@color/drawerContent"
            android:background="@color/drawerBg"
            android:paddingTop="16dp"
            android:textSize="16dp"
            android:paddingBottom="16dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/expand_GroupIndicator"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:src="@drawable/ic_expand_more_black_24dp"
            android:paddingRight="15dp"
            android:tint="@color/drawerContent"
            android:visibility="invisible"/>


    </RelativeLayout>

</LinearLayout>

最后是我的 NavigationView 的 sn-p 主要活动:

<android.support.design.widget.NavigationView
        android:background="@color/drawerBg"
        app:itemTextColor="@color/drawerContent"
        app:itemIconTint="@color/drawerContent"
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true">

        <ExpandableListView
            android:id="@+id/navDrawer_userListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@color/drawerContent"
            android:textColor="@color/drawerContent"
            android:background="@color/drawerBg"
            android:dividerHeight="@null"
            android:paddingTop="?attr/actionBarSize"
            android:layoutDirection="rtl"
            android:choiceMode="singleChoice"
            android:groupIndicator="@null">
        </ExpandableListView>

    </android.support.design.widget.NavigationView>

任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 编辑:好的,我很高兴报告我修复了可点击问题。我发现一个晦涩的帖子提到如果你有一个可点击的小部件,如按钮或切换/开关,它会阻止点击事件工作。简单的解决方案是将 android:focusable="false" 放在我的开关中。我现在唯一需要弄清楚的就是让新的孩子使用哈希图来匹配索引/位置与正确的项目。

标签: java android listview expandablelistview expandablelistadapter


【解决方案1】:

好的,我能够解决问题的最后一部分。事实证明,我所要做的就是在我的 onChildClick() 方法中设置一个条件,以使用以下代码调整我正在访问的索引:

//this conditional corrects for our new child header that is added in getChildView()
            String newChildSelect = "";

            if (childPosition == 0) {
                newChildSelect = "All Devices";
                getSupportActionBar().setSubtitle(newChildSelect);
            }
            else
            {
                newChildSelect = expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition - 1);
                //Change subtitle on toolbar to match item selection
                getSupportActionBar().setSubtitle((String)expandableListAdapter.getChild(groupPosition, childPosition - 1));
            }

【讨论】:

    猜你喜欢
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多