在底部工作表中向下滚动

问题描述 投票:0回答:5

我想在底部工作表完全向上时向下滚动视图(向上拖动到整个屏幕)。向上滚动时效果很好,但向下滚动时则不起作用。相反,整个底板都会下降。有什么出路吗?

主要活动

import android.os.Bundle;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button fab = (Button) findViewById(R.id.button);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BottomSheetDialog bottomSheetDialog  = new BottomSheetDialog(MainActivity.this);
                View parentView = getLayoutInflater().inflate(R.layout.sheetlayout,null);
                bottomSheetDialog.setContentView(parentView);
                BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from((View) parentView.getParent());
                bottomSheetBehavior.setPeekHeight(
                        (int) TypedValue.applyDimension(
                                TypedValue.COMPLEX_UNIT_DIP,100,getResources().getDisplayMetrics()
                        )
                );
                bottomSheetDialog.show();

            }
        });
    }
}

SheetLayout.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:text="button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1" />

    <Button
        android:text="button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2" />

    <Button
        android:text="button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button3" />

    <Button
        android:text="button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button4" />

    <Button
        android:text="button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button5" />

    <Button
        android:text="button6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button18" />

    <Button
        android:text="button7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button7" />

    <Button
        android:text="button8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button8" />

    <Button
        android:text="button9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button9" />

    <Button
        android:text="button10"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button10" />

    <Button
        android:text="button11"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button11" />

    <Button
        android:text="button12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button12" />

    <Button
        android:text="button13"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button13" />

    <Button
        android:text="button14"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button14" />

    <Button
        android:text="button15"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button15" />

    <Button
        android:text="button16"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button16" />

    <Button
        android:text="button17"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button17" />

    <Button
        android:text="button18"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button6" />
</LinearLayout>
    </ScrollView>

编辑1

<?xml version="1.0" encoding="utf-8"?>


<ScrollView
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent">

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:text="button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button1" />

        <Button
            android:text="button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button2" />

        <Button
            android:text="button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button3" />

        <Button
            android:text="button4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button4" />

        <Button
            android:text="button5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button5" />

        <Button
            android:text="button6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button18" />

        <Button
            android:text="button7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button7" />

        <Button
            android:text="button8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button8" />

        <Button
            android:text="button9"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button9" />

        <Button
            android:text="button10"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button10" />

        <Button
            android:text="button11"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button11" />

        <Button
            android:text="button12"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button12" />

        <Button
            android:text="button13"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button13" />

        <Button
            android:text="button14"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button14" />

        <Button
            android:text="button15"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button15" />

        <Button
            android:text="button16"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button16" />

        <Button
            android:text="button17"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button17" />

        <Button
            android:text="button18"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button6" />
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>
    </ScrollView>

编辑2

<?xml version="1.0" encoding="utf-8"?>

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

    <ScrollView
        android:layout_width="match_parent"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
        android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <Button
            android:text="@string/button1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/button1"
            android:layout_weight="0.66" />

        <Button
            android:text="button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button2" />

        <Button
            android:text="button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button3" />

        <Button
            android:text="button4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button4" />

        <Button
            android:text="button5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button5" />

        <Button
            android:text="button6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button18" />

        <Button
            android:text="button7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button7" />

        <Button
            android:text="button8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button8" />

        <Button
            android:text="button9"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button9" />

        <Button
            android:text="@string/button10"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button10" />

        <Button
            android:text="@string/button11"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button11" />

        <Button
            android:text="@string/button12"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button12" />

        <Button
            android:text="button13"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button13" />

        <Button
            android:text="button14"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button14" />

        <Button
            android:text="button15"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button15" />

        <Button
            android:text="button16"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button16" />

        <Button
            android:text="button17"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button17" />

        <Button
            android:text="button18"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button6" />
    </LinearLayout>
    </ScrollView>
</android.support.design.widget.CoordinatorLayout>
android scrollview bottom-sheet
5个回答
15
投票

我遇到了同样的问题并使用 android.support.v4.widget.NestedScrollView 修复了它。下面是我的底部表单布局代码

<?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"
    android:paddingBottom="8dp"
    android:paddingTop="8dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        android:orientation="horizontal"
        android:paddingBottom="8dp"
        android:paddingLeft="@dimen/activity_margin"
        android:paddingRight="@dimen/activity_margin"
        android:paddingTop="8dp">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginRight="32dp"
            android:src="@drawable/ic_email_black_24dp"
            android:tint="#737373" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Email a Copy"
            android:textColor="#737373"
            android:textSize="16sp" />

    </LinearLayout>

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

        <WebView
            android:id="@+id/webpage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"></WebView>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

8
投票

您只需要将 ScrollView

更改
NestedScrollView


2
投票

到目前为止我知道普通滚动视图不适用于Bottom-sheet,而向下滚动时,

Bottomsheet
也会
get down
,即
not expected behavior

您可以尝试 NestedScrollView,如下所示:

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="8dp"
        app:behavior_hideable="false"
        app:behavior_peekHeight="0dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">


        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:text="Any Long Text" />

        </androidx.appcompat.widget.LinearLayoutCompat>

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>


0
投票

我遇到了同样的问题,并使用 androidx.core.widget.NestedScrollView 修复了它,下面是我的底部表单布局代码。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <RelativeLayout
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--Add your UI here -->
    </RelativeLayout>
</androidx.core.widget.NestedScrollView>
</ScrollView>

0
投票

只需将

OnScrollChangeListener
添加到您的
ScrollView
并在滚动视图当前不在 y = 0 时禁用拖动。

    scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            bottomSheetBehavior.setDraggable(scrollY == 0);
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.