在ExpandableListView中使用两个不同的子条目布局

时间:2021-08-24 10:22:57

I'm trying to do the ExpandableListView to use two different layouts depending on item type. At this time I made basic functionality except one thing: after selecting a group in the listview, the child item's text is displayed wrong (text of different items is mixed)

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

My adapter source code is following:

我的适配器源代码如下:

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

As you can see I'm trying to set a different style on the last child item. What could be wrong?

正如您所看到的,我试图在最后一个子项目上设置不同的样式。可能是错的呢?

Right getChildView method:

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

2 个解决方案

#1


6  

I'm pretty sure that you are not returning the good child values when convertView != null on your getChildView method. Try to set your values after your condition, like that :

我很确定在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));

If you still have a problem with that, please put a comment on my answer to tell me and I will see what I can do.

如果你还是有问题,请在我的回答上留言告诉我,我看我能做什么。

#2


1  

Use HeterogeneousExpandableList . (https://developer.android.com/reference/android/widget/HeterogeneousExpandableList.html) It provides feature to have different header/child views in expandable list view.and also maintains its reuse.

使用HeterogeneousExpandableList。(https://developer.android.com/reference/android/widget/hetergeneousexpandablelist.html)它提供了在可扩展列表视图中具有不同头/子视图的特性。并保持其重用性。

#1


6  

I'm pretty sure that you are not returning the good child values when convertView != null on your getChildView method. Try to set your values after your condition, like that :

我很确定在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));

If you still have a problem with that, please put a comment on my answer to tell me and I will see what I can do.

如果你还是有问题,请在我的回答上留言告诉我,我看我能做什么。

#2


1  

Use HeterogeneousExpandableList . (https://developer.android.com/reference/android/widget/HeterogeneousExpandableList.html) It provides feature to have different header/child views in expandable list view.and also maintains its reuse.

使用HeterogeneousExpandableList。(https://developer.android.com/reference/android/widget/hetergeneousexpandablelist.html)它提供了在可扩展列表视图中具有不同头/子视图的特性。并保持其重用性。