This question already has answers here:
RecyclerView inside ScrollView is not working
(26 answers)
Closed 2 years ago.
i have
MAIN VIEW
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_products"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
/>
</ScrollView>
On my template adapter, i have a another recycler adapter for child views
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/title_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".2"
android:background="#color/GRAY"
android:text="TextView" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_for_specification_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:nestedScrollingEnabled="false"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
The code work, but when user scroll on android device, the parent scrollView does not allow me to scroll over the recycler view (1)
Use NestedScrollview instead of scrollview and use android:nestedScrollingEnabled="false" for recycler_products recyclerview not in both
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_products"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
/>
</androidx.core.widget.NestedScrollView>
If you are not able to scroll it is because of this line
android:nestedScrollingEnabled="false".
Change it to True, so you can enable the scrolling:
android:nestedScrollingEnabled="true".
In you code you have recylerview inside scroll view, this is wrong.
Option 1: If scroll view has not any other child view than remove it and just use recyclerview and also remove android:nestedScrollingEnabled="false
And your Main View code looks like this;
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_products"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Option 2: If scroll view has also other child including recyclerview than you should use nestedscrollview like below code:
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:orientation="vertical">
<ImageView
android:id="#+id/sellerProduct"
android:layout_width="match_parent"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:src="#drawable/iphone"
android:scaleType="fitXY"
android:contentDescription="#string/app_name" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:nestedScrollingEnabled="false
android:id="#+id/productList"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
Related
I try to create a RecyclerView which has to be in the bottom of my View and I want to be able to scroll the RecyclerView so I added a NestedScrollView.
But the problem is : when the RecyclerView has too many items, I'm not able to scroll and even worse because the items go the top of the screen and overlap with the rest of my layout.
<ConstraintLayout
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="wrap_content">
<unrelated data>...</unrealated data>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
app:layout_constraintTop_toBottomOf="#+id/XXX"
tools:layout_editor_absoluteX="0dp">
<LinearLayout
android:id="#+id/linear_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</ConstraintLayout>
I am making an xml layout file consists of 3 parts:
Part 1: some information and image about a place.
Part 2: two horizontal Buttons.
Part 3: RecyclerView of some people reviews.
This is my layout file:
<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:name="com.example.abdo.foodproject.OneItemFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abdo.foodproject.OneItemFragment"
tools:layout="#layout/activity_item"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/oneitem_background_theme"
android:orientation="vertical"
android:weightSum="13"
>
<LinearLayout>
(...) <!--part 1-->
</LinearLayout>
<LinearLayout <!--part 2-->
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/review_list"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/reviews"
android:background="#color/colorPrimaryDark"
/>
<Button
android:id="#+id/bestMeal_list"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryLight"
android:text="#string/best_meal"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView <!--part 3-->
android:id="#+id/recyclerViewItem"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
I want to have the buttons fixed at my screen, at the top of the window, without losing their view from the scrolling.
Does anyone have any idea how to do so?
You can use CoordinatorLayout to achive such an affect.
CoordinatorLayout has 2 childs which one has also 2 childs as you wish
AppBarLayout: is a linear layout with vertical orientation. It's contents can disappear or pin according to "app:layout_collapseMode" attribute when scroll completed.
1.1. CollapsingToolbarLayout: you can put your "part 1" view in this place with app:layout_collapseMode="parallax" to parallax when disappearing.
1.2. Fixed view. LinearLayout: feel free to replace with your "part 2" view. This part will be pinned when scroll completed.
Scrollable view: it is RecyclerView in your example.
Final code should be similar like:
<?xml version="1.0" encoding="utf-8"?>
<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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
app:elevation="0dp">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#android:color/transparent"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed">
<!--part 1-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_collapseMode="parallax">
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
<!--part 2-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weight_sum="2">
<Button
android:id="#+id/review_list"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/reviews"
android:background="#color/colorPrimaryDark"
/>
<Button
android:id="#+id/bestMeal_list"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryLight"
android:text="#string/best_meal"
/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<!--part 3-->
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.design.widget.CoordinatorLayout>
I am currently trying to add a double ScrollView inside my XML.
I am using two different views in my layout - a list and categories.
When a user clicks on a category - the list of videos will become visible and the categories will become invisible.
Currently, the category buttons are scroll-able and when I click a category button, the list of videos does become visible. However, the issue is that the list of videos do not scroll.
Here is my layout code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/tools"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--this is parent layout where I call ListView-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parent01"
android:descendantFocusability="beforeDescendants"
android:fitsSystemWindows="true"
android:focusableInTouchMode="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:divider="#android:color/transparent"
android:dividerHeight="1dp"></ListView>
<ProgressBar
android:id="#+id/nextProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:visibility="gone"
style="#android:style/Widget.DeviceDefault.Light.ProgressBar.Small"/>
</RelativeLayout>
</LinearLayout>
<!--This is the categories layout-->
<ScrollView
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/ll_buttons"
android:layout_marginTop="60dp"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="200dp">
<Button
android:layout_weight="0.5"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rec_img"/>
<Button
android:layout_weight="0.5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rec_img"
android:id="#+id/button2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="200dp">
<Button
android:layout_weight="0.5"
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rec_img"/>
<Button
android:layout_weight="0.5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rec_img"
android:id="#+id/button4"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
for nested scrolling better to use NestedScrollView
NestedScrollView as the name suggests is used when there is a need for a scrolling view inside another scrolling view.
ScrollView vs NestedScrollView
another thing use RecyclerView insted of Listview
RecyclerView vs. ListView
sample layout
<?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">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
// add here all your controlls
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
and dont forgot to set setNestedScrollingEnabled() property in RecyclerView like below code
If this property is set to true the view will be permitted to initiate nested scrolling operations with a compatible parent view in the current hierarchy. If this view does not implement nested scrolling this will have no effect. Disabling nested scrolling while a nested scroll is in progress has the effect of stopping the nested scroll.
mRecyclerView.setNestedScrollingEnabled(false);
use nested scroll views
<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.support.v4.widget.NestedScrollView>
how to scroll all above RecyclerView in scrollview
I have to implement RecyclerView in scrollview show as below code, but not scroll RecyclerView.
please give answer
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/horizontalScrollView"
android:layout_marginTop="10dp">
<RelativeLayout...
<android.support.v7.widget.RecyclerView
android:id="#+id/rvpouch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:layout_below="#+id/textView3">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</ScrollView>
Don't use RecyclerView inside ScrollView. Use NestedScrollView instead of ScrollView.
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.
For Example:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Use attribute android:nestedScrollingEnabled="false" for smooth scrolling.
Use NestedScrollView instead of scroll view and set
recyclerView.setNestedScrollingEnabled(false);
Following code snippet will help you to implement scrolling using ScrollView of RecyclerView
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarSize="3dp"
android:scrollbarThumbVertical="#drawable/scrollbar_black">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/rlFiltersSearchEvent"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#drawable/action_bar_gradient">
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvListOfEventsMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/fab_margin"
android:layout_below="#+id/rlFiltersSearchEvent"
android:nestedScrollingEnabled="false"
android:scrollbars="none" />
</RelativeLayout>
</ScrollView>
Hope it helps
We can see here last item is partially visible. How can i fix this?
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinator_layout"
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="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<include
layout="#layout/header"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/grey_background">
<ImageView
android:id="#+id/image"
android:layout_width="#dimen/thumbnail_width"
android:layout_height="#dimen/thumbnail_height"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/image"
android:orientation="vertical"
android:padding="#dimen/participant_left_padding">
<TextView
android:id="#+id/participants_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textColor="#android:color/white"/>
<TextView
android:id="#+id/total_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view"
android:textColor="#android:color/white"/>
<TextView
android:id="#+id/ranking"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ranking"
android:textColor="#android:color/white"/>
</LinearLayout>
<ImageView
android:id="#+id/overflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/ic_action_overflow"/>
</RelativeLayout>
I'm also having the same problem. In my opinion this happened because you set the AppBarLayout XML attribute android:fitsSystemWindows="true". To solve this i give the RecyclerView margin bottom equal to action bar size
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/recyclerView"
android:layout_marginBottom="?attr/actionBarSize">
#Lester was right problem was RecyclerView's wrap_content height. But changing match_parent was not working because. This layout was added to a fragment and that fragment was declared wrap_content. So I have changed fragment's height and recyclerview's height to match_parent and now problem solved.
<fragment
android:id="#+id/fragment"
android:name="com.example.fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
I had this problem with a RecyclerView inside a ConstraintLayout, and i changed my recyclerview constriants to "0dp" (match_constraint) and had no further trouble.
have a look
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- Title -->
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="#dimen/view_with_press_height"
android:text="#string/taxes_fees_charges"
android:gravity="center_vertical"
android:layout_marginStart="#dimen/general_side_margin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<!-- Details Recyclerview-->
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="#dimen/general_bottom_margin"
app:layout_constraintVertical_bias="0.0"
tools:itemCount="28"
tools:listitem="#layout/tax_details_row" />
</android.support.constraint.ConstraintLayout>
if you want to use the tools:itemCount="28" you will have to import xmlns:tools="http://schemas.android.com/tools" in your XMLfile.
I tried all the available option from most of possible site but I didn't get the solution.
Then, I think can I use bottom padding? And Yes, It's work for me.
I am sharing the code to you.
Nothing more attribute required other than height, width & padding.
android:paddingBottom="?attr/actionBarSize"
<android.support.v7.widget.RecyclerView
android:id="#+id/your id name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="?attr/actionBarSize"
app:layout_constraintTop_toBottomOf="#+id/your field" />
Use RelativeLayout instead of ConstraintLayout.It's working for me.
I'm showing some solution but didn't work
Now I find one easy solution for this cutting off the last item in recycler view
Add your recycler view into LinearLaout
After adding recycler view into Linearlayout add these two Attributes into recycler view.
android:paddingBottom="?attr/actionBarSize"
android:clipToPadding="false"
Now, Your Recyclerview looks like this,
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvQuotes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginEnd="#dimen/_10sdp"
android:background="#color/white"
android:paddingBottom="?attr/actionBarSize"
android:paddingTop="#dimen/_10sdp"
android:clipChildren="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listItem="#layout/item_dessert"
android:clipToPadding="false"/>
Easiest but not the best solution. Still works;
Return +1 in the getItemCount() method of your RecyclerView.Adapter implementation and wrap your code in onBindViewHolder method override with a try-catch block.
For others.Disabling nested scrolling in recyclerview also causes this problem in CoordinatorLayout with scrollable toolbar or tablayout. Because when you scroll recyclerview, toolbar or tablayout doesn't scroll.
don't align recyclerview with respect to any view.. simply make it match parent and provide margin from top....this worked for me
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewAddresses"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_marginTop="#dimen/dimen_120dp"
/>