【问题标题】:ExpandableListView with multiple layouts具有多个布局的 ExpandableListView
【发布时间】:2015-02-16 19:27:07
【问题描述】:

我为我的英语道歉,但用谷歌翻译。我的申请需要帮助, 我正在使用具有 3 种不同布局的 ExpandableListView,但我遇到了在各种论坛上没有找到答案的问题。基本上,如果方法 getChildView () 检查 convertView 是否为空,我会偏移所有布局,甚至我会重复几次。如果我不进行该检查,您会看到应有的布局,但是如果我单击另一个组或如果您浏览列表直到不再显示带有 EditText 的布局,EditText 中的值将被删除。 我希望我足够清楚,我希望答案同样清楚,不幸的是我一直在互联网上,但找不到我正在寻找的答案。 提前感谢那些会帮助我的人

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

【问题讨论】:

    标签: android android-layout expandablelistview expandablelistadapter


    【解决方案1】:

    现在您的列表并不“知道”它具有三种不同类型的记录。您需要通过覆盖适配器的下一个函数来“告诉”它:

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

    您还需要重写 Adapter 的函数来确定要使用哪种类型的视图:

    @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,上述情况是正确的。

    至于ExpandableAdapters,它们的功能类似:

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

    您需要覆盖所有或其中一些,具体取决于您是否为组使用不同的布局。 至于您的情况,我建议您尝试以下方法:

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

    【讨论】:

    • 以防万一其他人在找不到方法时对此感到有些困惑,不是具有这些方法的 ExpandableListAdapter 接口,而是实现该接口的 BaseExpandableListAdapter 类,您可以扩展
    猜你喜欢
    • 1970-01-01
    • 2018-11-18
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多