简体   繁体   中英

ScrollView with ListView and Scroll to position

Can somebody help? I have trouble with ScrollView and ListView. I need to scroll parentView(ScrollView) to position and then scroll ListView. I post screenshots for better understanding.

My xml:

<RelativeLayout 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.usoftek.masteryoga.Fragment.ProgramsOne.ProgramOne"
    android:background="@color/Background">

    <include
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/toolbar"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/toolbar" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/Text_Color_2"
        android:id="@+id/relativeLayout4"
        android:layout_below="@+id/toolbar">

        <RelativeLayout
            android:layout_width="240dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:background="@drawable/tabs_but_st"

            android:id="@+id/start">

            <ImageView
                android:layout_width="15dp"
                android:layout_height="20dp"
                android:id="@+id/imageView11"
                android:src="@drawable/play"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/textView21"
                android:layout_toStartOf="@+id/textView21" />

            <com.usoftek.masteryoga.MYCustomViews.MYTextView.Light
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Start Program"

                android:id="@+id/textView21"
                android:textSize="20sp"
                android:textColor="@color/Text_Color_3"
                android:layout_centerVertical="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginRight="40dp"
                android:layout_marginEnd="40dp"
                android:layout_marginLeft="15dp" />
        </RelativeLayout>
    </RelativeLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/ProgramScrollView"
        android:layout_below="@+id/relativeLayout4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:fillViewport="true">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/relativeLayout4"
            android:id="@+id/ProgramLay">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/descText">

                <com.usoftek.masteryoga.MYCustomViews.MYTextView.Light
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Advanced Bala Yoga for Back and Abs"
                    android:id="@+id/name_text"
                    android:layout_marginLeft="21dp"
                    android:layout_marginTop="15dp"
                    android:textSize="25sp"
                    android:textColor="@color/Text_Color_1"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="5dp" />

                <TableLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/name_text"
                    android:id="@+id/tableLayout"
                    android:layout_marginLeft="21dp">

                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <ImageView
                            android:layout_width="19dp"
                            android:layout_height="19dp"
                            android:id="@+id/imageView7"
                            android:src="@drawable/level_image_s"
                            android:scaleType="fitXY"
                            android:background="@android:color/transparent"
                            android:layout_marginRight="7dp" />

                        <com.usoftek.masteryoga.MYCustomViews.MYTextView.Light
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Level"
                            android:id="@+id/textView13"
                            android:textColor="@color/Text_Color_1"
                            android:layout_marginRight="52dp" />

                        <ImageView
                            android:layout_width="19dp"
                            android:layout_height="19dp"
                            android:id="@+id/imageView9"
                            android:scaleType="fitXY"
                            android:src="@drawable/goal_image_s"
                            android:layout_marginRight="7dp" />

                        <com.usoftek.masteryoga.MYCustomViews.MYTextView.Light
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Goal"
                            android:id="@+id/textView14"
                            android:textColor="@color/Text_Color_1"
                            android:layout_marginRight="52dp" />

                        <ImageView
                            android:layout_width="19dp"
                            android:layout_height="19dp"
                            android:id="@+id/imageView8"
                            android:src="@drawable/duration_image_s"
                            android:layout_marginRight="7dp" />

                        <com.usoftek.masteryoga.MYCustomViews.MYTextView.Light
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Time"
                            android:id="@+id/textView15"
                            android:textColor="@color/Text_Color_1"
                            android:layout_marginRight="52dp" />
                    </TableRow>

                    <TableRow
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp">

                        <com.usoftek.masteryoga.MYCustomViews.MYTextView.Light
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="New Text"
                            android:id="@+id/level_text"
                            android:layout_column="0"
                            android:textColor="@color/Text_Color_1"
                            android:layout_span="2" />

                        <com.usoftek.masteryoga.MYCustomViews.MYTextView.Light
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="New Text"
                            android:id="@+id/goal_text"
                            android:layout_column="2"
                            android:textColor="@color/Text_Color_1"
                            android:layout_span="2" />

                        <com.usoftek.masteryoga.MYCustomViews.MYTextView.Light
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="New Text"
                            android:id="@+id/time_text"
                            android:layout_column="4"
                            android:textColor="@color/Text_Color_1"
                            android:layout_span="2" />
                    </TableRow>
                </TableLayout>

                <com.usoftek.masteryoga.MYCustomViews.MYTextView.Light
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="This set is meant for dynamic young peple. Let your mind relax as you discover the abitiles that you body and mind offer you"
                    android:id="@+id/fullDesc_text"
                    android:layout_gravity="center_vertical"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:textColor="@color/Text_Color_1"
                    android:layout_marginLeft="21dp"
                    android:layout_below="@+id/tableLayout"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="10dp" />

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="30dp"
                    android:layout_below="@+id/fullDesc_text"
                    android:id="@+id/linearLayout3"
                    android:layout_marginLeft="21dp"
                    android:divider="@drawable/abc_list_divider_mtrl_alpha"
                    android:showDividers="middle">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1">

                        <ImageView
                            android:layout_width="15dp"
                            android:layout_height="15dp"
                            android:id="@+id/imageView2"
                            android:layout_centerVertical="true"
                            android:src="@drawable/down" />

                        <com.usoftek.masteryoga.MYCustomViews.MYTextView.Light
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Download program"
                            android:id="@+id/textView31"
                            android:gravity="center_vertical"
                            android:layout_centerVertical="true"
                            android:layout_toRightOf="@+id/imageView2"
                            android:layout_toEndOf="@+id/imageView2"
                            android:textColor="@color/Text_Color_2"
                            android:textSize="14dp"
                            android:layout_marginLeft="5dp"
                            android:singleLine="true" />
                    </RelativeLayout>
                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="@android:color/darker_gray"
                        android:layout_marginRight="10dp" />

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1">

                        <ImageView
                            android:layout_width="15dp"
                            android:layout_height="15dp"
                            android:id="@+id/imageView20"
                            android:layout_centerVertical="true"
                            android:src="@drawable/pencil" />

                        <com.usoftek.masteryoga.MYCustomViews.MYTextView.Light
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Edit program"
                            android:id="@+id/textView50"
                            android:layout_centerVertical="true"
                            android:layout_toRightOf="@+id/imageView20"
                            android:layout_toEndOf="@+id/imageView20"
                            android:textColor="@color/Text_Color_2"
                            android:textSize="14dp"
                            android:layout_marginLeft="5dp" />
                    </RelativeLayout>

                </LinearLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/img"
                    android:layout_below="@+id/linearLayout3">

                    <ImageView
                        android:layout_width="fill_parent"
                        android:layout_height="240dp"
                        android:id="@+id/programImg"
                        android:src="@drawable/layer8"
                        android:scaleType="centerCrop"
                        android:layout_alignParentLeft="false"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentEnd="true"
                        android:layout_alignParentBottom="false"
                        android:layout_alignParentTop="false"
                        android:layout_alignParentRight="false"
                        android:layout_margin="2dp" />

                    <ProgressBar
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/ImgLargeProgress"
                        android:layout_centerInParent="true" />
                </RelativeLayout>
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/descText">

                <com.usoftek.masteryoga.MYCustomViews.MYListView.NestedListView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/asanaslist"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />
            </RelativeLayout>

        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

In Java nothing interesting just setting Adapter and for list I have found this solution

That's what I have

在此处输入图片说明

That's what I need (after scrolling parent view to position to scroll ListView)

在此处输入图片说明

That's what I have with Scrolling

在此处输入图片说明

You are not supposed to use ListView inside a ScrollView ideally because their scroll events will collide. But can be done like this .

You can achieve this using design support library widgets like android.support.design.widget.CollapsingToolbarLayout and android.support.design.widget.NestedScrollView. Checkout this github samples of these widgets in this link https://github.com/sitepoint-editors/Design-Demo .

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