【问题标题】:ExpandableListView selected item lost on group collapseExpandableListView 所选项目在组折叠时丢失
【发布时间】:2014-01-21 11:04:28
【问题描述】:

我在片段上有一个 ExpandableListView,当我选择一个项目时,我正在更改该项目 (TextView) 的背景和文本颜色。这工作正常。但是,当我折叠包含当前所选项目的组时,选择会丢失。如何阻止这种情况发生/将所选项目设置回应有的状态?

我尝试过使用 onGroupExpand/CollapseListener,但没有成功。例如我试过这个:

_list.setOnGroupExpandListener(new OnGroupExpandListener() {
    @Override
    public void onGroupExpand(int groupPosition) {
        _list.setItemChecked(_activePosition, true);
    }
});

注意:_list_activePosition 在类级别声明。

这是我设置选中项的代码,从我看到的示例中,我认为这是标准做法,但如果不是,请告知,因为我是 android 新手。

HelpItemsFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    final List<HelpCategory> itemList = new ArrayList<HelpCategory>();
    HelpCategory helpCategory;

    //replace this with database stuff later
    helpCategory = new HelpCategory();
    helpCategory.setTitle("Group 1");
    helpCategory.addHelpItem(new HelpItem((long) 1, "1.1", "Details for 1.1"));
    helpCategory.addHelpItem(new HelpItem((long) 1, "1.2", "Details for 1.2"));
    helpCategory.addHelpItem(new HelpItem((long) 1, "1.3", "Details for 1.3"));
    itemList.add(helpCategory);

    helpCategory = new HelpCategory();
    helpCategory.setTitle("Group 2");
    helpCategory.addHelpItem(new HelpItem((long) 2, "2.1", "Details for 2.1"));
    helpCategory.addHelpItem(new HelpItem((long) 2, "2.2", "Details for 2.2"));
    helpCategory.addHelpItem(new HelpItem((long) 2, "2.3", "Details for 2.3"));  
    itemList.add(helpCategory);

    HelpExpandableListAdapter listAdapter = new HelpExpandableListAdapter(this.getActivity(), itemList);
    _list.setAdapter(listAdapter);

    _list.setOnChildClickListener(new OnChildClickListener() {
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
            HelpItem helpItem = itemList.get(groupPosition).getHelpItemList().get(childPosition);
            _activePosition = index;
            _callback.onItemSelected(null, helpItem.getTitle(), helpItem.getDetail(), _activePosition);
            parent.setItemChecked(index, true);
            return true;
        }
    });

【问题讨论】:

  • 你可以试试这个 - public View onCreateView(...) 而不是 public void onActivityCreated(...)

标签: android android-fragments expandablelistview


【解决方案1】:

适配器类HelpExpandableListAdapter应该是这样的

    public class HelpExpandableListAdapter extends BaseExpandableListAdapter {

       private LayoutInflater inflater;
        private ArrayList<CategoryGroup> mParent;

      public ExpandableListItems(Context context, ArrayList<CategoryGroup> parent) {
            mParent = parent;
            inflater = LayoutInflater.from(context);
        }


        @Override
        //counts the number of parent items so the list knows how many times calls getGroupView() method
        public int getGroupCount() {
            return mParent.size();
        }

        @Override
        //counts the number of children items so the list knows how many times calls getChildView() method
        public int getChildrenCount(int i) {
            return mParent.get(i).getArrayChildren().size();
        }

        @Override
        //gets the title of each parent/group
        public Object getGroup(int i) {
            return mParent.get(i).getTitle();
        }

        @Override
        //gets the name of each item
        public Object getChild(int i, int i1) {
            return mParent.get(i).getArrayChildren().get(i1);
        }

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

        @Override
        public long getChildId(int i, int i1) {
            return i1;
        }

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

        @Override
        //in this method you must set the text to see the parent/group on the list
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

            if (view == null) {
                System.out.println("i: " + i + "; b: " + b );
                view = inflater.inflate(R.layout.grouprow, viewGroup, false);
            }


            TextView textView = (TextView) view.findViewById(R.id.rowname);

            //"i" is the position of the parent/group in the list

            textView.setText(getGroup(i).toString());

            //return the entire view
            return view;
        }

        @Override
        //in this method you must set the text to see the children on the list
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = inflater.inflate(R.layout.childrow, viewGroup, false);
            }

            TextView textView = (TextView) view.findViewById(R.id.grpchild);
            //"i" is the position of the parent/group in the list and 
            //"i1" is the position of the child
            textView.setText(mParent.get(i).getArrayChildren().get(i1));

            //return the entire view
            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {
            /* used to make the notifyDataSetChanged() method work */
            super.registerDataSetObserver(observer);
        }
   }

团体课

   public class CategoryGroup {
   private String mTitle;
    private ArrayList<String> mArrayChildren;

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String mTitle) {
        this.mTitle = mTitle;
    }

    public ArrayList<String> getArrayChildren() {
        return mArrayChildren;
    }

    public void setArrayChildren(ArrayList<String> mArrayChildren) {
        this.mArrayChildren = new ArrayList(mArrayChildren);
    }
  }

在 onCreateView() 中

    ArrayList<CategoryGroup> arrayParentsGroups = new ArrayList<CategoryGroup>();

    arrayParentsGroups = createGroupList();

    listAdapter = new HelpExpandableListAdapter(this, arrayParentsGroups);
    _list.setAdapter(listAdapter);

    private List createGroupList() {
        ArrayList arrayParents = new ArrayList();
        for( int i = 0 ; i < 10 ; ++i ) { // 10 groups........
            CategoryGroup parent = new CategoryGroup();
            parent.setTitle("Group row" + i);

            ArrayList<String> arrayChildren = new ArrayList<String>();
            for (int j = 0; j < 3; j++) {
                arrayChildren.add("Child row" + j);
            }
            parent.setArrayChildren(arrayChildren);
            arrayParents.add(parent);
        }
        return arrayParents;
    }

【讨论】:

    猜你喜欢
    • 2015-11-18
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    相关资源
    最近更新 更多