我试图在Android中实现3级ExpandableListView
。
它看起来像这样:
2级和3级应该有CheckBox
- es。当选择级别2中的CheckBox
时,也会选择级别3中的所有孩子。
我已经完成了2个适配器 - 都扩展了BaseExpandableListAdapter
。所以最后我得到2个插入ExpandableListView
- s - 一个用于等级1 - 等级2,另一个用于等级2 - 等级3。
此外,我有2个单独的布局 - 一个用于第一级,一个用于第2-3级(因为它们是相同的)。它s just a
的TextView with id inside
RelativeView`。
问题:
当我将TextView
更改为CheckBox
es并将文本设置为它们时,我无法将列表扩展到第三级。我不明白它为什么会发生以及如何处理它,有人可以帮助我吗?
如何从没有孩子的2级项目中隐藏指标(显示展开/折叠状态的箭头)?
我不知道为什么我的ExpandableListView
并不想垂直适合所有屏幕,它只消耗1/4。我试图改变CustomExpandableListView
中的一些参数,但它没有用。
这里有一些截图:
截屏1:
截屏2:
如您所见,当我扩展亚洲名单时,欧洲名单隐藏起来。
这是第1级的代码:
public class RegionsAdapter extends BaseExpandableListAdapter {
private List<Object> objects;
private Activity activity;
private LayoutInflater inflater;
public RegionsAdapter(Activity activity, List<Object> objects) {
this.objects = objects;
this.activity = activity;
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return objects.get(groupPosition).getObjects().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Object object = (Object) getChild(groupPosition, childPosition);
CustomExpandableListView subObjects = (CustomExpandableListView) convertView;
if (convertView == null) {
subObjects = new CustomExpandableListView(activity);
}
CountriesAdapter adapter = new CountriesAdapter(activity, object);
subObjects.setAdapter(adapter);
return subObjects;
}
@Override
public int getChildrenCount(int groupPosition) {
return objects.get(groupPosition).getObjects().size();
}
@Override
public Object getGroup(int groupPosition) {
return objects.get(groupPosition);
}
@Override
public int getGroupCount() {
return objects.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Object object = (Object) getGroup(groupPosition);
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_region_item, null);
}
TextView name = (TextView) convertView.findViewById(R.id.name);
name.setText(object.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
级别2-3的代码:
public class CountriesAdapter extends BaseExpandableListAdapter {
private Object object;
private LayoutInflater inflater;
private Activity activity;
public CountriesAdapter(Activity activity, Object object) {
this.activity = activity;
this.object = object;
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return object.getObjects().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Object object = (Object) getChild(0, childPosition);
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_country_item, null);
Resources r = activity.getResources();
float px40 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, r.getDisplayMetrics());
convertView.setPadding(
convertView.getPaddingLeft() + (int) px40,
convertView.getPaddingTop(),
convertView.getPaddingRight(),
convertView.getPaddingBottom());
}
TextView name = (TextView) convertView.findViewById(R.id.name);
name.setText(object.getName());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
if (object.getObjects() == null) {
return 0;
}
return object.getObjects().size();
}
@Override
public Object getGroup(int groupPosition) {
return object;
}
@Override
public int getGroupCount() {
return 1;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_country_item, null);
Resources r = activity.getResources();
float px20 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
convertView.setPadding(
convertView.getPaddingLeft() + (int) px20,
convertView.getPaddingTop(),
convertView.getPaddingRight(),
convertView.getPaddingBottom());
}
TextView name = (TextView) convertView.findViewById(R.id.name);
name.setText(object.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
我还有一些自定义的ExpandableListView:
public class CustomExpandableListView extends ExpandableListView {
public CustomExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
我的MainActivity代码:
public class MainActivity extends AppCompatActivity {
public static CustomExpandableListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Item> objectsLvl1 = new ArrayList<Item>();
List<Item> objectsAsia = new ArrayList<Item>();
List<Item> objectsEurope = new ArrayList<Item>();
List<Item> objectsChina = new ArrayList<Item>();
List<Item> objectsGermany = new ArrayList<Item>();
objectsLvl1.add(new Item("Asia", objectsAsia));
objectsLvl1.add(new Item("Europe", objectsEurope));
objectsAsia.add(new Item("China", objectsChina));
objectsAsia.add(new Item("Japan"));
objectsChina.add(new Item("Central", null));
objectsChina.add(new Item("North", null));
objectsChina.add(new Item("South", null));
RelativeLayout parent = (RelativeLayout) findViewById(R.id.parent);
list = new CustomExpandableListView(this);
RegionsAdapter regionsAdapter = new RegionsAdapter(this, objectsLvl1);
list.setAdapter(regionsAdapter);
if (parent != null) {
parent.addView(list);
}
}
}
答案 0 :(得分:0)
然而,最后,我已经弄明白了。这就是答案。
这个很简单。我已在OnClickListener
方法中将TextView
添加到getGroupView
:
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isExpanded) {
expandableListView.expandGroup(groupPosition);
} else {
expandableListView.collapseGroup(groupPosition);
}
}
};
holder.childName.setOnClickListener(clickListener);
holder.childIndicator.setOnClickListener(clickListener);
更换标准指标并添加我自己的指标更容易。然后,getGroupView
内部使用它。
首先,将默认groupIndicator
设置为null
:
expandableListView.setGroupIndicator(null);
使用自定义图像:
holder.childIndicator = (ImageView) convertView.findViewById(R.id.imgChildIndicator);
if (getChildrenCount(groupPosition) == 0) {
holder.childIndicator.setAlpha(0);
} else {
holder.childIndicator.setImageResource(isExpanded ? R.drawable.expanded : R.drawable.collapsed);
}
最后,隐藏所有Level 3项目的groupIndicator
:
holder.childIndicator.setAlpha(0);
对1级项目进行了相同的更改。
我已将ExpandableListView
放入RelativeLayout
activity_main
内。{/ p>
改变了 MainActivity:
中的实现ExpandableListView list = (ExpandableListView) findViewById(R.id.expandableListView_Parent);
if (list != null) {
ParentAdapter adapter = new ParentAdapter(this, objectsLvl1);
list.setAdapter(adapter);
}
这解决了我的问题,现在我有一个全屏ExpandableList
。