如何 check/uncheck CheckedTextView (child items) items inside ExpandableListView?

How to check/uncheck CheckedTextView (child items) items inside ExpandableListView?

我的设计:我创建了一个自定义适配器(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 中看到 children 和 parent 元素。

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

我要找的是什么我:当我select任何child(可以是多个)我想表明selection 使用 CheckedTextView 打勾。当我 select 任何选中的 child 项目时,我也想取消选中。 (一个简单的 check/uncheck 设计)。同样,当任何 child 被 selected 时,另一个函数被调用用于绘图。这就是我卡住的地方。每次我select一个child时,随机多个children checkedTextViews也显示为checked not only the one I select。

经过一些搜索,我尝试了 3 次尝试来克服这个问题,并且只检查了 child I select。但似乎我的两次尝试都没有奏效。我不确定发生了什么。下面提到了我的尝试。我真的很感激对此的任何建议。

我尝试了尝试 4(在下面的第二节中描述。​​)但似乎 check/unchecks 没有在 real-time 中发生。

尝试 1: 尝试在 Fragment

中检查为真
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 适配器访问 child 元素。我更新了 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 : 对于 check/uncheck 我在下面提到的 的帮助下像尝试 4 中那样修改了适配器。我在 real-time 中看不到 checked 标记。当我单击 child 时,我想滚动 up/down 以直观地看到复选标记。与取消选中相同。它很奇怪。似乎需要某种 "refreshing" 才能在 real-time 中看到 check/uncheck 状态。我将不胜感激有关如何摆脱这种情况的任何建议。

谢谢。

尝试 4: 修改适配器。这适用于 check/uncheck,但在 real-time 中无法直观地看到它。

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 带有一个名为 class 的 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);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 并在适配器中添加一个内部 class 并使用以下 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;
}

如果Updated 2仍然存在多项检查项目的问题,那么可能的原因是mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))。与其使用 mService 来验证某个项目是否被选中,不如将 listHashMapHashMap<String, List<String>> 更改为HashMap<String, List<ChildItem>> [中接受的答案,你需要一个布尔字段而不是整数字段,数量]。然后当点击一个子项时,检查并更新列表。