I have made an Android app containing a CoordinatorLayout with a AppBar and 
a NestedScrollView. 


In my NestedScrollView I have a list of items, and a button which scroll to 
an item in my NestedScrollView.

When clicking the button, the Android app scroll down to the item, but 
doesn't show the whole item, only partially: 
http://i.stack.imgur.com/8kuq0.png

I expected something like the following: http://i.stack.imgur.com/k0A5N.png


It seems like the amount the app needs to scroll is about the same size as 
the AppBar, in order to show the whole view. If I remove the scroll flag in 
my layout file below, I get the expected behavior.

I'm in doubt if View.requestRectangleOnScreen() is the right method to 
call, or if I should use a different layout then NestedScrollView? What do 
I do wrong?


My activity is coded as follows:


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()));
                    }
                });
            }
        };
    }
}


And the layout:
<?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>


-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/e979c2be-31c7-471e-ac7b-f934b8300d91%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to