如何在android中禁用可扩展列表视图的滚动属性?

时间:2011-03-02 06:58:19

标签: android scroll expandablelistview

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

    <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>

//在Java代码中

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

}

答案 2 :(得分:-1)

您可能无法禁用可扩展列表视图的自动滚动,但是,您可以使用一些技巧来实现此目的。
您可以使用requestDisallowInterceptTouchEvent方法来禁用可扩展列表视图的滚动。这还不够。如果您不希望在扩展时自动滚动exp,则应计算子项的高度,然后将新的param布局设置为exp,然后刷新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();

                }
            });