【问题标题】:Expandable list with different size dividers for parent and child具有不同大小分隔符的可扩展列表,适用于父母和孩子
【发布时间】:2015-07-26 01:16:28
【问题描述】:

我是 android 新手,这是我在这里的第一个问题。

http://s11.postimg.org/tjhfoq7mb/123.png

我正在寻找像上图这样的东西。

  1. 父/组有子 = 父/组没有分隔符
  2. 父/组没有子 = 父/组有分隔符
  3. 最后一个孩子 = 孩子有一个完整的分隔线
  4. 不是最后一个孩子 = 不是完整的分隔符

有没有可能做到这一点。是是的?怎么样?

【问题讨论】:

  • 可以添加图片兔子

标签: android


【解决方案1】:

是的,有可能

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    // First of all add the parent line as View in custom_expandable_parent_view.xml and reference it
    convertView = _lay.inflate(R.layout.custom_expandable_parent_view, null);
    View parentLine = convertView.findViewById(your_parent_line_id);

    if (isExpanded) {
        // Now if the your list is expanded you dont have to show it
        // so just hide it
        parentLine.setVisibility(GONE);
    }

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    // now in add both childLine and  parentLine in custom_expandable_child_view.xml
    // reference both of them

    convertView = _lay.inflate(R.layout.custom_expandable_child_view, null);
    View parentLine = convertView.findViewById(your_id);
    View childLine = convertView.findViewById(your_id);

    // consider currently both line are showing in everychild.

    if (isLastChild) {
        // now, if it is last child then we have to show parentLine that means a long line. So we only hide childLine
        childLine.setVisibility(View.GONE);
    } else {
        // now, if it is not last child then we have to show childLine that means a short line. for all child except the long one   
        // so here we hide the parent one
        parentLine.setVisibility(View.GONE);
    }

    return convertView;
}

这就是我所做的代码。 Bcoz 我也想要和你一样的布局。 我已经解释过了。。 因此,请根据您的要求进行更改。

【讨论】:

  • 这看起来更简单,我会试试这个并回复你。非常感谢
  • 我有些困惑,请您添加行项目的布局,以便让我了解如何绘制父行和子行。如果我是菜鸟,请原谅我。我是安卓新手
  • 对不起,我不知道怎么画这样的线。可以加一行布局代码吗?
  • 是的,我终于能够完成我想要的。非常感谢,您挽救了这一天。 -_-
【解决方案2】:

注意:下面的响应是针对不可展开的列表。

这绝对是可行的。您不仅可以为选项和标题创建行,还可以为分隔符创建行,并为这些分隔符行提供您想要的样式(高度 0.5dp,所需的边距)。

基本上,与https://stackoverflow.com/a/13634801/1816603 中建议的解决方案相同,但使用 4 种布局:2 个用于选项(用于父级和子级),2 个用于分隔符(父级分隔符和子级分隔符)。

为简单起见,项目类型和内容可以放在同一个字符串中,或​​者为了更优雅的解决方案,您可以创建一个包含行类型和行文本的类。

final ListView drawerList = (ListView) findViewById(R.id.left_drawer);

// Add options to the list drawer
final List<String>  listOptions = new ArrayList<>();

listOptions.add("1Parent 1");
listOptions.add("2Child 1");
listOptions.add("4");
listOptions.add("2Child 2");
listOptions.add("3");
listOptions.add("1Parent 2");
listOptions.add("3");
listOptions.add("1Parent 3");

drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item_parent, listOptions) {

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

    @Override
    public boolean isEnabled(int position)
    {
        String selected = listOptions.get(position);
        if ( (selected.charAt(0) == '3') || (selected.charAt(0) == '4') )
            return false;
        else return true;
    }

    @Override
    public View getView(int position, View coverView, ViewGroup vg) {
        LayoutInflater inflater = (LayoutInflater) parent.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int type = getItemViewType(position);
        if (type == 1) // parent
        {
            View rowView = inflater.inflate(R.layout.list_item_parent, vg, false);

            TextView text1 = (TextView) rowView.findViewById(R.id.list_item_text);
            text1.setText(listOptions.get(position).substring(1));

            return rowView;
        }
        else if (type == 2) // child
        {
            View rowView = inflater.inflate(R.layout.list_item_child, vg, false);

            TextView text1 = (TextView) rowView.findViewById(R.id.list_item_text);
            text1.setText(listOptions.get(position).substring(1));

            return rowView;
        }
        else if (type == 3) // parent separator
        {
            View rowView = inflater.inflate(R.layout.list_separator_parent, vg, false);
            return rowView;
        }
        else if (type == 4) // child separator
        {
            View rowView = inflater.inflate(R.layout.list_separator_child, vg, false);
            return rowView;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        String selected = listOptions.get(position);

        return Character.getNumericValue(selected.charAt(0)) - 1;
    }
});

布局将被定义为:

list_item_parent - Row with full width, for the text of the parent options
list_item_child  - Row with margin on the left, for the text of the parent options
list_separator_parent - Row with dark background, height 0.5dp and full width
list_separator_child - Row with dark background, height 0.5dp and margin on the left

【讨论】:

  • 谢谢,我现在有个主意了。好吧,我确实已经制作了数据结构,并认为我将使用 ExpandableListView。我现在必须改变一切 -_-
  • 哦,我没有看到问题中提到任何可扩展性。
  • 你应该看过标题,开玩笑的。
  • 对。好吧,无论如何你的问题已经解决了,这很好。
  • 感谢您的宝贵时间。我有一种感觉,您的解决方案将来会为我和其他人使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-15
相关资源
最近更新 更多