1

I want to have a NestedScrollView in my CoordinatorLayout but the scrolling just never works. I don't see what I'm doing wrong here. Is there an issue with the NestedScrollView being inside the CoordinatorLayout? Could someone tell me why my layout isn't scrollable? Here was my attempt:

comments.xml:

<?xml version="1.0" encoding="utf-8"?>
 <android.support.design.widget.CoordinatorLayout
    xmlns:flatui="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fresco="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    android:id="@+id/comments_coordinator_layout">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/comments_appbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </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"
        android:fillViewport="true">

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

            <RelativeLayout
                android:layout_marginTop="?attr/actionBarSize"
                android:id="@+id/view_post"
                android:layout_width="match_parent"
                android:paddingRight="5dp"
                android:paddingLeft="5dp"
                android:orientation="horizontal"
                android:layout_height="175dp"
                android:background="#e6e6e6">

                <com.facebook.drawee.view.SimpleDraweeView
                    android:layout_marginTop="15dp"
                    android:id="@+id/poster_picture"
                    android:layout_width="75dp"
                    android:layout_height="75dp"
                    android:layout_marginLeft="10dp"
                    fresco:placeholderImage="@mipmap/blank_prof_pic"
                    fresco:roundedCornerRadius="5dp"
                    fresco:roundAsCircle="true"
                    />

                <TextView
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="15dp"
                    android:layout_toRightOf="@id/poster_picture"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:textStyle="bold"
                    android:id="@+id/poster_name"/>

                <TextView
                    android:layout_alignParentRight="true"
                    android:layout_marginTop="15dp"
                    android:layout_toRightOf="@id/poster_name"
                    android:layout_marginLeft="5dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:id="@+id/post_date"/>

                <TextView
                    android:layout_marginLeft="5dp"
                    android:layout_toRightOf="@id/poster_picture"
                    android:layout_below="@id/poster_name"
                    android:textSize="20sp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/view_status" />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:orientation="vertical"
                    android:id="@+id/container_list"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_above="@+id/send_message">
                </LinearLayout>


                <LinearLayout
                    android:id="@+id/send_message"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:layout_alignParentBottom="true"
                    android:orientation="horizontal" >

                    <com.cengalabs.flatui.views.FlatEditText
                        android:id="@+id/write_comment"
                        android:layout_width="0dp"
                        android:layout_height="40dp"
                        android:layout_weight="5"
                        android:paddingLeft="10dp"
                        android:gravity="bottom|left"
                        android:cursorVisible="false"
                        android:hint="Comment back!"
                        android:inputType="textMultiLine"
                        flatui:fl_fieldStyle="fl_box"
                        android:scrollHorizontally="false" />

                    <com.cengalabs.flatui.views.FlatButton
                        android:id="@+id/send_comment"
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:layout_marginLeft="5dp"
                        android:layout_gravity="center_vertical|center_horizontal"
                        android:gravity="center"
                        android:text="send"
                        flatui:theme="@array/sea"
                        flatui:fl_textAppearance="fl_light"/>
                </LinearLayout>

            </RelativeLayout>

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

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

EDIT: Adding in screen shot and layout initialization:

AndroidManifest.xml:

   <activity android:name=".com.tabs.activity.Comments"
        android:label="View Post"
        android:theme="@style/AppTheme.NoActionBar"
        android:configChanges="keyboardHidden"
        android:windowSoftInputMode="adjustResize|stateVisible|stateAlwaysHidden">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".com.tabs.activity.news_feed"/>
    </activity>

The reason why I need to have my Layout scrollable is because of this piece of code in Comments.java. This looks in my database and dynamically adds views into the containerList layout I have described in comments.xml. Comments.java:

public void populateComments(long postId) {
    //Prepare layout inflater and container
    //This is for populating the container container_list
    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout containerItems = (LinearLayout) findViewById(R.id.container_list); // this replaces your listview
        for (Comment i : commentsDatasource.getCommentsForPost(postId)) {
            View childView = vi.inflate(R.layout.comment_item, null);
            TextView message = (TextView)childView.findViewById(R.id.comment_message);
            TextView name = (TextView) childView.findViewById(R.id.commenter_name);
            TextView timestamp = (TextView) childView.findViewById(R.id.comment_timestamp);
            message.setText(i.getComment());
            name.setText(i.getCommenter());
            timestamp.setText(i.getTimeStamp());
            //Set profile picture
            DraweeController controller = news_feed.getImage(i.getCommenterUserId());
            SimpleDraweeView draweeView = (SimpleDraweeView) childView.findViewById(R.id.commenter_profile_photo);
            draweeView.setController(controller);
            containerItems.addView(childView);
        }
}

Screenshot of what is happening: You can't see it but my keyboard covers my LinearLayout called container_list.

How it looks

Edit: Added screenshot when I added in getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); in my Activity

Screen shot

user1871869
  • 3,317
  • 13
  • 56
  • 106

1 Answers1

0

You haven't set any scroll flags to your Toolbar try adding this to your Toolbar in your layout:

app:layout_scrollFlags="scroll|enterAlways"
vguzzi
  • 2,420
  • 2
  • 15
  • 19
  • If I add this however, my `ToolBar` will be able to be scrolled and I don't want to do that. I would rather just be able to scroll through all the contents below my `ToolBar`. Any other suggestions? – user1871869 Dec 11 '15 at 08:49
  • Does it scroll if you put in this line? – vguzzi Dec 11 '15 at 08:50
  • Yes, it does scroll, but only for the length of the ToolBar. – user1871869 Dec 11 '15 at 08:51
  • Alright, then it seems your view isn't scrolling just because it doesn't need to? If there is enough room to display the content it won't scroll. – vguzzi Dec 11 '15 at 08:51
  • If ^ is not the case, could you post some screenshots and your layout initialization code. – vguzzi Dec 11 '15 at 08:52
  • Hm. Well it should be scrollable because if I click the `EditText` on the bottom of the screen, I should be able to scroll through the entirety of the layout when the keyboard pops up. Yes Let me provide a screen shot and the layout initialization. – user1871869 Dec 11 '15 at 08:53
  • Try running this code when your `Activity` is created: `getActivity().getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);` – vguzzi Dec 11 '15 at 08:54
  • I added in more code and a screen shot of what is happening. Let me try what you just suggested and I will respond back in a second. – user1871869 Dec 11 '15 at 09:02
  • This seems to help but it only displays my `ListView` and hides the top `RelativeLayout`. Furthermore, I can't seem to scroll with this `ListView`. Any ideas why? I will post a screen shot of what happened. – user1871869 Dec 11 '15 at 09:05
  • Have a play around with setting different soft input modes for your keyboard while I do some more research, I suspect this is your issue. See the ENUM here - http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html – vguzzi Dec 11 '15 at 09:08
  • Thanks! I appreciate your help. I am deifnitely looking at that. Yeah, Ideally I guess I would just want the entire layout to be scrollable, and when you click on the `EditText` you would shift up all the layout contents and can also scroll through the layouts even if you have the keyboard there – user1871869 Dec 11 '15 at 09:11
  • Could you input more items into your list to see if the scroll works when the keyboard isn't displayed? – vguzzi Dec 11 '15 at 09:15
  • so I put a lot of items into the list and unfortunately the scroll doesn't work.. is it because I add the items in the Activity and the scroll cannot handle that because it is in the Layout? – user1871869 Dec 11 '15 at 09:24
  • It seems your trying to use a very old method of creating a List, I know this probably isn't the answer your looking for, but especially because your using material design features, I'd recommend switching to a RecyclerView instead of dynamically adding views to a LinearLayout. Its actually easier too! Try following this guide to set it up - http://www.truiton.com/2015/02/android-recyclerview-tutorial/ I did try and find a way of solving the issue whilst keeping to this method but there is very little about it as using a ListView or RecyclerView handles all of this for you! – vguzzi Dec 11 '15 at 09:33
  • .. And it may be because of this that the quite new `NestedScrollView` doesn't like your implementation as it's not used to views being added at runtime without the use of a `ListView` or `RecyclerView`. – vguzzi Dec 11 '15 at 09:34