如何选中/取消选中 ExpandableListView 内的 CheckedTextView(子项)项?

2024-05-04

我的设计 :我创建了一个自定义适配器(SignalsExpandableListAdapter)与我的 ExpandableListView 的 CheckedTextView 。

public class SignalsExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHandler;
private HashMap<String,List<String>> listHashMap;

public SignalsExpandableListAdapter(Context context, List<String> listDataHandler, HashMap<String, List<String>> listHashMap) {
    this.context = context;
    this.listDataHandler = listDataHandler;
    this.listHashMap = listHashMap;
}

然后数据用 HashMap 和 List 填充到片段内,如下所示。这效果很好。我可以在 ExpandableListView 中看到子元素和父元素。

ArrayList<List<String[]>> deviceMList = new ArrayList<>();
final List<String[]> mList = new ArrayList<>();
deviceMList.add(mList);

我正在寻找什么:当我选择任何子项(可以是多个)时,我想使用 CheckedTextView 用勾号指示该选择。当我选择任何选中的子项目时,我也想取消选中。 (简单的选中/取消选中设计)。当选择任何子项时,也会调用另一个函数来进行绘图。这就是我被困住的地方。每次当我选择一个孩子时,随机多个孩子的checkedTextViews也会被指示为已选中,而不仅仅是我选择的那个。

经过一番搜索后,我尝试了 3 次尝试来克服这个问题,并只检查我选择的孩子。但似乎我的两种尝试都不起作用。我不确定发生了什么事。下面提到了我的尝试。我真的很感激对此的任何建议。

我尝试了尝试 4(在下面第 II 节中进行了描述),但似乎检查/取消检查仍然没有实时发生。

尝试1:试图在 Fragment 中检查 true

listAdapter = new SignalsExpandableListAdapter(context,signalsListDataHeader,signalsListHash);
signalsExpandableListView.setAdapter(listAdapter);
signalsExpandableListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
//tickCheckboxes();

signalsExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        CheckedTextView sensorCheckedView  = (CheckedTextView) v.findViewById(R.id.sensorsCheckedTextView);
        if(!mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
            sensorCheckedView.setChecked(true);
            try {
                if(Plot==null) {
                    Log.e(LOG_TAG, "Plot is null!");
                }
                mService.mPlotManager.addSignal(deviceMList.get(groupPosition).get(childPosition), Plot);
            } catch (Exception e) {
                Log.e(LOG_TAG, "Error! + e);
                e.printStackTrace();
            }
        } else {
            mService.mPlotManager.removeSignal(deviceMList.get(groupPosition).get(childPosition));
            //signalsExpandableListView.setItemChecked(childPosition,false);
            sensorCheckedView.setChecked(false);
        }
        return true;
    }
});

尝试2:创建一个不同的函数并在 Fragment 中调用它。创建的函数如下,函数调用在尝试 1 上方注释。

//loop through groups and then children
public void tickCheckboxes() {
    //CheckedTextView sensorCheckedView  = (CheckedTextView) signalsExpandableListView.findViewById(R.id.sensorsCheckedTextView);
    for (int i = 0; i < deviceMList.size(); i++) {
        for (int j = 0; j < deviceMList.get(i).size(); j++) {
            if (mService.mPlotManager.ifPropertyExist(deviceMList.get(i).get(j))) {
                signalsExpandableListView.setItemChecked(j, true);
                //sensorCheckedView.setChecked(true);
            } else {
                signalsExpandableListView.setItemChecked(j, false);
                //sensorCheckedView.setChecked(false);
            }
        }
    }
}

尝试3:使用 ExpandableListView 适配器访问子元素。我更新了getChildView()方法如下。

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
    final String childText = (String) getChild(groupPosition,childPosition);
    if(view == null){
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.sensors_list_items,null);
    }
    CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
    txtListChild.setText(childText);
    txtListChild.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (txtListChild.isChecked()) {
                txtListChild.setChecked(false);
            } else {
                txtListChild.setChecked(true);
            }
        }
    });
    return view;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

列出项目 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckedTextView
        android:id="@+id/sensorsCheckedTextView"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:textColor="@color/black"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"/>


</LinearLayout>

组 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/deviceNameTextView"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:text="Device Name"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:background="@color/colorAccent"
        android:textColor="@color/colorPrimary"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"/>
</LinearLayout>

我正在寻找什么II:为了选中/取消选中,我在尝试 4 的帮助下修改了适配器answer https://stackoverflow.com/a/61996186/11375743如以下所说的。我看不到checked实时标记。当我单击一个子项时,我想向上/向下滚动以直观地看到复选标记。与取消选中相同。它很奇怪。似乎需要一种“刷新”才能实时查看选中/取消选中状态。我将不胜感激任何关于如何摆脱这个问题的建议。

谢谢。

尝试4:改装适配器。这适用于选中/取消选中,但无法实时直观地看到它。

public class SignalsExpandableListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> listDataHandler;
    private HashMap<String,List<String>> listHashMap;
    private MService mService;
    private ArrayList<List<String[]>> deviceMList ;

    public SignalsExpandableListAdapter(Context context, List<String> listDataHandler, HashMap<String, List<String>> listHashMap,mService service, ArrayList<List<String[]>> dMList ) {
        this.context = context;
        this.listDataHandler = listDataHandler;
        this.listHashMap = listHashMap;
        this.mService = service;
        this.deviceMList =dMList;
    }

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
    final String childText = (String) getChild(groupPosition,childPosition);
    if(view == null){
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.sensors_list_items,null);
    }
    CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
    txtListChild.setText(childText);

if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
        txtListChild.setChecked(true);
    } else {
        txtListChild.setChecked(false);
    }
    return view;
}

我假设你的mService使用名为 PlottingService 的类,然后尝试对适配器进行以下更改:

public class SignalsExpandableListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> listDataHandler;
    private HashMap<String,List<String>> listHashMap;
    private PlottingService mService;

    public SignalsExpandableListAdapter(Context context, List<String> listDataHandler, HashMap<String, List<String>> listHashMap, PlottingService ps) {
        this.context = context;
        this.listDataHandler = listDataHandler;
        this.listHashMap = listHashMap;
        this.mService = ps;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
        final String childText = (String) getChild(groupPosition,childPosition);
        if(view == null){
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.sensors_list_items,null);
        }
        CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
        txtListChild.setText(childText);

        // Always set check state according to data.
        if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
            txtListChild.setChecked(true);
        } else {
            txtListChild.setChecked(false);
        }

        return view;
    }

在 Fragment 内,执行尝试 1,仅更改:listAdapter = new SignalsExpandableListAdapter(context,signalsListDataHeader,signalsListHash); to listAdapter = new SignalsExpandableListAdapter(context,signalsListDataHeader,signalsListHash, mService);

希望有帮助!

更新:-在 Fragment 中,注释掉 OnChildClickListener 并在适配器中使用以下 getChildView:

public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View view, ViewGroup parent) {
    String childText = (String) getChild(groupPosition,childPosition);
    if(view == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.sensors_list_items,null);
    }
    CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
    txtListChild.setText(childText);

    // Always set check state according to data.
    if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
        txtListChild.setChecked(true);
    } else {
        txtListChild.setChecked(false);
    }

    txtListChild.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckedTextView sensorCheckedView = (CheckedTextView)v;
            if(!sensorCheckedView.isChecked()) {
                sensorCheckedView.setChecked(true);
                try {
                    if(Plot==null) {
                        Log.e(LOG_TAG, "Plot is null!");
                    }
                    mService.mPlotManager.addSignal(deviceMList.get(groupPosition).get(childPosition), Plot);
                } catch (Exception e) {
                    Log.e(LOG_TAG, "Error!" + e);
                    e.printStackTrace();
                }
            } else {
                mService.mPlotManager.removeSignal(deviceMList.get(groupPosition).get(childPosition));
                sensorCheckedView.setChecked(false);
            }
        }
    });
    return view;
}

更新2:-在 Fragment 中,注释掉 OnChildClickListener 并在适配器中添加一个内部类并使用以下 getChildView:

class Position {
    int group, child;
    Position(int group, int child) {
        this.group = group;
        this.child = child;
    }
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
    String childText = (String) getChild(groupPosition,childPosition);
    if(view == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.sensors_list_items,null);
    }
    CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
    txtListChild.setText(childText);

    // Always set check state according to data.
    if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
        txtListChild.setChecked(true);
    } else {
        txtListChild.setChecked(false);
    }

    txtListChild.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckedTextView sensorCheckedView = (CheckedTextView)v;
            int groupPosition = ((Position)v.getTag()).group;
            int childPosition = ((Position)v.getTag()).child;
            if(!sensorCheckedView.isChecked()) {
                sensorCheckedView.setChecked(true);
                try {
                    if(Plot==null) {
                        Log.e(LOG_TAG, "Plot is null!");
                    }
                    mService.mPlotManager.addSignal(deviceMList.get(groupPosition).get(childPosition), Plot);
                } catch (Exception e) {
                    Log.e(LOG_TAG, "Error!" + e);
                    e.printStackTrace();
                }
            } else {
                mService.mPlotManager.removeSignal(deviceMList.get(groupPosition).get(childPosition));
                sensorCheckedView.setChecked(false);
            }
        }
    });
    txtListChild.setTag(new Position(groupPosition, childPosition));
    return view;
}

If 更新2仍然存在多个检查项的问题,那么可能的原因是mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))。而不是使用mService要验证是否选择了某个项目,更改可能是更好的主意列表哈希映射 from HashMap<String, List<String>> to HashMap<String, List<ChildItem>>[已接受的答案滚动 ExpandableListView 后计数器的值发生变化 https://stackoverflow.com/questions/50092400/values-of-counter-changes-after-scrolling-expendablelistview,您需要一个布尔字段而不是整数字段,数量]。然后,当单击子项目时,检查并更新列表。

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

如何选中/取消选中 ExpandableListView 内的 CheckedTextView(子项)项? 的相关文章

  • 定期更新 SWT 会导致 GUI 冻结

    Problem 当 GUI 字段定期更新时 SWT 会冻结 我想要一个基于 SWT 的 GUI 其中文本字段的值会定期递增 最初我从单独的线程访问 textField 导致抛出异常 线程 Thread 0 org eclipse swt S
  • JTable 和 JScrollpane 大小的问题

    我有一个JScrollPane with a JTable在里面 在里面JTable我最初有 3 行 稍后添加行 默认JTable我的 3 行很难看 因为JScrollPane calls getPreferredScrollableVie
  • 如何使用应用程序接口将蓝牙套接字传递给另一个活动

    因此 根据我收集的信息 套接字连接既不可序列化 也不可分割 但我需要将蓝牙连接传递给另一个活动 我不想作为中间人编写服务 所以请不要将此作为解决方案发布 我听说有一种方法可以使用自定义应用程序接口来传递这些类型的对象 但我一生都找不到这样的
  • 有多少种方法可以将位图转换为字符串,反之亦然?

    在我的应用程序中 我想以字符串的形式将位图图像发送到服务器 我想知道有多少种方法可以将位图转换为字符串 现在我使用 Base64 格式进行编码和解码 它需要更多的内存 是否有其他可能性以不同的方式做同样的事情 从而消耗更少的内存 现在我正在
  • JFace ColumnWeigthData 导致父级增长

    我有一个 Eclipse RCP 应用程序 并且想要在TableViewer using ColumnWeigthData as ColumnLayoutData 问题是父表单 ScrolledForm在示例代码中 每当我布局表格时都会增加
  • 如何在 Eclipse 中使用其他外部 jar 依赖项创建不可运行/不可执行的 jar

    我无法通过 Eclipse 导出向导创建普通的 jar 不可运行 不可执行 它仅创建 jar 文件 但不会导出依赖的 jar 从而在从其他类调用导出的 jar 的方法时出现错误 请帮助 非常感谢 kurellajunior的建议 它是通过使
  • Android 时钟滴答数 [赫兹]

    关于 proc pid stat 中应用程序的总 CPU 使用率 https stackoverflow com questions 16726779 total cpu usage of an application from proc
  • 异步迭代器

    我有以下代码 while slowIterator hasNext performLengthTask slowIterator next 由于迭代器和任务都很慢 因此将它们放入单独的线程中是有意义的 这是对迭代器包装器的快速而肮脏的尝试
  • 在 IntelliJ 中运行 Spring Boot 会导致 Unable to load 'javax.el.E​​xpressionFactory'

    我正在尝试运行一个简单的 Spring Boot 应用程序 该应用程序具有以下 Maven pom file
  • 当您在数组列表上调用remove(object o)时,它如何比较对象?

    当您在 java 中的数组列表上调用remove object o 时 它如何比较对象以找到要删除的正确对象 它使用指针吗 或者它使用 Comparable 接口来比较对象吗 ArrayList remove 依赖于对象的实现Equal方法
  • 如何找到被点击的JLabel并从中显示ImageIcon?

    这是我的代码 我想知道哪个l单击 然后在新框架中显示该 ImageIcon e getSource 不起作用 final JFrame shirts new JFrame T shirts JPanel panel new JPanel n
  • 如何将 arraylist 从 servlet 传递到 javascript?

    我通过在属性中设置数组列表并将其转发到 jsp 来从 servlet 传递数组列表 Servlet ArrayList
  • 如何初始化静态地图?

    你会如何初始化静态Map在Java中 方法一 静态初始化方法二 实例初始化 匿名子类 或者 还有其他方法吗 各自的优点和缺点是什么 这是说明这两种方法的示例 import java util HashMap import java util
  • 在 Android 手机中通过耳机插孔发送数据

    我目前正在处理一个新项目 我必须通过具有特定电压的耳机插孔发送数据 然后我可以在该电压上工作 所以这里我需要根据我的数据来编程具体电压 我是否可以在android中访问耳机的输出电压 然后创建一个应用程序来控制该电压 这是一篇讨论此问题的
  • 对于当前月份和日期但年份不同的日期,经过的月份计算未给出正确的结果

    我正在尝试计算自特定日期以来经过的月份 该函数工作正常 尽管如果我将今天的日期与过去的不同年份放在一起 它会给我一个月的差异 不到一个月 假设对于所有日期 该函数都运行良好 除了 如果今天是 2014 03 06 YYYY MM DD 并且
  • Flash 对象未显示在phonegap android 中

    我已经在 android 手机间隙创建了一个应用程序 我有一个屏幕 我想显示一个静态 flash obj 所以我在屏幕 HTML 页面中放入了以下代码
  • Fragment 生命周期和在不存在的 Fragment 上调用 onCreate 的问题

    我正在 Android 中测试片段 并且片段生命周期有一些令人困惑的行为 我有一个活动 在横向和纵向模式下使用 xml 布局 我有一些代码可以访问在片段布局之一中定义的 EditText 对象 如果我以横向模式启动应用程序 一切都会正常 我
  • 摩尔斯电码 至 英语

    我现在的问题是让 摩尔斯电码转英语 正常工作 将英语转换为莫尔斯电码的第一部分工作正常 我知道以前已经有人问过这个问题 但我不知道我做错了什么 我知道我需要在某个地方进行拆分 但我只是不确定将其放在代码中的何处 现在 莫尔斯电码到英语的部分
  • 为什么应该首选 Java 类的接口?

    PMD https pmd github io 将举报以下违规行为 ArrayList list new ArrayList 违规行为是 避免使用 ArrayList 等实现类型 而是使用接口 以下行将纠正违规行为 List list ne
  • 如何在不同版本的Google App Engine中使用自定义域名?

    我使用谷歌应用程序引擎作为我的 Android 和 Web 应用程序的服务器 我使用 Android Studio 开发了 Android 应用程序 并使用 Eclipse 开发了 Web 应用程序 我在应用程序引擎中部署了两个版本 第一个

随机推荐