代码改变世界

ExpandableListView置顶和子list收放

2015-12-23 17:30  Weiggle  阅读(880)  评论(0编辑  收藏  举报
  private int sign = -1;
  result_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, final View v, int groupPosition, long id) {
                final View view = result_list.getChildAt(0);
                final int p = groupPosition;
                if(sign == -1){
                    result_list.expandGroup(groupPosition);
                    scroll(result_list, p, view);
                    sign = groupPosition;
                }else if(sign == groupPosition){
                    result_list.collapseGroup(sign);
                    sign = -1;
                }else{
                    result_list.collapseGroup(sign);
                    result_list.expandGroup(groupPosition);
                    scroll(result_list, p, view);
                    sign = groupPosition;
                }
                return true;
            }
        });

  //控制滑动
    public static void scroll(final ExpandableListView result_list,final int position,final View view){
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                if (android.os.Build.VERSION.SDK_INT >= 11) {
                    result_list.smoothScrollToPositionFromTop((position - 1),
                            -view.getHeight(), 300);
                }
            }
        });
    }