繁体   English   中英

将相同的可点击子项添加到每个组可扩展列表视图的顶部-Android

[英]Add same clickable child to top of every group expandablelistview - android

好的,所以我一直在寻找范围,可以将相同的子项添加到每个组中,但是我无法使它响应点击。 现在,它的行为就像一个子标头,我需要它像其他子项一样。

目前,我的列表是从另一个文件中的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的片段:

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

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

好的,我能够解决问题的最后一部分。 事实证明,我要做的只是将条件放在我的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));
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM