带有全选复选框的可扩展列表视图:组项目单击和滚动错误

2023-12-02

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

假设我有 4 个组,有 50 个孩子。当展开一个组时,我单击全选复选框,一切正常,所有复选框都被选中,并在滚动时保持其状态。

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

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

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

}

组站点.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();
            }
        });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有全选复选框的可扩展列表视图:组项目单击和滚动错误 的相关文章

  • 如何强制 Eclipse 将 xml 布局和样式显示为文本?

    我最近升级到带有 ADT 20 0 3 的 Eclipse 4 2 Juno 如果我查看旧项目中的布局或样式 Eclipse 只会向我显示其适当的基于控件的编辑器 我想编辑语法突出显示的 xml 文本 我没有找到将插件的编辑器切换到此模式的
  • 如何使用Android opencv使图像的白色部分透明

    我无法链接超过 2 个网址 因此我将我的照片发布到此博客 请在这里查看我的问题 http blog naver com mail1001 220650041897 http blog naver com mail1001 220650041
  • 从 Android 代码设置的 SECRET_CODE

    我知道如何使用清单文件中的秘密代码 它与此源代码配合良好
  • 安卓定位不准确

    我正在尝试获取当前用户的位置 我试图重构我的代码以获得更好的结果 但我只是不断得到关于准确度的荒谬位置 它在 900 600 米之间 如何才能得到更好的结果 使其精度达到50m以内 这是我的代码 package com agam mapsl
  • 播放 SoundCloud 曲目

    我可以在 Android 应用程序中播放 SoundCloud 中的曲目吗 我正在尝试这段代码 但它不起作用 String res https api soundcloud com tracks 84973999 stream client
  • 从 React Native Js 代码调用 Android Native UI 组件方法

    我创建了一个 CustomView SignatureView java 它扩展了 LinearLayout 以捕获 Android Native 中的签名 并创建了SignatureCapturePackage java和Signatur
  • 如何在android线性布局上获得阴影? [复制]

    这个问题在这里已经有答案了 可能的重复 如何在android中为View设置阴影 https stackoverflow com questions 4406524 how to set shadow to a view in androi
  • 使用 Retrofit2 和 Mockito 或 Robolectric 进行 Android 单元测试

    我可以测试 Retrofit2beta4 的真实响应吗 我需要 Mockito 或 Robolectic 吗 我的项目中没有活动 它将是一个库 我需要测试服务器是否正确响应 现在我有这样的代码并卡住了 Mock ApiManager api
  • 放置在 NavigationDrawer 顶部的片段

    我正在尝试添加一个PreferenceFragment在我的应用程序中 问题是 它自动放置在我的顶部NavigationDrawer public class SetPreferenceActivity extends Activity O
  • Android 应用程序中的 Eszett (ß)

    我的 res layout activity 文件中的德语 字符在我的应用程序中自动转换为 ss 即使我将语言和键盘设置为德语 它仍然不会显示 Android 中可以显示 吗 edit
  • 如何检查用户在EditText中输入自己的电话号码?

    用户将在我的 Android 应用程序的注册页面上的编辑文本中输入手机号码 如何检查用户输入的是他 她的手机号码而不是其他人的 我试过这个 TelephonyManager tMgr TelephonyManager mAppContext
  • Java 文件上传速度非常慢

    我构建了一个小型服务 它从 Android 设备接收图像并将其保存到 Amazon S3 存储桶中 代码非常简单 但是速度非常慢 事情是这样的 public synchronized static Response postCommentP
  • Android:滚动 Horizo​​ntalScrollView 时如何禁用 ScrollView 的垂直滚动?

    我正在开发一个带有带有 ScrollView 的 Activity 的 Android 应用程序 其中包含 Horizo ntalScrollView 等内容 当我触摸 Horizo ntalScrollView 时 我想禁用外部 Scro
  • 在 React Native 中调试应用程序崩溃

    我是 React Native 新手 我正在尝试安装 React Native Facebook SDK 以便我可以使用我的应用程序进行 Facebook 登录 我按照此处列出的步骤操作 https tylermcginnis com in
  • 在 Android 中调整可绘制对象的大小

    我正在为进度对话框设置一个可绘制对象 pbarDialog 但我的问题是我想每次调整可绘制的大小 但不知道如何调整 这是一些代码 Handler progressHandler new Handler public void handleM
  • HERE 地图:更改路线已行驶部分的颜色

    导航时可以改变路线的颜色吗 具体来说 我希望路线中已行驶的部分的颜色与即将行驶的部分的颜色不同 现在都是同一个颜色 将 MapRoute 对象的 TravelColor 变量设置为透明对我来说很有效 mapRoute color Resou
  • 按钮 - 单击时更改背景颜色

    我的活动中有 8 个按钮 我正在寻找的是 按钮具有默认背景 单击按钮时 背景颜色应更改为其他颜色 这部分非常简单 但是 当我单击任何其他按钮时 第一个按钮的背景颜色应该变回默认颜色 我知道这将使用 选择器状态 来完成 但我不太确定如何实现它
  • Android:监听状态栏通知

    有没有办法在状态栏被下拉时监听通知 1 用于检测状态栏变化 您可以注册一个监听器来获取系统UI可见性变化的通知 因此 要在您的活动中注册侦听器 Detecting if the user swipe from the top down to
  • 获取其他指针的MotionEvent.getRawX/getRawY

    我可以获取其他指针的MotionEvent getRawX getRawY 值吗 MotionEvent getRawX API 参考 http developer android com reference android view Mo
  • Android应用程序可以像旧的普通java小程序一样嵌入到网页中吗?

    我对 android 平台一无所知 也无法在互联网上找到这个基本问题的答案 更新 好的 我本身无法嵌入 Android 应用程序 但是我可以在 Android Webbrowser 中嵌入 Java 的东西吗 不可以 您无法将 Androi

随机推荐