简体   繁体   中英

How to disable scrolling property of expandable list view in android?

是否可以通过xml或代码禁用android中可扩展列表视图的滚动属性?

我认为这是不可能的,因为该视图会像ListView一样自动设置为垂直滚动列表,因为ExpandableListView继承了ListView。

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_expandable_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/LinearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">

    <!-- your code --->

     <ExpandableListView
                android:id="@+id/activity_expandable_list_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"/>


    enter code here
        </LinearLayout>
    </ScrollView>

// In java code

mListView = (ExpandableListView) findViewById(R.id.activity_expandable_list_view);
        MyExpandableListAdapter adapter = new MyExpandableListAdapter(this,
                mGroups);
        mListView.setAdapter(adapter);
        mListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {
                setListViewHeight(parent, groupPosition);
                return false;
            }
        });



private void setListViewHeight(ExpandableListView listView,
                               int group) {
    ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
    int totalHeight = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
            View.MeasureSpec.EXACTLY);
    for (int i = 0; i < listAdapter.getGroupCount(); i++) {
        View groupItem = listAdapter.getGroupView(i, false, null, listView);
        groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

        totalHeight += groupItem.getMeasuredHeight();

        if (((listView.isGroupExpanded(i)) && (i != group))
                || ((!listView.isGroupExpanded(i)) && (i == group))) {
            for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                View listItem = listAdapter.getChildView(i, j, false, null,
                        listView);
                listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

                totalHeight += listItem.getMeasuredHeight();

            }
        }
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    int height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
    if (height < 10)
        height = 200;
    params.height = height;
    listView.setLayoutParams(params);
    listView.requestLayout();

}

You may not disable the autoscroll of an expandable listview however, you can use some tricks to achive this.
you can use requestDisallowInterceptTouchEvent method to disable scrolling of expandable listview. this will be not enough. If you do not want to auto-scroll of exp while expanding you should calculate the height of childs then set new param layouts to exp then refresh the scrollview.

boolean dispatchMode=false;
expandableListView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    int action = event.getAction();
                    v.getParent().requestDisallowInterceptTouchEvent(dispatchMode);
                    switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        // Disallow ScrollView to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(dispatchMode);

                        break;

                    case MotionEvent.ACTION_UP:
                        // Allow ScrollView to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(dispatchMode);

                        break;
                    }

                    // Handle ListView touch events.
                    v.onTouchEvent(event);
                    return true;

                }
            });




                @Override
                public void onGroupExpand(int groupPosition) {
                    LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expandableListView.getLayoutParams();
                    // param.height = (expandableListView.getChildCount() *
                    // expandableListView.getHeight());

                    int childrenCount = leftAttributeAdapter.getChildrenCount(groupPosition);
                    param.height += leftAttributeAdapter.getChildrenCount(groupPosition) * expandableListView.getChildAt(0).getHeight();
                    if (childrenCount > 2 && childrenCount < 10) {
                        param.height += 50 * 2;
                    } else if (childrenCount > 10)
                        param.height += 50 * childrenCount;

                    toast("Expandingggg  Children count:" + leftAttributeAdapter.getChildrenCount(groupPosition)  + " and Parameter heigh:" + param.height);

                    expandableListView.setLayoutParams(param);
                    expandableListView.refreshDrawableState();
                    scrollView1.refreshDrawableState();

                }
            });

            expandableListView.setOnChildClickListener(new OnChildClickListener() {

                @Override
                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                    Toast.makeText(getActivity(), " Child Clicked***" + this.toString(), Toast.LENGTH_SHORT).show();
                    return false;
                }
            });

            expandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

                @Override
                public void onGroupCollapse(int groupPosition) {
                    Toast.makeText(getActivity(), " Collapsed***" + this.toString(), Toast.LENGTH_SHORT).show();
                    LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expandableListView.getLayoutParams();
                    int childrenCount = leftAttributeAdapter.getChildrenCount(groupPosition);
                    param.height -= leftAttributeAdapter.getChildrenCount(groupPosition) * expandableListView.getChildAt(0).getHeight();
                    if (childrenCount > 2 && childrenCount < 10) {
                        param.height -= 30;
                    } else if (childrenCount > 10)
                        param.height -= 120;
                    expandableListView.setLayoutParams(param);
                    expandableListView.refreshDrawableState();
                    scrollView1.refreshDrawableState();

                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM