Android的可扩展列表:用不同的布局组 [英] Android expandable list: groups with different layouts

查看:104
本文介绍了Android的可扩展列表:用不同的布局组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to display different child layout for each group like:

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

我的问题是,而在群体点击,孩子名单群体之间的相互变化。

My problem is while clicking on the groups, the child list is inter changing between groups.

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

Can anybody tell me that what i am doing wrong?

推荐答案

这是因为

if (convertView == null)

删除它,它应该正常工作,如果它的工作,因为它应该if语句删除此之后,返回并添加if语句里面,如果(convertView == NULL),使它看起来,你想要它,因为如果去掉( convertView == NULL)将使滚动慢得多,使用了大量的更多的RAM。

remove it and it should work fine, if it worked as it should after removing this if statement, return it and add if statements inside if (convertView == null) to make it appear as you want it, since removing if (convertView == null) will make scrolling much slower and uses a lot more ram.

例如你可以把在一个XML文件中或者在一个视图中的所有意见,然后切换方法中只使用setVisibility显示适合当前列表项布局的一部分。

for example you can put all the views in one xml files or in one view, and then inside the switch method just use setVisibility to show the part of the layouts that suits the current list item.

这篇关于Android的可扩展列表:用不同的布局组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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