ScrollView内部的可扩展ListView

时间:2018-03-19 16:24:05

标签: android xml kotlin scrollview expandablelistview

我在ScrollView内创建了 Expandable ListView 。当我想在 Expandable ListView 中滚动时,我必须选择它并按住它,而我滚动,否则整个页面将滚动。这是我的代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.kitkat.crossroads.JobBidsFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:focusable="true"
            android:focusableInTouchMode="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textViewJobInformation"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/bebasneueregular"
                    android:text="Job Information"
                    android:textAlignment="center"
                    android:textSize="30dp"
                    tools:layout_editor_absoluteX="163dp"
                    tools:layout_editor_absoluteY="16dp"/>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/viewName"
                        android:layout_width="140dp"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@id/textViewJobName1"
                        android:layout_alignTop="@id/textViewJobName1"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingVertical="10dp"
                        android:text=" Job:"
                        android:textAlignment="viewStart"
                        android:textColor="#FF000000"
                        android:visibility="visible" />

                    <TextView
                        android:id="@+id/textViewJobName1"
                        android:layout_width="300dp"
                        android:layout_height="wrap_content"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingLeft="10dp"
                        android:paddingVertical="10dp"
                        android:text="text"
                        android:textColor="#FF000000"

                        android:visibility="visible" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/viewDesc"
                        android:layout_width="140dp"
                        android:layout_height="match_parent"
                        android:layout_alignBottom="@id/textViewJobDescription1"
                        android:layout_alignTop="@id/textViewJobDescription1"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingVertical="10dp"
                        android:text=" Description:"
                        android:textAlignment="viewStart"
                        android:textColor="#FF000000"
                        android:visibility="visible" />

                    <TextView
                        android:id="@+id/textViewJobDescription1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_toEndOf="@+id/textName"
                        android:layout_weight="3"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingLeft="10dp"
                        android:paddingVertical="10dp"
                        android:text="TextView"
                        android:textColor="#FF000000"
                        android:visibility="visible"

                        />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/viewSize"
                        android:layout_width="140dp"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@id/textViewJobSize1"
                        android:layout_alignTop="@id/textViewJobSize1"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingVertical="10dp"
                        android:text=" Job Size:"
                        android:textAlignment="viewStart"
                        android:textColor="#FF000000"
                        android:visibility="visible" />

                    <TextView
                        android:id="@+id/textViewJobSize1"
                        android:layout_width="300dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_toEndOf="@+id/textName"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingLeft="10dp"
                        android:paddingVertical="10dp"
                        android:text="TextView"
                        android:textColor="#FF000000"
                        android:visibility="visible" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/viewType"
                        android:layout_width="140dp"
                        android:layout_height="wrap_content"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingVertical="10dp"
                        android:text=" Job Type:"
                        android:textAlignment="viewStart"
                        android:textColor="#FF000000"
                        android:visibility="visible" />


                    <TextView
                        android:id="@+id/textViewJobType1"
                        android:layout_width="300dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_toEndOf="@+id/textName"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingLeft="10dp"
                        android:paddingVertical="10dp"
                        android:text="TextView"
                        android:textColor="#FF000000"
                        android:visibility="visible" />

                </LinearLayout>


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="200dp">

                    <ExpandableListView
                        android:id="@+id/expandable_list_view"
                        android:layout_width="match_parent"
                        android:layout_height="fill_parent"
                        android:padding="10dp"
                        android:pointerIcon="arrow"
                        android:dividerHeight="3dp">

                    </ExpandableListView>

                </LinearLayout>


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>




            </LinearLayout>


            <!-- ACTIVE BIDS PART -->


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="10dp">

                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/bebasneueregular"
                    android:text="Active bids"
                    android:textAlignment="center"
                    android:textSize="30dp"
                    tools:layout_editor_absoluteX="163dp"
                    tools:layout_editor_absoluteY="16dp" />

                <SearchView
                    android:id="@+id/searchViewJob"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:iconifiedByDefault="false"
                    android:queryHint="Name, Location, Size, etc." />

                <ListView
                    android:id="@+id/jobListView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </LinearLayout>

        </LinearLayout>

    </ScrollView>
</FrameLayout>

我希望用户能够选择 Expandable ListView ,并显示不可滚动的子信息。有滚动视图到位的方法吗?

是否可以使 Expandable ListView 不可滚动或使其与ScrollView一起使用

编辑 - 使用NestedScrollView

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.kitkat.crossroads.JobBidsFragment">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:focusable="true"
            android:focusableInTouchMode="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textViewJobInformation"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/bebasneueregular"
                    android:text="Job Information"
                    android:textAlignment="center"
                    android:textSize="30dp"
                    tools:layout_editor_absoluteX="163dp"
                    tools:layout_editor_absoluteY="16dp"/>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/viewName"
                        android:layout_width="140dp"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@id/textViewJobName1"
                        android:layout_alignTop="@id/textViewJobName1"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingVertical="10dp"
                        android:text=" Job:"
                        android:textAlignment="viewStart"
                        android:textColor="#FF000000"
                        android:visibility="visible" />

                    <TextView
                        android:id="@+id/textViewJobName1"
                        android:layout_width="300dp"
                        android:layout_height="wrap_content"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingLeft="10dp"
                        android:paddingVertical="10dp"
                        android:text="text"
                        android:textColor="#FF000000"

                        android:visibility="visible" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/viewDesc"
                        android:layout_width="140dp"
                        android:layout_height="match_parent"
                        android:layout_alignBottom="@id/textViewJobDescription1"
                        android:layout_alignTop="@id/textViewJobDescription1"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingVertical="10dp"
                        android:text=" Description:"
                        android:textAlignment="viewStart"
                        android:textColor="#FF000000"
                        android:visibility="visible" />

                    <TextView
                        android:id="@+id/textViewJobDescription1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_toEndOf="@+id/textName"
                        android:layout_weight="3"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingLeft="10dp"
                        android:paddingVertical="10dp"
                        android:text="TextView"
                        android:textColor="#FF000000"
                        android:visibility="visible"

                        />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/viewSize"
                        android:layout_width="140dp"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@id/textViewJobSize1"
                        android:layout_alignTop="@id/textViewJobSize1"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingVertical="10dp"
                        android:text=" Job Size:"
                        android:textAlignment="viewStart"
                        android:textColor="#FF000000"
                        android:visibility="visible" />

                    <TextView
                        android:id="@+id/textViewJobSize1"
                        android:layout_width="300dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_toEndOf="@+id/textName"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingLeft="10dp"
                        android:paddingVertical="10dp"
                        android:text="TextView"
                        android:textColor="#FF000000"
                        android:visibility="visible" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/viewType"
                        android:layout_width="140dp"
                        android:layout_height="wrap_content"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingVertical="10dp"
                        android:text=" Job Type:"
                        android:textAlignment="viewStart"
                        android:textColor="#FF000000"
                        android:visibility="visible" />


                    <TextView
                        android:id="@+id/textViewJobType1"
                        android:layout_width="300dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_toEndOf="@+id/textName"
                        android:background="#FFFFFFFF"
                        android:gravity="center_vertical"
                        android:paddingLeft="10dp"
                        android:paddingVertical="10dp"
                        android:text="TextView"
                        android:textColor="#FF000000"
                        android:visibility="visible" />

                </LinearLayout>


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="200dp">

                    <ExpandableListView
                        android:id="@+id/expandable_list_view"
                        android:layout_width="match_parent"
                        android:layout_height="fill_parent"
                        android:padding="10dp"
                        android:pointerIcon="arrow"
                        android:dividerHeight="3dp">

                    </ExpandableListView>

                </LinearLayout>


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:orientation="horizontal">

                </LinearLayout>




            </LinearLayout>


            <!-- ACTIVE BIDS PART -->


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="10dp">

                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/bebasneueregular"
                    android:text="Active bids"
                    android:textAlignment="center"
                    android:textSize="30dp"
                    tools:layout_editor_absoluteX="163dp"
                    tools:layout_editor_absoluteY="16dp" />

                <SearchView
                    android:id="@+id/searchViewJob"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:iconifiedByDefault="false"
                    android:queryHint="Name, Location, Size, etc." />

                <ListView
                    android:id="@+id/jobListView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </LinearLayout>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>
</FrameLayout>

1 个答案:

答案 0 :(得分:2)

XML代码示例:

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header">

    <ScrollView
            android:fillViewport="true"
            android:layout_marginTop="130dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/homeLayout"
                    android:clickable="true"
                    android:gravity="center_vertical"
                    android:background="@drawable/layout_click_effect"
                    android:layout_width="match_parent"
                    android:layout_height="45dp">
                    <ImageView
                        android:id="@+id/homeIv"
                        android:layout_width="45dp"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/ic_home_black_24dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Home" />
                </LinearLayout>
                <View
                    android:background="@android:color/darker_gray"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"/>

                <ExpandableListView
                    android:id="@+id/topCatgExpLv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start"
                    android:groupIndicator="@null"
                    android:dividerHeight="1dp" />
        </ScrollView>
    </android.support.design.widget.NavigationView>

创建代码:

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

}