如何实现Expandablelistview子级自定义布局? [英] How to achieve Expandablelistview child custom layout?

查看:224
本文介绍了如何实现Expandablelistview子级自定义布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在android中实现这一目标.我有这样的布局:

I can't figure out how to achieve this in android. I have a layout like this:

我想要这样,以便在单击加号按钮时它将显示完整的布局,如下所示:

I want it such that on click of the plus button it will display the full layout like this:

此布局是expandablelistview项的子级.因此,单击expandablelistview项时,它会显示该类别中的项短暂丢失.然后,如果用户想查看全部内容,则将单击加号按钮. 任何帮助将不胜感激.

This layout is the child of an expandablelistview item. So when the expandablelistview item is clicked it shows a short lost of items in that category. Then, if the user wants to see all, he will click the plus button. Any help will be appreciated.

推荐答案

创建一个名为expandable_list_layout

Create an XML layout with the name expandable_list_layout

<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并覆盖

Create a custom adapter. Let`s call it InboxExpandableListAdapter. This class should extend BaseExpandableListAdapter and override

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

如果您在此处查看,则重写方法带有两个参数,即组位置和子位置.这些建议我们应该为适配器提供某种类型的列表,其中必须包含组及其子级.我正在为该类创建一个构造函数

If you look here, the overriden method takes two arguments i.e., a groupposition and a childposition. These suggest that we should supply the adapter with some kind of list which must contain groups and their child. I am creating a constructor for the class

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会填充每个项目的视图.因此,在这些方法中,为您要显示的组及其各自的子级增加布局

Now getChildView method and getGroupView populates the view for each item. So inside these methods, inflate the layout which you want to show for group and their respective child

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

这篇关于如何实现Expandablelistview子级自定义布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆