ScrollView无法正常工作

时间:2016-01-19 10:02:13

标签: android android-layout scrollview android-scrollview

我有一个linearlayout和一个viewpager,它包含一个列表视图。我希望linearlayout(餐厅页面和图像)在向上滚动时上升。为此我试图使用scrollView但它不起作用。我该怎么办? Screenshot

这是xml -

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:fillViewport="true"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:id="@+id/ResView"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include layout="@layout/header_restaurant_page" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginTop="-40dp"
        android:background="#ffffff">

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:background="@android:color/black">

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:textAlignment="center"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Pre Order"
                android:id="@+id/textView8"
                android:gravity="center"
                android:textColor="#ffffff" />

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:textAlignment="center"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Pay Online"
                android:id="@+id/textView9"
                android:gravity="center"
                android:textColor="#ffffff" />
        </LinearLayout>

        <com.astuetz.PagerSlidingTabStrip
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="30dp"
            android:textSize="15sp"
            app:pstsIndicatorHeight="3dp"
            app:pstsShouldExpand="true"
            app:pstsIndicatorColor="@color/white"
            app:typeface="robotoRegular"
            android:textColor="#fff"
            android:background="@android:color/black"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            tools:context="com.csform.android.uiapptemplate.restaurant"
            android:background="#fff">

        </android.support.v4.view.ViewPager>
    </LinearLayout>

</LinearLayout>
</ScrollView>

Include标记用于餐馆详细信息的标题。 如果可能的话,我想在滚动时将PagerSlidingTabStrip(优惠,菜单,关于,评论)粘贴到顶部。

3 个答案:

答案 0 :(得分:0)

我不知道,但您可以尝试该代码

<?xml version="1.0" encoding="UTF-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include layout="@layout/tool_bar" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none">

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

答案 1 :(得分:0)

您可以使用CoordinatorLayoutTabLayout

轻松完成此操作

(如下例所示):

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <!-- <include layout="@layout/header_restaurant_page" />-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/black"
            android:orientation="horizontal"
            android:padding="20dp">

            <TextView
                android:id="@+id/textView8"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Pre Order"
                android:textAlignment="center"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/textView9"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Pay Online"
                android:textAlignment="center"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#ffffff" />
        </LinearLayout>

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbarmain"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

        <com.astuetz.PagerSlidingTabStrip
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="30dp"
            android:textSize="15sp"
            app:pstsIndicatorHeight="3dp"
            app:pstsShouldExpand="true"
            app:pstsIndicatorColor="@color/white"
            app:typeface="robotoRegular"
            android:textColor="#fff"
            android:background="@android:color/black"/>

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_below="@id/app_bar_layout" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

请记住,ScrollView只能有一个孩子。 顺便说一句。

答案 2 :(得分:0)

而是使用自定义视图寻呼机。

public class SmartViewPager extends ViewPager {
public SmartViewPager(Context context) {
    super(context);
    mGestureDetector = new GestureDetector(context, new XScrollDetector());
}
private GestureDetector mGestureDetector;
private boolean mIsLockOnHorizontalAxis = false;

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (!mIsLockOnHorizontalAxis)
        mIsLockOnHorizontalAxis = mGestureDetector.onTouchEvent(event);

    if (event.getAction() == MotionEvent.ACTION_UP)
        mIsLockOnHorizontalAxis = false;

    getParent().requestDisallowInterceptTouchEvent(mIsLockOnHorizontalAxis);
    return super.onTouchEvent(event);
}
private class XScrollDetector extends SimpleOnGestureListener {


    /**
     * @return true - if we're scrolling in X direction, false - in Y
     *         direction.
     */
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return Math.abs(distanceX) > Math.abs(distanceY);
    }

}