如何在Expandable ListView中为没有子项的组设置任何指示器?

时间:2016-07-19 12:24:59

标签: java android expandablelistview expandablelistadapter

我想为没有孩子的组设置任何指示器。我使用了可扩展列表视图。这是截图Initial layout。最初没有孩子的组没有指示。但是一扩展我的如果有一定数量的孩子,那么没有孩子的群体也会开始显示该指标。

MainActivity.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        android:clickable="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

    <ExpandableListView
        android:id="@+id/exp_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="160dp"
        android:indicatorRight="?android:attr/expandableListPreferredItemIndicatorRight"
        android:background="@android:color/white"
        android:footerDividersEnabled="true"
        android:groupIndicator="@null">
    </ExpandableListView>

            <!--<ListView-->
                <!--android:background="@android:color/white"-->
                <!--android:id="@+id/menuList"-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="match_parent"-->
                <!--android:layout_marginTop="10dp" />-->
        </LinearLayout>
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

Listheader.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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:orientation="horizontal">

      <ImageView
            android:id="@+id/iconimage"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:padding="10dp"/>

        <TextView
            android:id="@+id/submenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textColor="#000000"
            android:textSize="16sp"
            android:gravity="center_vertical" />


    </LinearLayout>

</LinearLayout>

ExpandableListAdapter.java

   package com.global.market;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Samarth on 17-Jul-16.
 */
public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<ExpandedMenuModel> mListDataHeader; // header titles

    // child data in format of header title, child title
    private HashMap<ExpandedMenuModel, List<String>> mListDataChild;
    ExpandableListView expandList;

    public ExpandableListAdapter(Context context, List<ExpandedMenuModel> listDataHeader,
                                 HashMap<ExpandedMenuModel, List<String>> listChildData, ExpandableListView mView) {
        this.mContext = context;
        this.mListDataHeader = listDataHeader;
        this.mListDataChild = listChildData;
        this.expandList = mView;
    }

    @Override
    public int getGroupCount() {
        int i = mListDataHeader.size();
        Log.d("GROUPCOUNT", String.valueOf(i));
        return this.mListDataHeader.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int childCount = 0;
        if (groupPosition<2) {
            childCount = this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
                    .size();
        }
        return childCount;
    }

    @Override
    public Object getGroup(int groupPosition) {
        Log.d("Group",groupPosition+"");
        return this.mListDataHeader.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
//        Log.d("CHILD", mListDataChild.get(this.mListDataHeader.get(groupPosition))
//                .get(childPosition).toString());
        return this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
                .get(childPosition);
    }

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

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ExpandedMenuModel headerTitle = (ExpandedMenuModel) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.listheader, null);
        }
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.submenu);
        ImageView headerIcon = (ImageView) convertView.findViewById(R.id.iconimage);
      //  lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle.getIconName());
        headerIcon.setImageResource(headerTitle.getIconImg());

        if (isExpanded &&  getChildrenCount(groupPosition)>0) {

            lblListHeader.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                    R.drawable.collapse, 0);
        }
        else if(getChildrenCount(groupPosition)>0){
            // If group is not expanded then change the text back into normal
            // and change the icon

            lblListHeader.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                    R.drawable.expand, 0);
        }
        return convertView;
    }

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

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

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

        txtListChild.setText(childText);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


}

1 个答案:

答案 0 :(得分:0)

我认为你在GroupView中添加了一个Icon。然后,在getGroupView()期间,您可以检查该组拥有的子视图的数量,并决定是否显示&#34;空&#34;图标

组视图布局:

<LinearLayout>
    <LinearLayout
        ...>
      <ImageView
            .../>
        <TextView
            ... />

      <ImageView> 
        <!-- IMAGEVIEW ICON TO SHOW THIS GROUP IS EMPTY -->
        android:id="@+id/empty_group_icon"
        android:visibility="invisible"
      </ImageView>

      </LinearLayout>
</LinearLayout>

getGroupView()

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    ...
    if(getChildrenCount(groupPosition) > 0)
        convertView.findViewById(R.id.empty_group_icon).setVisibility(View.INVISIBLE);
    else
        convertView.findViewById(R.id.empty_group_icon).setVisibility(View.VISIBLE);

    return convertView;
}