Horizontal RecyclerView inside vertical ScrollView - android

So I have a horizontal RecyclerView inside a vertical ScrollView. Everything inside my layout is displayed fine and it all scrolls in the directions I want and it does it smoothly.
The only problem I have, is that the RecyclerView is below some other content in the ScrollView and when the RecyclerView is partially visible, it will line the bottom of the RecyclerView with the bottom of the screen on start-up. This means that the content above the RecyclerView is pushed off the screen.
Does anyone know why this happens, and how I can fix it?
Here is a simple layout that does what I just described. You don't even need to populate the RecyclerView, it will still do it.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#fff"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#000"/>
</LinearLayout>
</ScrollView>

Turns out this issue was reported to Google here Issue - 81854
According to Google it is working as intended. The problem is the fact that RecyclerView has focusableInTouchMode set to true. To fix the problem I set focusableInTouchMode and focusable to true on the top-most view of the ScrollView.
Below is the fix for the code sample I provided in the original question:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#fff"
android:focusableInTouchMode="true"
android:focusable="true"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#000"/>
</LinearLayout>
</ScrollView>

I have been looking for a solution for a long time. In the end, I decided by accident. In your activity, find the ScrollView and add a touch listener for it. Everything will work as it should
ScrollView scroll = findViewById(R.id.scroll);
scroll.setOnTouchListener((view, motionEvent) -> {
return false;
});

Related

How to scroll a ListView without using NestedScrollView?

This question has probably been asked at least a dozen times in the past. Seen previous responses but still can't seem to get it to work
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/colors_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
I do have a workable solution which I do not wish to use since I am told there is no need to use NestedScrollView since ListView is scrollable by itself. Yet, I can't seem to scroll my ListView if I use it by itself. Why?
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/layout_background_color">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical">
<ListView
android:id="#+id/colors_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
I guess the big problem here that is you set ListView with android:layout_height="wrap_content", it can not scroll because of its height always increases by its children.
Take a try with android:layout_height="match_parent". (Magic happened!)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical">
<ListView
android:id="#+id/colors_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Your nested scrollview always push listview to scroll down. Wraping listview or recyclerview with scrollview is bad way, your ui can be heavy. Try another method like recyclerview multiple view holder for better building scrolling ui with list

How to prevent ScrollView scrolling down when a child has textIsSelectable="true" attribute?

Previously, this was the layout for my DialogFragment:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".InfoDialog"
android:id="#+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- (More Views here) -->
</LinearLayout>
</ScrollView>
It works fine. However, I need to make textView1 selectable. But when I add the android:textIsSelectable="true" attribute to it, scrollView is scrolled down a bit when the dialog is displayed. (NB - I am able to scroll it up to the top manually, but that's obviously not a solution.)
I've tried adding android:descendantFocusability="beforeDescendants" to scrollView, but that hasn't worked.
I've also tried this in my onCreateDialog() and onViewCreated() methods:
ScrollView scrollView = layoutView.findViewById(R.id.scrollView);
scrollView.fullScroll(ScrollView.FOCUS_UP);
scrollView.scrollTo(0, 0);
But that hasn't worked either - ie, scrollView is still scrolled down a bit when the Dialog is shown.
Any ideas?
Update:
Using this to the end of my onCreateDialog() works:
ScrollView scrollView = layoutView.findViewById(R.id.scrollView);
scrollView.post(() -> {
scrollView.fullScroll(ScrollView.FOCUS_UP);
});
However, it's not an ideal solution (esp. as you can see the scroll being performed on-screen). Is there a better solution, preferably xml?

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"

can i create a nestedScroll Layout like this?

I think it can be achieved by NestedScrollingChild NestedScrollingParent.
But I can't really understand them.Who can help me out!
Product manager insists on the design.
The ScrollView contains a LinearLayout ,a "TabLayout" and a ViewPager.
The ViewPager contains 2 fragment contains RecyclerView or just only 2 RecyclerView.
When the ScrollView scroll to Bottom , the RecyclerView can scroll to Bottom.
When the RecyclerView scroll to Top , the ScrollView can scroll to Top.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="1000dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="60dp"></TableLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>
</ScrollView>
Just change your ScrollView to android.support.v4.widget.NestedScrollView
Post your xml if that doesn't work
ِYou can do that, but it's not good at all to use this type of layout
First you need add "wrap_content" abilty ViewPager and RecyclerView's
ViewPager and RecyclerView
Now you can use scrollview easily, put all item in it
If you want some effect or something when user scrolling or some item get visible in scrolling you can get position of item then make listener on
"OnScrollChangedListener" of scrollview

ScrollView white space at bottom

I'm having an issue with ScrollView which leaves a blank space at the bottom. It is filled with few TextViews and GridViews and should fill the whole RelativeLayout parent.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".showPictures">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView.../>
<GridView.../>
...
</LinearLayout>
</ScrollView>
</RelativeLayout>
Does anybody have an idea what's wrong?
Make your relative layout height match parent, also use android:fillViewPort = "true" in your scroll view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".showPictures">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
//...
you can use this code to avoid extra padding on bottom of scroll view:
android:overScrollMode="never"
In my case I had a GridView inside a ScrollView that was causing this issue. It turns out the the layout_gravity in the GridLayout as 'center' was causing this issue.
'center_horizontal' makes more sense for a ScrollView anyway, but I added the ScrollView after the original layout was done (using 'center'), when I saw that the elements were going off-screen on some devices, hence the issue.
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" // having this as android:layout_gravity="center" causes extra space at bottom!
android:columnCount="2">
....
Give weight to every child in the scroll view including linear layout with value 1

Categories

Resources