In activity, I have TabLayout & FrameLayout for loading fragment. fragment contains RecyclerView. it works fine for first time only. but when I change tab and back to previous tab the RecyclerView not scrolling full.
Main Activity
<android.support.v4.widget.NestedScrollView
android:fillViewport="true"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/tabMain"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<FrameLayout
android:id="#+id/containerMain"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Fragment
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvMedia"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false" />
</LinearLayout>
The recyclerView has a smooth scrolling by itself but when we need to put recyclerView within any scrollView it will not work like the below:
Layout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<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">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
The solution for this is we need to used nestedScrollView instead of scrollview like the below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
The problem occurs when we use nestedScrollView and put recyclerView inside nestedScrollView is, it scrolls in various speed depending on gesture. The scrolling feature will not be smooth.
So to fix this issue all you have to do after setting your adapter is to add this line ViewCompat.setNestedScrollingEnabled(recyclerView, false);
This is not a good solution. Placing a RecyclerView inside a NestedScrollView, causes ALL elements of the RecyclerView’s adapter to be rendered, ths using alot of memory. This can be so slow in most devices with less memory.
This approach might also lead to disabling need scrolling, which will disable views recycling thus all items will be initialized at once.e.g. In a list with 1000 items. This will make the application lag. You can avoid it if you use pagination where you load a fixed number of items when the user scrolls down on the list.
Read more about pagination.
Pagination with RecyclerView – Etienne Lawlor – Medium
Android RecyclerView Pagination with Paging Library using MVVM ...
Paging library overview | Android Developers
Related
So,
I have a layout.xml file which contains a Recycler View. Now, what I want is, I added two image Views on top of this Recycler View but I want those ImageViews to scroll with the RecyclerView as well.
I just wanted to ask, is this possible to do? And if yes, how can I achieve this?
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/content_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include
android:id="#+id/header_bar"
layout="#layout/section_header" />
<ImageView-1>
<ImageView-2>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/category_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingHorizontal="#dimen/category_grid_edge_space"
android:paddingTop="#dimen/category_grid_padding_top"
android:scrollbarSize="#dimen/grid_padding"
android:scrollbarStyle="outsideOverlay"
android:scrollbarThumbVertical="?android:attr/textColorSecondary"
android:scrollbars="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Put your RecycleView in NestedScrollView
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
layout="#layout/your_header"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/category_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
also don't forget to disabled NestedScrolling for RecyclerView
myRecyclerView.setNestedScrollingEnabled(false);
OR
Add those views as a header. Here is a good guide on how to add a custom item view: https://medium.com/androiddevelopers/get-ahead-using-headers-in-recyclerview-2909a69b19
https://github.com/masudias/RecyclerView-with-Header-and-Footer
You can use header and footer i
I want to create a layout that has for example the main layout is a vertical LinearLayout, inside it is a TextView and a wrapped RecyclerView. I want to add a SwipeRefreshLayout that only can be swiped outside the RecyclerView.
Here is the simple XML for this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="40dp"
tools:context=".fragments.warehouse.WHTabOne">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Where should I put the SwipeRefreshLayout to make it only works for outside the RecyclerView?
Make SwipeRefreshLayout your root layout and put everything inside it. Example:
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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"
android:id="#+id/swipe_refresh_layout"
android:padding="40dp"
tools:context=".fragments.warehouse.WHTabOne">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
What I was asking for is to prevent the SwipeRefreshLayout from starting when swiping on RecyclerView only, and swiping everywhere else activate SwipeRefreshLayout.
But I found that the RecyclerView is by default overrides SwipeRefreshLayout until it reaches the top of the RecyclerView then it start to activate SwipeRefreshLayout. I thought it won't work because my list was too short and could't notice that. Now that I've added more items to the list, it's clear. So basically, adding everything under SwipeRefreshLayout is the right thing by default it will make it in right way.
I have an activity that have viewpager inside scrollview like the follwoing
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--someViews-->
</LinearLayout>
<!--then viewPager-->
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
Then the view pager holds some fragments which have a tall view too.
The problem is the scroll view not scrolling because of the view is not need to scroll but the view inside the viewpager fragment's not scrolling
so, Is it possible to make the whole view including the inside view pager scroll as one view?
What I am saying that I don't need to scroll the viewpager fragment as separated part I need to scroll it as part of activity view.
Thanks
I believe the setup you are looking for would work inside a CoordinatorLayout using the NestedScrollView widget like so:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Display1" />
<TextView
android:id="#+id/subTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/subtitle"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Body1" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="300dp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I have just tested this with a sample project with a single text view in a Fragment and I was able to both scroll the view and swipe through the pages.
Please note though: I did have to provide a specific height for the ViewPager in order for it to display.
I want to have scrollable screen with RecyclerView as one of its children, hierarchy would look like that:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
...
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
.../>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
.../>
<FrameLayout
.../>
<android.support.v7.widget.RecyclerView
android:id="#+id/list_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:nestedScrollingEnabled="false"/>
<FrameLayout
.../>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
Note that all the items of RecyclerView should be visible and be siblings of ImageView, FrameLayout etc.
In the current solution, there is one significant issue, onBindViewHolder is called for all items at once, but I want them to be bind when they appear on the screen, like in standard RecyclerView. I was doing some experiments with
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
but it failed after all.
I know one of the solutions is implementing heterogeneous recyclerView, but I would like to avoid it. Any ideas?
Probably it happens because in this case, recyclerview cant know the height of itself.
Can you try:
layoutManager.isAutoMeasureEnabled=true
How can I have a ViewPager above a RecyclerView, or have several RecyclerViews in a ScrollView? I designed the following layout, but it has some problems when scrolling.
What is the problem and how can I solve it?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dcdcdc"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".ActivityMain"
tools:showIn="#layout/app_bar_main">
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="200dip"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</ScrollView>
I have tried compile 'com.bartoszlipinski:recyclerviewheader2:2.0.1' with
<com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top">
1- firstly it supports 11 sdk or greater,
2- secondly ViewPager wont work in header RecyclerView.
and i have tried RecyclerView layout_width="wrap_content" but it is just supported in sdk21 .
How can I solve the RecyclerView scrolling ?
Try losing the top ScrollView and using a ListView instead of RecyclerView.
ScrollView is unnecessary here and you need a hack for ViewPager to work with RecyclerView as it consumes all touch events. It would be simpler if you could get away with a ListView.
You can take your view pager to app bar in collapssing toolbar layout and it will be ok