【问题标题】:Using two different layouts for child items in ExpandableListView在 ExpandableListView 中为子项使用两种不同的布局
【发布时间】:2012-09-05 16:52:40
【问题描述】:

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

我的adapter源代码如下:

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;

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    我很确定当你的 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));
    

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

    【讨论】:

    • 非常感谢您提供快速优质的答复!
    • 这不会造成问题吗?如果 convertView 不为空怎么办?我们如何知道是哪个布局(假设在不同的布局中有不同的 ID)
    • 这取决于您拥有什么,但实际上,如果您管理具有不同内容的不同项目视图类型,则必须检查转换视图的状态。一种常见的方法是在 convertView 上使用“setTag”和“getTag”。在我的回答中,我希望在布局 com.oleg.mart.foreign.R.layout.child_row_notlast 和 com.oleg.mart 上都有 ID“com.oleg.mart.foreign.R.id.NChild”。 foreign.R.layout.child_row.
    • @Sebastien 这个解决方案在 ViewHolder 的情况下如何工作?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 2015-05-14
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 2015-08-05
      相关资源
      最近更新 更多