草庐IT

android - 带有全选复选框的可扩展 ListView : group itemclick and scrolling bug

coder 2023-12-20 原文

我有一个 ExpandableListView,组名旁边有一个复选框,展开时,子项也有复选框。

假设我有 4 个小组,每个小组有 50 个 child 。当一个组展开时,我点击选择所有复选框,一切正常,所有复选框都被选中并在我滚动时保持它们的状态。

但是如果我一直滚动到列表的最后一个子项,之后滚动会出现错误(滚动到顶部然后触摸屏幕停止滚动不再起作用),点击组也不再起作用,直到我再次单击全选复选框。 单击子复选框不会执行任何操作,我必须单击全选复选框才能正常工作。

我改变了子复选框、组复选框的可聚焦状态并尝试了很多方法,但我找不到解决这个问题的方法。你知道它来自哪里吗?

public class ExpandableListAdapter extends BaseExpandableListAdapter {


    LayoutInflater cinflater;
    LayoutInflater ginflater;
    ArrayList<Group> array;
    Context context;


    public ExpandableListAdapter(Context context, ArrayList<Group> array) {
        super();

        cinflater = LayoutInflater.from(context);
        ginflater = LayoutInflater.from(context);

        this.array = array;
        this.context = context;

    }


    public class ChildViewHolder {

        CheckBox contactcheckbox;
        TextView name;

    }


    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return array.get(groupPosition).getContacts().size();
    }


    private OnCheckedChangeListener contactchecklistener = new OnCheckedChangeListener() {
               @Override
               public void onCheckedChanged(CompoundButton checkboxView, boolean isChecked) {

                   Contact contact = (Contact) checkboxView.getTag();
                   contact.setCheck(isChecked);

               }

            };

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

        ChildViewHolder cholder;

        if(convertView == null){

            cholder = new ChildViewHolder();

            convertView = cinflater.inflate(R.layout.childsitems, null);

            cholder.name = (TextView) convertView.findViewById(R.id.childnametextview);

            cholder.contactcheckbox = (CheckBox) convertView.findViewById(R.id.childcheckbox);
            cholder.contactcheckbox.setOnCheckedChangeListener(contactchecklistener);


            convertView.setTag(cholder);

        }else{

            cholder = (ChildViewHolder) convertView.getTag();

        }

        cholder.contactcheckbox.setTag(array.get(groupPosition).getContacts().get(childPosition));
        cholder.contactcheckbox.setChecked(array.get(groupPosition).getContacts().get(childPosition).isCheck());

        cholder.name.setText(array.get(groupPosition).getContacts().get(childPosition).getName());

        return convertView;
    }



    public class GroupViewHolder {
        TextView groupname;
        CheckBox groupcheck;

    }


    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return array.size();
    }

    private OnCheckedChangeListener groupchecklistener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            int index = (Integer) buttonView.getTag();

            for(int i = 0;i<array.get(index).getContacts().size();i++){
                array.get(index).getContacts().get(i).setCheck(isChecked);
            }

            array.get(index).setCheck(isChecked);

            notifyDataSetChanged();

        }
    };


    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {


        GroupViewHolder gholder;

        if(convertView==null){
            gholder =  new GroupViewHolder();
            convertView = ginflater.inflate(R.layout.groupsitems,null);

            gholder.groupname = (TextView)convertView.findViewById(R.id.groupnametextview);
            gholder.groupcheckbox = (CheckBox)convertView.findViewById(R.id.groupcheckbox);
            gholder.groupcheckbox.setOnCheckedChangeListener(groupchecklistener);

            convertView.setTag(gholder);

        }else{
            gholder = (GroupViewHolder)convertView.getTag();
        }

        gholder.groupname.setText(array.get(groupPosition).getGroupname());
        gholder.groupname.setTextSize(24);
        gholder.groupname.setTextColor(Color.parseColor("#FF858585"));
        gholder.groupname.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        int paddingleft = 48;  // 38 dps
        final float scale = getResources().getDisplayMetrics().density;
        int dpvalue = (int) (paddingleft * scale + 0.5f);
        gholder.groupname.setPadding(dpvalue, 16, 0, 16);


        gholder.groupcheckbox.setTag(groupPosition);
        gholder.groupcheckbox.setChecked(array.get(groupPosition).isCheck());

        if(isExpanded == true){
                  convertView.setBackgroundDrawable(getResources().getDrawable(R.drawable.expandedbackground));
        }else{
            convertView.setBackgroundColor(Color.TRANSPARENT);
        }

        return convertView;
    }





    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }

}

groupsitems.xml :

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <CheckBox
        android:id="@+id/groupcheckbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="12dp"
        android:focusable="false"
        android:gravity="right|center"
        android:paddingRight="10dp" />

<TextView
android:id="@+id/groupnametextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/groupcheckbox"
android:focusableInTouchMode="false"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF858585"
android:textStyle="bold" />

</RelativeLayout>

子项.xml :

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:paddingLeft="8dp" >


<TextView
        android:id="@+id/childnametextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/childcheckbox"
        android:gravity="right|center"
        android:paddingLeft="16dp"
        android:textColor="#FFAFAFAF"
        android:textSize="16sp"
        android:textStyle="bold" />

<CheckBox
        android:id="@+id/childcheckbox"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

最佳答案

我找到了解决方案。我没有在 groupcheckbox 上使用 OnCheckedChangeListener,而是使用了 OnClickListener,它解决了如下所有问题:

gholder.groupcheckbox.setChecked(array.get(groupPosition).isCheck());
gholder.groupcheckbox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                for(int i = 0;i<array.get(groupPosition).getContacts().size();i++){
                    array.get(groupPosition).getContacts().get(i).setCheck(gholder.groupcheckbox.isChecked());
                }

                array.get(groupPosition).setCheck(gholder.groupcheck.isChecked());

                notifyDataSetChanged();
            }
        });

关于android - 带有全选复选框的可扩展 ListView : group itemclick and scrolling bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13432660/

有关android - 带有全选复选框的可扩展 ListView : group itemclick and scrolling bug的更多相关文章

  1. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  2. c - mkmf 在编译 C 扩展时忽略子文件夹中的文件 - 2

    我想这样组织C源代码:+/||___+ext||||___+native_extension||||___+lib||||||___(Sourcefilesarekeptinhere-maycontainsub-folders)||||___native_extension.c||___native_extension.h||___extconf.rb||___+lib||||___(Rubysourcecode)||___Rakefile我无法使此设置与mkmf一起正常工作。native_extension/lib中的文件(包含在native_extension.c中)将被完全忽略。

  3. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  4. ruby-on-rails - 带有 Zeus 的 RSpec 3.1,我应该在 spec_helper 中要求 'rspec/rails' 吗? - 2

    使用rspec-rails3.0+,测试设置分为spec_helper和rails_helper我注意到生成的spec_helper不需要'rspec/rails'。这会导致zeus崩溃:spec_helper.rb:5:in`':undefinedmethod`configure'forRSpec:Module(NoMethodError)对thisissue最常见的回应是需要'rspec/rails'。但这是否会破坏仅使用spec_helper拆分rails规范和PORO规范的全部目的?或者这无关紧要,因为Zeus无论如何都会预加载Rails?我应该在我的spec_helper中做

  5. Ruby:如何使用带有散列的 'send' 方法调用方法? - 2

    假设我有一个类A,里面有一些方法。假设stringmethodName是这些方法之一,我已经知道我想给它什么参数。它们在散列中{'param1'=>value1,'param2'=>value2}所以我有:params={'param1'=>value1,'param2'=>value2}a=A.new()a.send(methodName,value1,value2)#callmethodnamewithbothparams我希望能够通过传递我的哈希以某种方式调用该方法。这可能吗? 最佳答案 确保methodName是一个符号,而

  6. ruby-on-rails - 向 Rails 3 添加 Ruby 扩展方法的最佳实践? - 2

    我有一个要在我的Rails3项目中使用的数组扩展方法。它应该住在哪里?我有一个应用程序/类,我最初把它放在(array_extensions.rb)中,在我的config/application.rb中我加载路径:config.autoload_paths+=%W(#{Rails.root}/应用程序/类)。但是,当我转到railsconsole时,未加载扩展。是否有一个预定义的位置可以放置我的Rails3扩展方法?或者,一种预先定义的方式来添加它们?我知道Rails有自己的数组扩展方法。我应该将我的添加到active_support/core_ext/array/conversion

  7. ruby-on-rails - 带有 Pry 的 Rails 控制台 - 2

    当我进入Rails控制台时,我已将pry设置为加载代替irb。我找不到该页面或不记得如何将其恢复为默认行为,因为它似乎干扰了我的Rubymine调试器。有什么建议吗? 最佳答案 我刚发现问题,pry-railsgem。忘记了它的目的是让“railsconsole”打开pry。 关于ruby-on-rails-带有Pry的Rails控制台,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/question

  8. 带有 attr_accessor 的类上的 Ruby instance_eval - 2

    我了解instance_eval和class_eval之间的基本区别。我在玩弄时发现的是一些涉及attr_accessor的奇怪东西。这是一个例子:A=Class.newA.class_eval{attr_accessor:x}a=A.newa.x="x"a.x=>"x"#...expectedA.instance_eval{attr_accessor:y}A.y="y"=>NoMethodError:undefinedmethod`y='forA:Classa.y="y"=>"y"#WHATTT?这是怎么回事:instance_eval没有访问我们的A类(对象)然后它实际上将它添加到

  9. ruby - 如何在 ruby​​ 中复制目录结构,不包括某些文件扩展名 - 2

    我想编写一个ruby​​脚本来递归复制目录结构,但排除某些文件类型。因此,给定以下目录结构:folder1folder2file1.txtfile2.txtfile3.csfile4.htmlfolder2folder3file4.dll我想复制这个结构,但不包含.txt和.cs文件。因此,生成的目录结构应如下所示:folder1folder2file4.htmlfolder2folder3file4.dll 最佳答案 您可以使用查找模块。这是一个代码片段:require"find"ignored_extensions=[".cs"

  10. ruby - 扩展类和实例 - 2

    这个问题有两个部分。在RubyProgrammingLanguage一书中,有一个使用模块扩展字符串对象和类的示例(第8.1.1节)。第一个问题。为什么如果您使用新方法扩展类,然后创建该类的对象/实例,则无法访问该方法?irb(main):001:0>moduleGreeter;defciao;"Ciao!";end;end=>nilirb(main):002:0>String.extend(Greeter)=>Stringirb(main):003:0>String.ciao=>"Ciao!"irb(main):004:0>x="foobar"=>"foobar"irb(main):

随机推荐