【问题标题】:How to set no indicator for the group having zero child in Expandable ListView?如何在可扩展列表视图中为具有零子项的组设置无指示符?
【发布时间】:2016-07-19 12:24:58
【问题描述】:

我想为具有零子项的组设置不指示符。我使用了可扩展列表视图。这是屏幕截图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;
    }


}

【问题讨论】:

    标签: java android expandablelistview expandablelistadapter


    【解决方案1】:

    我认为您在 GroupView 中添加了一个图标。然后,在getGroupView() 期间,您检查该组有多少子视图并决定是否显示“空”图标

    组视图布局:

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

    【讨论】:

      猜你喜欢
      • 2014-02-25
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多