【问题标题】:ExpandableListView and CheckedTextView strange behaviorExpandableListView 和 CheckedTextView 的奇怪行为
【发布时间】:2010-09-30 12:28:15
【问题描述】:

我有一个带有 CursorTreeAdapter 和 CheckedTextView 的 ExpandableListActivity。 它在点击时工作正常(我必须手动切换 CheckedTextView,但那是因为这是一个 ExpandableListView)但是当适配器在光标上调用 requery 时,被检查的项目不是正确的(和以前一样)。

你有什么线索吗?

【问题讨论】:

  • 顺便说一句,带有 CheckBox 和侦听器,它也不起作用。似乎 ExpandableListView 是按位置保持复选框的状态而不保持组位置

标签: android cursor expandablelistview


【解决方案1】:

这很可能是 Android 的列表回收在起作用。这是一个类似问题的 SO 问题:Android: Handle ListView Recycle。在您的情况下,您的适配器处理 TextView 标签的更新,因此这些看起来是正确的,但它不关心复选框状态。我有类似的问题,并通过提供我自己的适配器解决了这个问题,基于SimpleExpandableListAdapter,但覆盖了getChildView 方法,该方法同时处理文本视图内容和复选框状态。

在这种情况下,我发现查看相应的 Android 源文件以了解实际情况很有启发性。在这种情况下,有趣的文件是 ExpandableListView.javaSimpleExpandableListAdapter.java(我的适配器基于的文件,在你的情况下是 CursorTreeAdapter.java

【讨论】:

    【解决方案2】:

    查看我的答案以保持选中项目的状态。 . 访问Android ExpandableListView with Checkbox, Controlling checked state

    【讨论】:

      【解决方案3】:

      ExpandedListView 遇到了同样的问题。 Switch 或另一个 Checkable 控件在展开/折叠其他组组件后未保存语句。我通过将状态保存到 childData 列表/数组中解决了这个问题。

      public class ExpandPrefAdapter extends BaseExpandableListAdapter {
      
      private static final String TAG = ExpandPrefAdapter.class.getName();
      
      private List<String> mGroupTitle = new ArrayList<>();
      private Context mContext;
      private List<List<ExpandableListItem>> mChildData = new ArrayList<>();
      
      public ExpandPrefAdapter(Context context, final List<String> groupTitle, final
      List<List<ExpandableListItem>> childData) {
          mContext = context;
          mGroupTitle = groupTitle;
          mChildData = childData;
      }
      
      @Override
      public View getChildView(int listPosition, final int expandedListPosition,
                               boolean isLastChild, View convertView, ViewGroup parent) {
      
          LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View rootView = inflater.inflate(R.layout.content_pref_entry, parent, false);
      
          final int groupPos = listPosition;
          final int childPos = expandedListPosition;
      
          ExpandableListItem item = (ExpandableListItem) getChild(listPosition, expandedListPosition);
      
          TextView textViewPrefTitle = (TextView) rootView.findViewById(R.id.text_view_pref_title);
          textViewPrefTitle.setText(item.getTitle());
      
          final Switch switchView = (Switch) rootView.findViewById(R.id.switch_pref);
          switchView.setChecked(mChildData.get(groupPos).get(childPos).isChecked());
          switchView.setOnCheckedChangeListener((v, b) -> mChildData.get(groupPos).get(childPos).setChecked(b));
          return rootView;
      }
      
      @Override
      public int getChildrenCount(int listPosition) {
          return mChildData.get(listPosition).size();
      }
      
      @Override
      public Object getChild(int listPosition, int expListPosition) {
          return mChildData.get(listPosition).get(expListPosition);
      }
      
      @Override
      public long getChildId(int listPosition, int expandedListPosition) {
          return expandedListPosition;
      }
      
      @Override
      public Object getGroup(int listPosition) {
          return mGroupTitle.get(listPosition);
      }
      
      @Override
      public int getGroupCount() {
          return mGroupTitle.size();
      }
      
      @Override
      public long getGroupId(int groupPosition) {
          return groupPosition;
      }
      
      @Override
      public View getGroupView(int listPosition, boolean isExpanded,
                               View convertView, ViewGroup parent) {
      
          String groupTitle = (String) getGroup(listPosition);
          if (convertView == null) {
              LayoutInflater mInflater = (LayoutInflater) mContext.
                      getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = mInflater.inflate(R.layout.content_pref_group, null);
          }
          TextView textViewGroup = (TextView) convertView
                  .findViewById(R.id.text_view_pref_group_title);
          textViewGroup.setText(groupTitle);
          Log.wtf(TAG, "getGroupView(): listPosition: " + listPosition + " isExpanded: " + isExpanded);
          return convertView;
      }
      
      @Override
      public boolean hasStableIds() {
          return true;
      }
      
      @Override
      public boolean isChildSelectable(int listPosition, int expandedListPosition) {
          return false;
      }
      }
      

      我想它会帮助你

      【讨论】:

        猜你喜欢
        • 2011-04-07
        • 1970-01-01
        • 2017-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-04
        • 2011-10-04
        • 2015-03-16
        相关资源
        最近更新 更多