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
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
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
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
I'm using a RecyclerView inside the bottom sheet from google support library. I have noticed that sometimes bottom sheet intercepts touch events. I have not found any answers to this issue.
My screen looks like this
Code for the layout:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/appColor">
<ui.view.GMapView
android:id="#+id/gMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<FrameLayout
android:id="#+id/mBottomSheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
app:behavior_peekHeight="160dp"
app:behavior_hideable="false"
android:fillViewport="true"
android:orientation="vertical"
android:background="#color/uiColor"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvAddresses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Thanks for any help!
For that you need to stop scrolling of recycler view, below is the solution for that,
android:nestedScrollingEnabled="true"
I want to add vertical as well as horizontal RecyclerViews inside a vertical scrollview.
Is it possible to have multiple recycler views inside vertcal scrollview
what control I need to use.
Also RecyclerView nested in one ScrollView is not good practice so can anybody tell me whats right way to do it can I add RecyclerView inside another RecyclerView or I need to use horizontal and vertical scrollview only to achieve this.
You can do something like this for getting two RecyclerView nested in one ScrollView which is by the way not recommended.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/tools"
android:focusableInTouchMode="true"
android:padding="8dp"
android:background="#drawable/old_map"
android:layout_height="match_parent">
<ScrollView
android:layout_below="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="600dp">
<view
android:scrollbarSize="6dp"
android:id="#+id/recent_post"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="400dp"
android:scrollbars="vertical" />
<view
android:scrollbarSize="6dp"
android:id="#+id/recent_post"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="400dp"
android:scrollbars="horizontal" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I don't namely know what kind of scrolling do you want but it is possible to use both scrolls at the same time:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/HorizontalScrollView02"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- HERE YOUR CODE-->
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>