Expandablelistview组标题单击时失去焦点

时间:2016-05-27 17:25:35

标签: android expandablelistview

我有一个expandablelistview,一次打开一个组头,也就是说,只要我点击一个组头,我就会关闭以前打开的组头。任何时候我第一次点击一个组标题它表现正常,但随后点击组标题会将点击的组标题从视图推到顶部。如何让小组标题保持在最顶层?

list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @SuppressLint("NewApi")
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
         //   parent.smoothScrollToPositionFromTop(groupPosition, 0);
            parent.setSelectionFromTop(groupPosition,0);

            return false;
        }
    });
    list.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {


        @Override
        public void onGroupExpand(int groupPosition) {
            if (lastExpandedPosition != -1 && groupPosition != lastExpandedPosition) {
                list.collapseGroup(lastExpandedPosition);
            }
            lastExpandedPosition = groupPosition;
        }
    });

1 个答案:

答案 0 :(得分:1)

将以下代码用于onGroupClick()方法。一旦我解决了这个问题。希望它对你有所帮助

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        parent.smoothScrollToPositionFromTop(groupPosition, 0);
    }
},100);