【问题标题】:Expandablelistview android with checkbox at onclick select more itemsExpandablelistview android在onclick上带有复选框选择更多项目
【发布时间】:2016-12-15 18:06:59
【问题描述】:

我为我的英语道歉。我正在尝试编写一个在 ExpandableListView 中使用复选框的应用程序,但是,当我单击一个项目列表时,该列表会检查单击的项目和周期为 6 的后续项目。例如,如果我单击第一个项目,除了第一个,第六个,第十二个等等等等。 下面是我的代码

example

prepareListData();
    expandableListView = (ExpandableListView)findViewById(R.id.expandableListViewProfessioni);
    expandableListAdapterProfessioni = new ExpandableListAdapterProfessioni(this,listDataHeader,listDataProfessioni);

    expandableListView.setAdapter(expandableListAdapterProfessioni);


    expandableListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            setListViewHeight(parent, groupPosition);

            return false;
        }
    });


    expandableListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            return false;
        }
    });private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataProfessioni = new HashMap<String, ArrayList<Mestieri>>();
    listDataHeader.add("Professione svolta");

    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Gson gson = new Gson();
    String json = sharedPrefs.getString("HomeActivity", null);
    Type type = new TypeToken<ArrayList<Mestieri>>() {}.getType();
    ArrayList<Mestieri> professioni = gson.fromJson(json, type);

    listDataProfessioni.put(listDataHeader.get(0), professioni);
}

private void setListViewHeight(ExpandableListView listView,
                               int group) {
    ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
    int totalHeight = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
            View.MeasureSpec.EXACTLY);
    for (int i = 0; i < listAdapter.getGroupCount(); i++) {
        View groupItem = listAdapter.getGroupView(i, false, null, listView);
        groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

        totalHeight += groupItem.getMeasuredHeight();

        if (((listView.isGroupExpanded(i)) && (i != group))
                || ((!listView.isGroupExpanded(i)) && (i == group))) {
            for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                View listItem = listAdapter.getChildView(i, j, false, null,
                        listView);
                listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

                totalHeight = 700;

            }
        }
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    int height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
    if (height < 10)
        height = 200;
    params.height = height;
    listView.setLayoutParams(params);
    listView.requestLayout();

}/**************** ADAPTER *******************/public class ExpandableListAdapterProfessioni extends BaseExpandableListAdapter{

private Context _context;
private List<String> _listDataHeader;

public static ArrayList<String> listaProfessioni;

private HashMap<String, ArrayList<Mestieri>> _listDataChild;

public ExpandableListAdapterProfessioni(Context context, List<String> listDataHeader,
                             HashMap<String, ArrayList<Mestieri>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon).getNome();
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

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

    final String childText = (String) getChild(groupPosition, childPosition);

    listaProfessioni = new ArrayList<String>();

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.expandable_listview_item_professioni, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.expandableListViewProfessioniItem);

    txtListChild.setText(childText);

    final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxItem);

    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(checkBox.isChecked()){
                checkBox.setChecked(false);

            }else {
                checkBox.setChecked(true);

                //listaProfessioni.add(getChild(groupPosition,childPosition).toString());
            }

        }
    });

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.expandable_listview_professioni, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.expandableListViewProfessioniHeader);
    lblListHeader.setTypeface(null, Typeface.NORMAL);
    lblListHeader.setText(headerTitle);

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

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

【问题讨论】:

    标签: android checkbox expandablelistview onitemclick


    【解决方案1】:

    我已经用这个 sn-p 解决了:

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {final int mGroupPosition = groupPosition;
            final int mChildPosition = childPosition;
            final String childText = (String) getChild(groupPosition, childPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.expandable_listview_item_professioni, null);
            }
            TextView txtListChild = (TextView) convertView
                    .findViewById(R.id.expandableListViewProfessioniItem);
            txtListChild.setText(childText);
            final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxItem);
            checkBox.setOnCheckedChangeListener(null);
            if (mChildCheckStates.containsKey(mGroupPosition)) {
                boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                checkBox.setChecked(getChecked[mChildPosition]);
            } else {
                boolean getChecked[] = new boolean[getChildrenCount(mGroupPosition)];
                mChildCheckStates.put(mGroupPosition, getChecked);
                checkBox.setChecked(false);
            }
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                        getChecked[mChildPosition] = isChecked;
                        mChildCheckStates.put(mGroupPosition, getChecked);
                    } else {
                        boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                        getChecked[mChildPosition] = isChecked;
                        mChildCheckStates.put(mGroupPosition, getChecked);
                    }
                }
            });
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(checkBox.isChecked()){
                        checkBox.setChecked(false);
                    }else {
                        checkBox.setChecked(true);
                    }
                }
            });
            return convertView;
        }
    

    【讨论】:

      【解决方案2】:

      首先,你应该添加一个数据结构来保持所有复选框的状态:

      private SparseArray<SparseArray<Boolean>> checked;
      

      在你的适配器上设置一些额外的方法:

      public boolean isChildChecked(int groupPosition, int childPosition) {
          SparseArray children = checked.get(groupPosition);
          if (children == null) return false;
          return children.get(childPosition, false);
      }
      
      public void setChildChecked(int groupPosition, int childPosition, boolean checked) {
          SparseArray children = checked.get(groupPosition);
          if (children == null) {
              children = new SparseArray<Boolean>();
              checked.put(groupPosition, children);
          }
          children.put(childPosition, checked);
      }
      
      public void toggleChild(int groupPosition, int childPosition) {
          setChildChecked(groupPosition, childPosition, 
                  ! getChildChecked(groupPosition, childPosition));
      }
      

      现在您可以在设置复选框时获取getView 中的选中值,并从事件处理程序中设置选中值:

      final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxItem);
      checkBox.setChecked(getChildChecked(groupPosition, childPosition));
      
      convertView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
      
              ExpandableListAdapterProfessioni.this.toggleChild(groupPosition, childPosition);
               //listaProfessioni.add(getChild(groupPosition,childPosition).toString());
              notifyDataSetChanged();
          }
      });
      

      这是处理检查状态的一种方法。如果您在 Mestieri 类中添加一个“已检查”属性并改用它,而不是单独的嵌套 SparseArray,这将更有意义。

      注意:为了在设备旋转时保留所有这些复选框,您需要以某种方式将选中的值保存在 onSaveInstanceState() 中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        • 2023-04-06
        • 2018-02-23
        • 1970-01-01
        • 2011-07-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多