我在android中创建了一个ExpandableListView
。我有一些类别有更多的子类别。而其他没有子类别。
如果图标没有子类别,我怎么能删除它?
E.g。我有一个类别如Home,sarees等。在Home中,没有子类别,所以我需要删除可扩展的展开图标。
这是content_home.xml
ExpandableListView
<?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:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.domore.onlineangelnx.Home"
tools:showIn="@layout/activity_home">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ExpandableListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"/>
<!--android:choiceMode="singleChoice"-->
</android.support.v4.widget.DrawerLayout>
以下是我的drawable_list_item.xml
,TextView
显示ExpandableListView
标题
<?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">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@color/list_item_title"
android:gravity="center_vertical" />
</LinearLayout>
这是我的ExpandableListViewAdapter
,用于计算项目及其子项目
package adapter;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.domore.onlineangelnx.R;
import java.util.HashMap;
import java.util.List;
/**
* Created by MY WORLD on 10/27/2015.
*/
public class ExpandableListAdapter extends BaseExpandableListAdapter {
Context context;
List<String> listDataHeader;
HashMap<String,List<String>> listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild){
this.context=context;
this.listDataHeader=listDataHeader;
this.listDataChild=listDataChild;
}
@Override
public Object getChild(int i, int i1) {
return this.listDataChild.get(this.listDataHeader.get(i)).get(i1);
}
@Override
public int getGroupCount() {
return this.listDataHeader.size();
}
@Override
public int getChildrenCount(int i) {
return this.listDataChild.get(this.listDataHeader.get(i)).size();
}
@Override
public Object getGroup(int i) {
return this.listDataHeader.get(i);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
String headerTitle=(String)getGroup(i);
if(view==null){
LayoutInflater inflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.drawer_list_item,null);
}
TextView lblListHeader=(TextView)view.findViewById(R.id.title);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return view;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
final String childText=(String)getChild(i,i1);
if(view==null){
LayoutInflater inflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.child_tems,null);
}
TextView txtListChild=(TextView)view.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
这是我的Home.java(MainActivity.java),它设置List<String>
和HashMap<String,List<String>>
的参数
package com.domore.onlineangelnx;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ExpandableListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import MainFragments.HomeFragment;
import MainFragments.SareesFragment;
import adapter.ExpandableListAdapter;
public class Home extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private ExpandableListAdapter adapter=null;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] navMenuItems;
private List<String> listDataHeader=null;
private HashMap<String,List<String>> listDataChild=null;
private ExpandableListView mDrawerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mTitle=mDrawerTitle=getTitle();
navMenuItems=getResources().getStringArray(R.array.nav_drawer_item);
mDrawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList=(ExpandableListView)findViewById(R.id.list_slidermenu);
//Setting adapter
prepareListData();
adapter=new ExpandableListAdapter(getApplicationContext(),listDataHeader,listDataChild);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return groupPosition == 0;
}
});
mDrawerList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Fragment fragment = null;
switch (groupPosition) {
case 0:
fragment = new HomeFragment();
break;
case 1:
switch (childPosition) {
case 0:
fragment=new SareesFragment();
break;
}
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
mDrawerList.setItemChecked(childPosition, true);
mDrawerList.setSelection(childPosition);
setTitle(navMenuItems[childPosition]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
return false;
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.app_name,R.string.app_name){
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
//displayView(0);
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
private void prepareListData(){
listDataHeader=new ArrayList<>();
listDataChild=new HashMap<>();
listDataHeader.add(navMenuItems[0]);
listDataHeader.add(navMenuItems[1]);
listDataHeader.add(navMenuItems[2]);
listDataHeader.add(navMenuItems[3]);
listDataHeader.add(navMenuItems[4]);
listDataHeader.add(navMenuItems[5]);
listDataHeader.add(navMenuItems[6]);
List<String> home=new ArrayList<>();
//Sub-Menu For SAREES
List<String> sarees=new ArrayList<>();
sarees.add("a");
sarees.add("b");
sarees.add("c");
sarees.add("d");
listDataChild.put(listDataHeader.get(1),sarees);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
int id = item.getItemId();
switch (id){
case R.id.action_settings:
return true;
case R.id.action_search:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private void displayView(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment=new HomeFragment();
break;
case 1:
fragment=new SareesFragment();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager=getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuItems[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
try {
getSupportActionBar().setTitle(mTitle);
}
catch (NullPointerException e){
e.printStackTrace();
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
请帮我解决这个问题。
答案 0 :(得分:2)
首先,删除expandable listview的默认图标
<ExpandableListView [...]
android:groupIndicator="@null" />
并且在getGroupView
方法的Expandable listview适配器中使用以下代码
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.YOUR_LIST_GROUP_XML, null);
}
TextView header_tv = (TextView) convertView.findViewById(R.id.list_header_tv);
ImageView arrow_iv =(ImageView) convertView.findViewById(R.id.arrow_iv);
header_tv.setText(headerTitle);
Boolean check = false;
try {
YOUR_HAS_MAP_LIST.get(this._listDataHeader.get(groupPosition))
.get(0);
}
catch (Exception ex)
{
check = true;
ex.printStackTrace();
}
if(check)
{
arrow_iv.setVisibility(View.INVISIBLE);
}
if(isExpanded)
{
arrow_iv.setImageResource(R.drawable.UP_ARROW);
}
else
{
arrow_iv.setImageResource(R.drawable.DOWN_ARROW);
}
return convertView;
}
答案 1 :(得分:0)
上述答案中存在单一问题。当您展开类别时,其他类别的图标也会被禁用。因为,在第一次代码尝试时,它不会超出try,直到你的listdatachild.get(listdataheader.get(i))。get(0)找到。所以,它不会每次都在(检查)。因此,图标被隐藏了。
以下是完美的解决方案
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.YOUR_LIST_GROUP_XML, null);
}
TextView header_tv = (TextView) convertView.findViewById(R.id.list_header_tv);
ImageView arrow_iv =(ImageView) convertView.findViewById(R.id.arrow_iv);
header_tv.setText(headerTitle);
Boolean check = false;
try {
if(YOUR_HAS_MAP_LIST.get(this._listDataHeader.get(groupPosition))
.get(0)==null){}
else{
if (isExpanded) {
arrow_iv.setImageResource(R.drawable.arrow_up);
} else {
arrow_iv.setImageResource(R.drawable.arrow_down);
}
}
}
catch (Exception ex)
{
check = true;
ex.printStackTrace();
}
if(check)
{
arrow_iv.setVisibility(View.INVISIBLE);
}
else{
arrow_iv.setVisibility(View.VISIBLE);
}
return convertView;
}