简体   繁体   English

带有ListView的ScrollView并滚动到位置

[英]ScrollView with ListView and Scroll to position

Can somebody help? 有人可以帮忙吗? I have trouble with ScrollView and ListView. 我在ScrollView和ListView上遇到麻烦。 I need to scroll parentView(ScrollView) to position and then scroll ListView. 我需要滚动parentView(ScrollView)定位,然后滚动ListView。 I post screenshots for better understanding. 我发布了屏幕截图以便更好地理解。

My xml: 我的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 在Java中,仅设置Adapter并没有任何有趣的事情,我找到了这个解决方案

That's what I have 那就是我所拥有的

在此处输入图片说明

That's what I need (after scrolling parent view to position to scroll ListView) 那就是我需要的(将父视图滚动到滚动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. 理想情况下,您不应该在ScrollView使用ListView因为它们的滚动事件会发生冲突。 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. 您可以使用设计支持库小部件(例如android.support.design.widget.CollapsingToolbarLayout和android.support.design.widget.NestedScrollView)来实现此目的。 Checkout this github samples of these widgets in this link https://github.com/sitepoint-editors/Design-Demo . 在此链接https://github.com/sitepoint-editors/Design-Demo中检出这些小部件的github示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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