Android - Recycleview Inside Scrollview - android

I am building a social app and have an activity for viewing user's profiles (like Instagram).
I basically want to be able to scroll normally and naturally from the ScrollView to the RecyclerView and viceversa.
So for example, if the user does a strong swipe from the bottom of the RecyclerView I want it to take him up to the top of the ScrollView . I was thinking of the following solution (but don't really know how to do it): make the RecyclerView's height "expanded" so it would contain all the items but with no scrollbar, so that the ScrollView can do it's a natural thing.
My layout looks like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- FIRST REGION THIS IS SET PRGRAMATICALLY TO TAKE UP WHOLE SCREEN-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView> <Textview> <Bla> <Bla>
</RelativeLayout>
<!-- SECOND REGION ALSO SET TO TAKE UP ALL SCREEN PROGRAMATICALLY-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
/>
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</RelativeLayout>
Screenshots:
First Region
Second Region

use this mRecyclerView.setNestedScrollingEnabled(false);

Related

scroll ListView inside a LinearLayout which is wrapped in ScrollView

I have an activity which should be scrollable, therefore i surrounded it with a ScrollView. I have two listViews in there, which i do not want to be as large as it would be necessary to display all items.
It would be nice if i could set a maxHeight property, so that in case there are only 2 items no empty space would be present, but if there are 50 items i would only want to see like 5 of them at a time. Unfortunately there is no such property, so i decided to just set the height to a fixed number. Any advice how to do that more properly would be much appreciated.
However the main problem is that, when i try to scroll one of the listViews the whole LinearLayout scrolls down. (I can avoid this if i use another finger to 'hold' the LinearLayout in place while scrolling the list, but that certainly not a solution.)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_settings"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.ballmaschine.pages.SettingsPage">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Some other stuff here -->
<TextView
android:text="Eine Maschine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Medium"
android:layout_marginTop="8dp" />
<ListView
android:layout_width="match_parent"
android:id="#+id/lvCalibsM1"
android:layout_height="200dp" />
<TextView
android:text="Zwei Maschinen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Medium"
android:layout_marginTop="8dp" />
<ListView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/lvCalibsM2"/>
<!-- Some more stuff here -->
</LinearLayout>
Add this to the ListView
android:scrollbars = "none"
and this in Java Code
listView.setNestedScrollingEnabled(false)
In these cases always try NestedScrollView and also add this line to it
app:layout_behavior="#string/appbar_scrolling_view_behavior"

Multiple horizontal RecyclerView inside NestedScrollView steal focus

Actually I'm currently working for a AndroidTV app. I have multiple horizontal RecyclerView right to left inside a NestedScrollView like that image.
Problem is that when I scroll more towards left, then focus moves to different list or different view which is not good.
I don't want the focus to change. If the list reaches to the end, then focus should remain at same position.
I tried :
android:descendantFocusability="blocksDescendants"
android:focusableInTouchMode="true" //in parent layout
But it didn't work..
Can anyone help me out ??
Not solved
Try changing your ScrollView to NestedScrollView. A reason behind this is
**NestedScrollView**
NestedScrollView is just like ScrollView, but it supports acting as
both a nested scrolling parent and child on both new and old versions
of Android. Nested scrolling is enabled by default.
**ScrollView**
Layout container for a view hierarchy that can be scrolled by the
user, allowing it to be larger than the physical display. A ScrollView
is a FrameLayout, meaning you should place one child in it containing
the entire contents to scroll; this child may itself be a layout
manager with a complex hierarchy of objects
This will help you to determine which layout is being focused.
You can use below structure for nested scroll
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:clickable="false"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:id="#+id/scroll_search_all"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:isScrollContainer="false"
android:nestedScrollingEnabled="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:isScrollContainer="false"
android:nestedScrollingEnabled="false" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
I hope this will help!
Try using this one code in your recycleview section 2:
android:layoutDirection="rtl"

Scroll ListView instead of his Items

I have a layout which is divided in other two layouts: a LinearLayout for the "Header" and a RelativeLayout for the "Content".
In the content layout I have a ListView that needs to grown when the user scrolls, and hide this same ListView behind the header layout.
Basically, I need something like this:
What would be the best aproach to do something like this? This is my layout.xml right now:
<LinearLayout
android:id="#+id/layout_full"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_base"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:weightSum="1"
tools:context=".app.Main">
<!-- Header -->
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".55"
android:background="#drawable/background_header_small"
android:gravity="center_horizontal">
</LinearLayout>
<!-- Content -->
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight=".45">
<ListView
android:id="#+id/listview_full"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/text_add"
android:layout_alignEnd="#+id/text_add"
android:layout_alignStart="#+id/text_add"
android:divider="#null"
android:dividerHeight="0dp"
android:showDividers="none"
android:layout_marginBottom="10dp"
android:footerDividersEnabled="false"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
</LinearLayout>
Can I somehow remove the elevator component of the ListView, and scroll the ListView instead of the Items inside the ListView?
Apologies, I can't comment yet. But you probably want to use the CoordinatorLayout as your parent, instead of the LinearLayout you're using now. From there you have two options: the hackier way is to use a transparent CollapsingToolbar that is placed underneath the header, the other way is to create your own Behavior that will increase the high/top of the List.
This is under the assumption that the ListView will continue to scroll as normal when the the top of it reaches the top of the screen?
I can throw something together when I get home if this seems like it's on the right track.

Android Scrollview Won't fill parent space

I have a relative layout fills the entire screen. I want to add a scrollview in the middle of it to wrap a bunch of content so that I can make it pan up when the soft keyboard gets displayed. However, as soon as I wrap it in a scrollview, the bottom most layout stops filling the remainder of the screen.
Here is the XML where I have the ScrollView commented out.
<include
android:id="#+id/top_bar_with_save_button"
layout="#layout/top_bar_with_save_button"/>
<FrameLayout
android:id="#+id/log_entry_title_frame"
android:layout_below="#id/top_bar_with_save_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/log_entry_title_frame"
android:layout_alignParentBottom="true">
<!--
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/log_entry_title_frame"
android:orientation="vertical"
android:background="#f00">
<!-- Lots of stuff in here -->
<EditText
android:id="#+id/log_entry_notes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:gravity="top|left"/>
</RelativeLayout>
<!--
</ScrollView>
-->
</LinearLayout>
It looks like this:
But as soon as I remove the comment from the ScrollView it immediately compresses like this:
Why does this happen? I need it to fill the entire space on the screen, and I cannot figure out what is happening. Can anyone tell me how to fix this? Thanks!
You need to add the fillViewport="true" attribute to your ScrollView tag. Or you can add it programmatically with scrollView.setFillViewPort(true);. Otherwise, the ScrollView will wrap to its child's content height.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
... >
layout_height of scrolling container should be "wrap_content".

How to Prevent Scrolling within FrameLayouts and Instead Have the Parent LinearLayout Handle all Scrolling?

I have a scrollable LinearLayout parent with a few FrameLayouts nested inside it as follows:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical"
android:orientation="vertical">
<FrameLayout
android:id="#+id/hb_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/gn_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/yt_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I would like FrameLayouts to take up the space they need, allowing for scrolling of the full view through the LinearLayout.
Issue is: the LinearLayout will only take up the screen, it won't scroll, instead the FrameLayout's will scroll if the content overflows the bottom of the screen.
To be clear, I just want the FrameLayouts to fill whatever space they need, and the LinearLayout can scroll it like one long view. Must I fix the FrameLayout heights to achieve this? If so, is there risk of my layout breaking on different screen sizes/densities (or does DP really work in all cases?).
Thank you immensely!
EDIT: I have confirmed that setting fixed heights in the FrameLayout does exactly what I want this to do: scroll as one. However, why doesn't wrap_content measure the height and then go from there? That is what I expected the case was... I'm not certain how to judge the right heights for each element.
Try adding a scrollview to the layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<FrameLayout
android:id="#+id/hb_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/gn_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/yt_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>

Categories

Resources