如何实现Expandablelistview子自定义布局?

时间:2015-10-07 08:01:46

标签: android android-layout xamarin xamarin.android expandablelistview

我无法弄清楚如何在android中实现这一点。我有这样的布局:

enter image description here

我想这样,点击加号按钮就会显示完整的布局:

enter image description here

此布局是expandablelistview项的子项。因此,当单击expandablelistview项时,它会显示该类别中项目的短缺。然后,如果用户想要查看全部,他将单击加号按钮。 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

创建名为expandable_list_layout

的XML布局
<ExpandableListView
    android:id="@+id/data_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_below="@id/dynamicHeadingTxt"
    android:layout_marginTop="3dp"
    android:layout_weight="8"
    android:cacheColorHint="#00ffffff"
    android:childDivider="@drawable/child_separator"
    android:divider="@drawable/child_separator"
    android:groupIndicator="@null" >
</ExpandableListView>

创建自定义适配器。我们称之为InboxExpandableListAdapter。此类应扩展BaseExpandableListAdapter并覆盖

public class ada extends BaseExpandableListAdapter{

@Override
public Object getChild(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return false;
}
}

如果你看这里,overriden方法有两个参数,即groupposition和childposition。这些建议我们应该为适配器提供某种必须包含组及其子组的列表。我正在为类

创建一个构造函数
public InboxExpandableListAdapter(Activity context, List<String> data, List<String> data_secondry,
        List< List<String>> dataCollections) {
    this.context = context;
    this.dataCollections = dataCollections;
    this.data = data;
    this.data_secondry = data_secondry;
}

现在,getChildView方法和getGroupView填充每个项目的视图。因此,在这些方法中,要为组及其各自的子项展示要显示的布局

public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    final String data = (String) getChild(groupPosition, childPosition);
    LayoutInflater inflater = context.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.child_item, null);
    }

    TextView item = (TextView) convertView.findViewById(R.id.data);

    item.setText(data);
    return convertView;
}

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

    String laptopName = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.group_item,
                null);
    }

    TextView item = (TextView) convertView.findViewById(R.id.data);
    item.setTypeface(null, Typeface.BOLD);
    item.setText(laptopName);
    return convertView;
}

答案 1 :(得分:0)

CoordinatorLayout是为这种情况设计的东西。看看Android Developers blog

中的示例

或者,您可能有两个布局分组 - 一个是最上面的行,另一个是 - 下面的其他行。然后处理“+”按钮Click事件,您将在其上显示后者(可能的动画)。

我会继续使用CoordinatorLayout,因为这是更好的方法,但请随意尝试两者,看看哪种方式更适合你。