父母和孩子的不同大小分隔符的可扩展列表

时间:2015-05-14 12:09:22

标签: android

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

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

我希望在上图中创建如下所示的内容。

  1. 父/组有子=父/组没有分隔符
  2. 父/组没有子=父/组有分隔符
  3. 最后一个孩子=孩子有一个完整的分隔符
  4. 不是最后一个孩子=不是一个完整的分隔者
  5. 是否有可能实现这一目标。是是的?如何?

2 个答案:

答案 0 :(得分:2)

是的,这是可能的

@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我也想要像你的一样布局。 我解释了.. 因此,请根据您的要求进行更改。

enter image description here

答案 1 :(得分:1)

注意:下面的回复是针对不可扩展的列表。

这绝对可行。您不仅可以为选项和标题创建行,还可以为分隔符创建行,并为这些分隔符行提供所需的样式(高度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