多级ExpandableListView中的项目并不总是可单击

时间:2014-08-19 10:59:03

标签: android android-listview expandablelistview expandablelistadapter

我根据互联网上的很多例子制作了一个通常的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 http://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"
        />

how it looks

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

我不太了解我所依据的来源是onMeasure中的内容。以及listviews的组和子组之间的分离。也许最好为第一级的每个组都有一个可扩展的列表视图,而不是第二级的每个组都有一个?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

尝试在每个getChildViewgetGroupView方法

上附加您的点击监听器
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);



        cache.put(groupPosition*10000 + childPosition, l);
        l.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Log.d("LibraryListAdapter"," group clicked :  " + groupPosition + "childClicked :" + childPosition);

                    }
                });
        return l;
    }

    @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));

        row.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Log.d("LibraryListAdapter"," group clicked :  " + groupPosition);

                    }
                });
        return row;
    }
}



public class LibraryListCityAdapter extends LibraryListBaseAdapter
{

    @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"));
        row.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Log.d("LibraryListCityAdapter"," group clicked :  " + groupPosition + " childClicked :" + childPosition);


                    }
                });
        return row;
    }


    @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));
        rowsetText(localLibs.get(state).get(city).get(childPosition).get("NAME"));
        row.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Log.d("LibraryListCityAdapter"," group clicked :  " + groupPosition );


                    }
                });
        return row;
    }

}

我所做的更改只是在getGroupView和getChildView方法上并删除了 来自上层Listview的setOnChildClickListener

答案 2 :(得分:-1)

我不知道你的情况会出现什么问题。但我可以告诉你这个问题可能的解决方法。

尝试使用名为FloatingGroupExpandableListView的库,它比Expandable listview好得多。

使用这个你必须少编码&amp;当滚动时组视图缩小时,它也会改变组颜色。