具有多种布局的ExpandableListView [英] ExpandableListView with multiple layouts

查看:263
本文介绍了具有多种布局的ExpandableListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的英语致歉,但已通过Google翻译.我需要我的申请帮助, 我正在使用具有3种不同布局的ExpandableListView,但是遇到了在各种论坛上都找不到答案的问题.基本上,如果方法getChildView()检查convertView是否为null,则我偏移所有布局,甚至重复几次.如果我没有进行检查,那么您会看到应有的布局,但是如果我单击另一个组或浏览列表直到不再显示带有EditText的布局,则EditText中的值将被删除. 我希望我已经足够清楚,也希望答案也一样清晰,但不幸的是,我已经遍及整个互联网,但是找不到我想要的答案. 在此先感谢那些会帮助我的人

I apologize for my English but translated with google. I need help for my application, I'm using a ExpandableListView with 3 different layouts but I'm having problems that I have not found an answer on various forums. Basically if the method getChildView () check if the convertView is null, I offsets all layouts or even me repeat them several times. If I do not make that check, you see the layout as it should, but the values in the EditText erased if I click on another group or if you skim the list until no more to show the layout with the EditText. I hope I was clear enough and I hope that answers are equally clear, unfortunately I have been all over the internet but could not find the answer I was looking for. Thanks in advance to those who will help me

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

            View view = convertView;
            ViewHolder holder;

            if (view == null) {
                    holder = new ViewHolder();

                    if (groupPosition == 0) {
                            convertView = infalInflater.inflate(R.layout.adv_item, null);
                            holder.btn1 = (ImageButton) convertView
                                            .findViewById(R.id.button1);
                            holder.btn2 = (ImageButton) convertView
                                            .findViewById(R.id.button2);
                            holder.btn3 = (ImageButton) convertView
                                            .findViewById(R.id.button3);
                            holder.et1 = (EditText) convertView.findViewById(R.id.editText1);
                            holder.et2 = (EditText) convertView.findViewById(R.id.editText2);
                            holder.et3 = (EditText) convertView.findViewById(R.id.editText3);
                            holder.et4 = (EditText) convertView.findViewById(R.id.editText4);
                            holder.et5 = (EditText) convertView.findViewById(R.id.editText5);
                            holder.et6 = (EditText) convertView.findViewById(R.id.editText6);
                            holder.check_sc1 = (CheckBox) convertView
                                            .findViewById(R.id.item_check_sc1);
                            holder.check_sc2 = (CheckBox) convertView
                                            .findViewById(R.id.item_check_sc2);
                            holder.check_sc3 = (CheckBox) convertView
                                            .findViewById(R.id.item_check_sc3);
                            holder.check_oo = (CheckBox) convertView
                                            .findViewById(R.id.item_check_oo);
                            convertView.setTag(holder);
                    }
                    if (groupPosition == 1) {
                            convertView = infalInflater.inflate(R.layout.visual_item, null);
                    }
                    if (groupPosition == 2) {
                            convertView = infalInflater.inflate(R.layout.routes_item, null);
                    }
            } else {
                    holder = (ViewHolder) convertView.getTag();
            }
            return convertView;
    }

推荐答案

现在,您的列表不知道"它具有三种不同类型的记录.您需要通过重写适配器的下一个功能来告诉"它:

Right now your list doesn't "know" that it has three different types of records. You need to "tell" it that by overriding the next function of the Adapter:

@Override
public int getViewTypeCount() {
    return 3;     // <-- Here goes your view types count
}

此外,您还需要覆盖适配器的功能以找出要使用的视图类型:

Also you need to override the Adapter's function to figure out which type of view to use:

@Override
public int getItemViewType(int position) {
    return getItemViewType(position);  // <-- Here must be your logic to get the type of the item
}

因此,现在Android可以使用三种类型的列表项.现在,您不需要'if(groupPosition == 2)'(等等),因为ListView将使用这些函数来确定正确的布局,以传递到适配器的'getView()'函数中. 上面对于简单的ListAdapters 是正确的.

So now Android has a pool of three types of list items you can use. And now you dont need 'if (groupPosition == 2)' (and etc.) because a ListView will use those functions to figure the right layout to pass into adapter's 'getView()' function. The above is true for simple ListAdapters.

对于 ExpandableAdapters ,它们具有相似的功能:

As for ExpandableAdapters they have similar functions:

public int getGroupTypeCount ()
public int getGroupType (int groupPosition)
public int getChildType (int groupPosition, int childPosition)
public int getChildTypeCount ()

您需要覆盖全部或其中一部分,这取决于您是否对组使用不同的布局. 至于您的情况,我建议您尝试以下方法:

You need to override all of them, or some of them, depending whether you use different layouts for groups or not. As for your case, i suggest to try something like:

@Override
public int getChildTypeCount () {
    return 3;   <-- you have three types of views
}

@Override
public int getChildType (int groupPosition, int childPosition) {
    /*
     * In case of group 1 this function returns 1,
     * in case of group 2 it returns 2, and so on.
     */
    return groupPosition;
}

这篇关于具有多种布局的ExpandableListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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