How to scroll a ListView without using NestedScrollView? - android

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

Related

RecyclerView inside NestedScrollView inside SwipeRefreshLayout doesn't scroll smoothly

Here's my layout.
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/activity_main_swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_marginBottom="45dp"
android:layout_height="wrap_content">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true">
<Button
android:layout_width="300dp"
android:layout_height="100dp"
android:id="#+id/noob_button"
android:text="haha" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/noob_button"
android:orientation="vertical"
android:clipToPadding="false"
android:scrollbars="vertical" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
This is all inside RelativeLayout.
I have set recyclerview.setNestedScrol..(false);
It still hangs and if I set fixed height of the RecyclerView it doesn't hang. It scrolls but the scroll is not smooth.
You do not actually need a RecyclerView inside a NestedScrollView. As far as I have understood your question, you need a Button and a RecyclerView under the button. So you might consider adding the Button as the header of the RecyclerView which is neater implementation.
If you are thinking of adding a header in your RecyclerView please see my answer here on how this can be achieved.
I have explained how a footer can be added. The same rule applies for adding a header view as well. Please let me know if you have any further questions regarding this.

Android - Recycleview Inside Scrollview

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

Horizontal RecyclerView inside vertical ScrollView

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

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>

Overlay for RelativeLayout inside ListView with match_parent

I am dealing with a weird problem.
I am using the following layout for ListView rows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:jn="http://schemas.android.com/apk/res/com.appeaser.justnoteproject"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<BUNCH OF VIEWS />
<RelativeLayout
android:id="#+id/rlCon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="#dimen/margin_note_bottom"
android:layout_marginLeft="#dimen/margin_note_left"
android:layout_marginRight="#dimen/margin_note_right"
android:layout_marginTop="#dimen/margin_note_bottom" >
<BUNCH OF VIEWS />
<View
android:id="#+id/vOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/blue_overlay"
android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>
In my list adapter, I toggle vOverlay's visibility based on a certain condition. But, even after toggling the visibility, vOverlay is not displayed.
To verify that the code works as intended, I changed the height and width of vOverlay to 50dp. When I did this, vOverlay's visibility worked as intended - except that the size I need is match_parent.
Could someone explain what I'm doing wrong? How can I get intended results?
replace outer RelativeLayout with FrameLayout and move your overlay to be a second child of that FrameLayout

Categories

Resources