android - 在 ExpandableListView 中对子项使用两种不同的布局

标签 android expandablelistview

我正在尝试使 ExpandableListView 根据项目类型使用两种不同的布局。在这个时候我做了基本的功能,除了一件事: 在 listview 中选择组后,子项的文本显示错误(不同项的文本混合)

我的适配器源代码如下:

SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
            this,
            groupData,
            com.oleg.mart.foreign.R.layout.group_row,
            groupFrom,
            new int[] { com.oleg.mart.foreign.R.id.Group },
            childData,
            R.layout.simple_list_item_1,
            childFrom,
            childTo)
{
    @Override
    public int getChildTypeCount() {
        return 2;
    }

    /*@Override
    public int getChildrenCount(int groupPosition)
    {
        return childData.get(groupPosition).size();
    } */

    @Override
    public int getChildType(int groupPosition, int childPosition)
    {
        int result = 0;
        if (childPosition == getChildrenCount(groupPosition) - 1)
            result = 1;

        return result;
    }

    @Override
    public View getChildView (
            int groupPosition, int childPosition, 
            boolean isLastChild, 
            View convertView, ViewGroup parent)
    {
        if (convertView == null) {
            TextView textView = null;
            LayoutInflater inflater = (LayoutInflater)getSystemService(getBaseContext().LAYOUT_INFLATER_SERVICE);
            int itemType = getChildType(groupPosition,childPosition);
            String myText = childData.get(groupPosition).get(childPosition).get("chapter");

            switch (itemType) {
                case 0:
                    convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row_notlast, null);
                    textView = (TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.NChild);
                    textView.setPadding(30,20,30,20);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
                    textView.setText(Html.fromHtml(myText));
                    break;
                case 1:
                    convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row, null);
                    textView = (TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.Child);
                    textView.setPadding(30,20,30,20);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
                    textView.setText(Html.fromHtml(myText));
                    break;
            }
        }
        return convertView;
    }
};

如您所见,我正在尝试为最后一个子项设置不同的样式。有什么问题吗?

getChildView方法:

TextView textView = null;
String myText = null;
myText = childData.get(groupPosition).get(childPosition).get("chapter");

if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater)getSystemService(getBaseContext().LAYOUT_INFLATER_SERVICE);
    int itemType = getChildType(groupPosition,childPosition);


    switch (itemType) {
        case 0:
            convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row_notlast, null);
            break;
        case 1:
            convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row, null);
            break;
    }
}

textView = (TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.NChild);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setText(Html.fromHtml(myText));

return convertView;

最佳答案

我敢肯定,当 getChildView 方法上的 convertView != null 时,您不会返回正确的子值。 尝试根据您的条件设置您的值,就像这样:

TextView textView = null;

if (convertView == null) {
     LayoutInflater inflater = (LayoutInflater)getSystemService(getBaseContext().LAYOUT_INFLATER_SERVICE);
     int itemType = getChildType(groupPosition,childPosition);
     String myText = childData.get(groupPosition).get(childPosition).get("chapter");

     switch (itemType) {
         case 0:
             convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row_notlast, null);
             break;
         case 1:
             convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row, null);
             break;
    }
}

textView = (TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.NChild);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setText(Html.fromHtml(myText));

如果您对此仍有疑问,请对我的回答发表评论告诉我,我会看看我能做些什么。

关于android - 在 ExpandableListView 中对子项使用两种不同的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12286235/

相关文章:

android - 如何在更改文本输入时使用异步任务执行搜索服务

java - 我尝试将 ExpandableListView 与自定义适配器一起使用,但无法显示任何内容

Android:父 View 未收到触摸事件

android - Expandablelistview 子项上的 ImageButton

android - Lollipop 按钮上的自定义字体

android - 对于 BLE,从字符串转换为十六进制值意味着什么?

android - 有没有办法向用户显示输入文本的格式,例如 ___/__/__?

Android 使用 expandableListView 显示列表

java - 如何从 subview 中存在的按钮实现可扩展 ListView setOnItemClickListener?

c# - 手机游戏全局记分牌