android– 多级ExpandableListView中的项目并不总是可点击的

android– 多级ExpandableListView中的项目并不总是可点击的,第1张

概述我根据互联网上的许多例子制作了一个通常的3级ExpandableListView,效果很好.有一点需要注意.单击第2级和第3级始终不起作用.虽然有时它工作正常.通常在开始时它无处不在.然后它不再起作用,就像50%的项目一样.没有任何逻辑.有时我扩展第二组,然后我不能再崩溃那个组.或者有时我扩展

我根据互联网上的许多例子制作了一个通常的3级ExpandableListVIEw,效果很好.

有一点需要注意.单击第2级和第3级始终不起作用.虽然有时它工作正常.通常在开始时它无处不在.然后它不再起作用,就像50%的项目一样.没有任何逻辑.有时我扩展第二组,然后我不能再崩溃那个组.或者有时我扩展第二组然后我不能崩溃第二级别的任何其他组.唯一一致的是它通常通过将锁定的视图滚出可见区域并返回来进行修复.

会发生什么事?

    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.chooselib);        //...        ExpandableListVIEw lv = (ExpandableListVIEw) findVIEwByID(R.ID.libListVIEw);        lv.setAdapter(new librarylistadapter());    }    final List<String> states = new ArrayList<String>();    final List<List<String>> citIEs = new ArrayList<List<String>>();    final List<List<List<Map<String, String>>>> locallibs = new ArrayList<List<List<Map<String, String>>>>();    public abstract class libraryListBaseAdapter extends Baseexpandablelistadapter{ //changing these functions has no effect whatsoever on the problem        @OverrIDe        public Object getChild(int groupposition, int childposition)        {            return getChildID(groupposition, childposition);        }        @OverrIDe        public long getChildID(int groupposition, int childposition)        {            return (groupposition << 15) + childposition + 1;        }        @OverrIDe        public Object getGroup(int groupposition)        {            return getGroupID(groupposition);        }        @OverrIDe        public long getGroupID(int groupposition)        {            return (groupposition << 15);        }        @OverrIDe        public boolean hasStableIDs()        {            return true;        }        @OverrIDe        public boolean isChildSelectable(int groupposition, int childposition)        {            return true;        }    }    public class librarylistadapter extends libraryListBaseAdapter    {        Map<Integer, ExpandableListVIEw> cache = new TreeMap<Integer, ExpandableListVIEw>();        @OverrIDe        public VIEw getChildVIEw(final int groupposition, final int childposition,                                 boolean isLastChild, VIEw convertVIEw, VIEwGroup parent)        {            if (cache.containsKey(groupposition*10000 + childposition))                return cache.get(groupposition*10000 + childposition);            libraryListCityVIEw l = new libraryListCityVIEw();            l.setpadding((int)(60 * getResources().getdisplayMetrics().density), l.getpaddingtop(), l.getpaddingRight(), l.getpaddingBottom());            //l.setLayoutParams(new VIEwGroup.LayoutParams(VIEwGroup.LayoutParams.FILL_PARENT, VIEwGroup.LayoutParams.WRAP_CONTENT)); has no effect whatsoever            //l.setFocusable(true); has no effect whatsoever            l.setAdapter(new libraryListCityAdapter(groupposition,childposition));            if (citIEs.get(groupposition).size() == 1) l.expandGroup(0);            l.setonChildClickListener(new ExpandableListVIEw.OnChildClickListener() {                @OverrIDe                public boolean onChildClick(ExpandableListVIEw expandableListVIEw, VIEw vIEw, int i, int lib, long l) {                    onLeafClick(groupposition,childposition,lib);                    return false;                }            });            cache.put(groupposition*10000 + childposition, l);            return l;        }        @OverrIDe        public int getChildrenCount(int groupposition)        {            return citIEs.get(groupposition).size();        }        @OverrIDe        public int getGroupCount()        {            return states.size();        }        @OverrIDe        public VIEw getGroupVIEw(int groupposition, boolean isExpanded,                                 VIEw convertVIEw, VIEwGroup parent)        {            VIEw row = //convertVIEw != null && convertVIEw instanceof TextVIEw ? convertVIEw :                    getLayoutInflater().inflate(androID.R.layout.simple_expandable_List_item_1, parent, false);            ((TextVIEw) row).setText(states.get(groupposition));            return row;        }    }    public class libraryListCityVIEw extends ExpandableListVIEw    {        int intGroupposition, intChildposition, intGroupID;        public libraryListCityVIEw()        {            super(libraryList.this);        }        protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec)        {            wIDthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(wIDthMeasureSpec), MeasureSpec.EXACTLY);            heightmeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST); //what does this do?            super.onMeasure(wIDthMeasureSpec, heightmeasureSpec);        }        //from https://stackoverflow.com/questions/19298155/issue-with-expanding-multi-level-expandableListvIEw        @OverrIDe        protected voID onDetachedFromWindow() {            try {                super.onDetachedFromWindow();            } catch (IllegalArgumentException e) {                // Todo: Workaround for http://code.Google.com/p/androID/issues/detail?ID=22751            }        }    }    public class libraryListCityAdapter extends libraryListBaseAdapter    {        int state,city;        libraryListCityAdapter (int a, int b) {            state = a;            city = b;        }        @OverrIDe        public VIEw getChildVIEw(int groupposition, int childposition,                                 boolean isLastChild, VIEw convertVIEw, VIEwGroup parent)        {            //todo: use convertVIEw (but do not want to mix groupVIEw/childVIEw up)            VIEw row = getLayoutInflater().inflate(R.layout.libraryinListvIEw , parent, false);            ((TextVIEw) row).setText(locallibs.get(state).get(city).get(childposition).get("name"));            return row;        }        @OverrIDe        public int getChildrenCount(int groupposition)        {            return locallibs.get(state).get(city).size();        }        @OverrIDe        public int getGroupCount()        {            return 1;        }        @OverrIDe        public VIEw getGroupVIEw(int groupposition, boolean isExpanded,                                 VIEw convertVIEw, VIEwGroup parent)        {            VIEw row = getLayoutInflater().inflate(R.layout.librarycityinListvIEw, parent, false);            ((TextVIEw) row).setText(citIEs.get(state).get(city));            return row;        }    }

chooselib.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:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="@string/lay_chooselib_choose"            androID:ID="@+ID/textVIEw" androID:layout_gravity="center_vertical|left" androID:padding="5sp"/>    <ExpandableListVIEw            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:ID="@+ID/libListVIEw" androID:layout_gravity="right|center_vertical"            /></linearLayout>

librarycityinListvIEw.xml:

<TextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"          androID:ID="@androID:ID/text1"          androID:layout_wIDth="fill_parent"          androID:layout_height="?androID:attr/ListPreferredItemHeight"          androID:gravity="center_vertical"          androID:paddingleft="?androID:attr/expandableListPreferredChildpaddingleft"          androID:singleline="true"          androID:textAppearance="?androID:attr/textAppearanceListItem"        />

libraryinListvIEw.xml:

<TextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"          androID:ID="@androID:ID/text1"          androID:layout_wIDth="fill_parent"          androID:layout_height="?androID:attr/ListPreferredItemHeight"          androID:paddingleft="60dip"          androID:gravity="center_vertical"        />

听起来有点像https://code.google.com/p/android/issues/detail?id=3414,但有点相反(因为有问题的列表是外部列表视图中的项目),并且更改descand可聚焦性并没有改变任何东西. (因为只有文本视图在列表中,所以它们无论如何都不可聚焦).在logcat中也没什么用处

我不太了解我所基于的来源是onMeasure中的东西.以及ListvIEws的组和子组之间的分离.也许最好为第一级的每个组都有一个可扩展的列表视图,而不是第二级的每个组都有一个?

解决方法:

查看代码时,我注意到hasStableIDs方法返回true,但getGroupID和getChildID根据位置变量返回值.我认为这会混淆ExpandableListVIEw逻辑.
只有当您的对象具有不可变的唯一ID时,hasStableIDs才会返回true.

总结

以上是内存溢出为你收集整理的android – 多级ExpandableListView中的项目并不总是可点击的全部内容,希望文章能够帮你解决android – 多级ExpandableListView中的项目并不总是可点击的所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1117108.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存