【问题标题】:Android expandable list: groups with different layoutsAndroid 可扩展列表:具有不同布局的组
【发布时间】:2012-09-07 08:52:15
【问题描述】:

我想为每个组显示不同的子布局,例如:

@Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {

            if (convertView == null) {

                switch (groupPosition){

                case 0:
                    LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.child_row, null);
                    TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
                    tvPlayerName.setText(arrChildelements[groupPosition][childPosition]);
                    break;

                case 1:
                    LayoutInflater inflater1 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater1.inflate(R.layout.child_row1, null);
                    TextView tvPlayerName1 = (TextView) convertView.findViewById(R.id.tvPlayerName);
                    tvPlayerName1.setText(arrChildelements[groupPosition][childPosition]);
                    break; 

                case 2:
                    LayoutInflater inflater2 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater2.inflate(R.layout.child_row2, null);
                    TextView tvPlayerName2 = (TextView) convertView.findViewById(R.id.tvPlayerName);
                    tvPlayerName2.setText(arrChildelements[groupPosition][childPosition]);
                    break;

                case 3:             
                    LayoutInflater inflater3 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater3.inflate(R.layout.child_row3, null);
                    TextView tvPlayerName3 = (TextView) convertView.findViewById(R.id.tvPlayerName);
                    tvPlayerName3.setText(arrChildelements[groupPosition][childPosition]);
                    break;

                }


            } return convertView;
    }

我的问题是在点击组时,子列表在组之间互换。

谁能告诉我我做错了什么?

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    因为

    if (convertView == null)
    

    删除它并且它应该可以正常工作,如果在删除此 if 语句后它可以正常工作,则返回它并在 if (convertView == null) 中添加 if 语句以使其显示为您想要的,因为删除 if ( convertView == null) 会使滚动速度变慢并使用更多内存。

    例如,您可以将所有视图放在一个 xml 文件或一个视图中,然后在 switch 方法中只使用 setVisibility 显示适合当前列表项的布局部分。

    【讨论】:

    • 删除 if (convertView == null) 后,它按预期工作。我不明白,我应该在 if (convertView == null) 里面添加什么,以使其使用更少的内存。
    • 我必须展示 7 种不同的布局,很难将所有视图放在一个 xml 中。
    【解决方案2】:

    如果(convertView == null)

    从子视图的自定义适配器中删除它

    它对所有人都很好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 2016-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多