android - 在 CoordinatorLayout 中滚动查看只显示部分 View

标签 android android-coordinatorlayout android-appbarlayout nestedscrollview

我制作了一个 Android 应用程序,其中包含一个带有 AppBar 和 NestedScrollView 的 CoordinatorLayout。在我的 NestedScrollView 中,我有一个项目列表,以及一个滚动到我的 NestedScrollView 中的项目的按钮。

单击按钮时,Android 应用向下滚动到该项目,但不显示整个项目,仅部分显示:http://i.stack.imgur.com/8kuq0.png

我希望得到如下结果:http://i.stack.imgur.com/k0A5N.png

为了显示整个 View ,应用程序需要滚动的量似乎与 AppBar 的大小大致相同。如果我在下面的布局文件中删除滚动标志,我会得到预期的行为。

我做错了什么?

更新 (12/01/2016):我更新了图片,所以它们更大了。此外,正如我在 Herry 的回复中所写,我怀疑 View.requestRectangleOnScreen() 是否是正确的调用方法,或者我是否应该使用与 NestedScrollView 不同的布局。

我的 Activity 编码如下:

package com.example.sij.coordinatorlayoutbug;

import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView itemToNavigateTo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        itemToNavigateTo = (TextView)findViewById(R.id.itemToNavigateTo);
        Button navigatorButton = (Button)findViewById(R.id.navigator_button);
        navigatorButton.setOnClickListener(navigateToItemListener());
    }

    View.OnClickListener navigateToItemListener() {
        return new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        itemToNavigateTo.requestRectangleOnScreen(
                            new Rect(
                                0, 
                                0, 
                                itemToNavigateTo.getWidth(),
                                itemToNavigateTo.getHeight()));
                    }
                });
            }
        };
    }
}

布局文件:

<?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:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways" />

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <Button
                android:id="@+id/navigator_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/scroll_to_item" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/item1"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lorem_ipsum"
                android:paddingBottom="20dp"/>

            <!-- Items 2 to 5 omitted for brevity -->

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/item6"/>
            <TextView
                android:id="@+id/itemToNavigateTo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lorem_ipsum"
                android:paddingBottom="20dp"/>              
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

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

最佳答案

你需要在你的

中添加android:fillviewport="true"
 <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:fillViewport="true">

关于android - 在 CoordinatorLayout 中滚动查看只显示部分 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34725441/

相关文章:

android - 在android自定义键盘中设置键盘模式

android - 如何将 RecyclerView 放在 CollapsingToolbarLayout 下面并在 Android 中折叠时响应工具栏?

android - 当运行时 View 可见性从“消失”更改为“可见”时,AppBar 布局自动展开

java - 折叠工具栏不会随着抽屉布局而折叠

android - 在 AppBarLayout 折叠之前防止 RecyclerView 在 AppBarLayout 下滚动

android - 应用程序 :elevation ="0dp" hide layout inside toolbar

Android Lollipop Wifi 套接字 java.net.ConnectException ETIMEDOUT

android - 解析日期并将其存储在 SQLite 数据库中

Android - 测试版与生产版

android - 膨胀类 androidx.constraintlayout.widget.CoordinatorLayout 时出错