【问题标题】:Multi-level ExpandableListView with CheckBoxes in AndroidAndroid 中带有 CheckBoxes 的多级 ExpandableListView
【发布时间】:2016-09-04 02:12:21
【问题描述】:

我正在尝试在 Android 中实现 3 级 ExpandableListView

看起来像这样:

  • 1 级
    • 2 级
    • 2 级
      • 3 级
    • 2 级
  • 1 级
    • 2 级
    • 2 级

Level 2 和 Level 3 应该有CheckBox-es。而当第二层CheckBox被选中时,他在第三层的所有孩子也被选中。

我已经完成了 2 个适配器 - 都扩展了 BaseExpandableListAdapter。所以最后我插入了 2 个 ExpandableListView-s - 一个用于 Level 1 - Level 2,另一个用于 Level 2 - Level 3。

我还有 2 个单独的布局 - 一个用于 1 级,一个用于 2-3 级(因为它们是相同的)。 Its just aTextViewwith id insideRelativeView`.

问题:

  • 当我将 TextView 更改为 CheckBoxes 并将文本设置为它们时,我无法将列表扩展到第三级。我不明白它为什么会发生以及如何处理它,有人可以帮助我吗?

  • 如何从没有子级的 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);
    }
}
}

【问题讨论】:

    标签: android user-interface checkbox expandablelistview


    【解决方案1】:

    最后我想通了。这就是答案。

    • 用复选框扩展列表:

    这个很简单。我在getGroupView 方法中添加了OnClickListenerTextView

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

    最后,隐藏我所有 3 级项目的 groupIndicator

    holder.childIndicator.setAlpha(0);
    

    对 1 级项目进行了相同的更改。

    • 在屏幕上安装 ExpandableListView:

    我已将ExpandableListView 放入我的RelativeLayoutactivity_main

    MainActivity中改变了实现:

    ExpandableListView list = (ExpandableListView) findViewById(R.id.expandableListView_Parent);
    
        if (list != null) {
            ParentAdapter adapter = new ParentAdapter(this, objectsLvl1);
            list.setAdapter(adapter);
        }
    

    这解决了我的问题,现在我有一个全屏ExpandableList

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多