RecyclerView inside NestedScrollView does not retain recycler scroll position if rc view height is not given in dp - android-recyclerview

<RelativeLayout 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="#color/black"
android:fillViewport="true"
tools:context=".Details_Act.About_Act">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:id="#+id/master_nest"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Some More Layouts>...</Some More Layouts>
<androidx.recyclerview.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:id="#+id/eps_rcview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relative"
android:layout_marginTop="20dp" />
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
In above case when i backpress from another activity recycler position is reset
But if i give recyclerview a height of any 400dp,500dp,etc... instead of fill parent or match parent it work it retain recycler position
Is there anyone who can help me😩
I know nestedscrollview take only one child , i am not going to give full code cuz it has too many lines

Related

CollapsingToolbarLayout not Collapsing with RecyclerView Scroll Behavior

I have a CollapsingToolbarLayout that shows an image (placed inside the ConstraintLayout to keep the ratio 1:1) and a RecyclerView that has the com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior attached to it.
Image of Layout
The image is in green and the RecyclerView is in blue.
The problem is when I scroll up on the RecyclerView, the CollapsingToolbarLayout doesn't collapse. Instead, the RecyclerView just scrolls underneath the entire layout. I have to manually scroll inside the CollapsingToolbarLayout for it to collapse.
Here is the code for the layout:
<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"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="#+id/toolbar">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:src="#drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
android:nestedScrollingEnabled="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end"
app:srcCompat="#android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I originally had the RecyclerView inside a NestedScrollView with the scrolling behavior attached to the NestedScrollView, but this was causing issues with the Adapter creating a ViewHolder object for EVERY single item in the RecyclerView and that was causing a lot of performance and lagging/freezing issues.
From my understanding, a RecyclerView should not be placed inside a NestedScrollView for this reason and that is why I took the NestedScrollView out.
Any help would be appreciated!
Simply you should disable nested scrolling in recycler view
android:nestedScrollingEnabled="false"
Also, if you have nested recycler views you should disable nested scrolling in child recycler views.
If you want the image to collapse when recyclerview scrolls, use this scroll flags
app:layout_scrollFlags="scroll|snap|enterAlwaysCollapsed"
and use
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
in your recyclerview , and android:nestedScrollingEnabled attribute is not required for what you want to achieve.
have you looked into https://www.journaldev.com/13927/android-collapsingtoolbarlayout-example
Also see some of the things you are missing

Androidx scrolling view behavior not working

After switching to androidx the following code stopped working. The TextView below the Toolbar used to scroll with the content, but now it doesn't. I managed to make it work only when I set the same scrollFlags to Toolbar, but I would like to keep that in place. Is there a solution to this besides moving the Toolbar out of AppBarLayout and CoordinatorLayout?
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
app:title="Non-scrollable title"
android:layout_width="wrap_content"
android:layout_height="?android:attr/actionBarSize"/>
<TextView
app:layout_scrollFlags="scroll"
android:layout_margin="16dp"
android:textSize="20sp"
android:text="this should scroll with the content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- some scrollable content here -->
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
replace attribute
old:
app:layout_behavior="#string/appbar_scrolling_view_behavior"
new:
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
and add
implementation 'com.google.android.material:material:1.0.0'
In my case, the problem was in the margins of NestedScrollView. I removed margins from NestedScrollView and everything started work.

Recyclerview multiple viewtype causes first row item expanded unexpectedly

I want to make something like this
so I am using a recyclerview which has two viewtype ( for the first item I am using one kind of viewholder and rest of item I am using another viewholder).
But on my device, it is showing like this
For the first viewholder I am using this layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/attach_circle_imageview"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/attach_photo"
app:civ_border_color="#607D8A"
app:civ_border_width="1dp"
android:layout_marginLeft="14dp"
android:layout_marginBottom="14dp"/>
and for others, I am using this layout file
<android.support.v7.widget.CardView
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginLeft="14dp"
android:layout_marginBottom="14dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="10dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/gallary_iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/white_bg_iv"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/pic_photo_number_bg"
app:layout_constraintBottom_toBottomOf="#+id/gallary_iv"
app:layout_constraintEnd_toEndOf="#+id/gallary_iv"
app:layout_constraintStart_toStartOf="#+id/gallary_iv"
app:layout_constraintTop_toTopOf="#+id/gallary_iv" />
<TextView
android:id="#+id/pic_count_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="16sp"
android:textColor="#506876"
app:layout_constraintTop_toTopOf="#+id/white_bg_iv"
app:layout_constraintBottom_toBottomOf="#+id/white_bg_iv"
app:layout_constraintStart_toStartOf="#+id/white_bg_iv"
app:layout_constraintEnd_toEndOf="#id/white_bg_iv"/>
</android.support.constraint.ConstraintLayout>
In the first-row pictures height is expanded unexpectedly, how can I solve this problem?
set the first viewholder layout
android:layout_width="match_parent"
android:layout_height="match_parent"
change to
android:layout_width="90dp"
android:layout_height="90dp"

how to define two different layout managers in a single recylerview? i have used two recyclerviews for layout managers

how to define two different layout managers in a single recyclerview? actually now i defined the layouts using two recylerviews.Even i want to scroll both the recylerview at the time.
this is my code.
<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"
tools:context=".LinearLayout" >
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_recycler"
android:layout_width="match_parent"
android:orientation="horizontal"
android:overScrollMode="never"
android:layout_height="250dp"/>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear_recycler"
android:layout_width="match_parent"
android:orientation="vertical"
android:overScrollMode="never"
android:scrollbars="none"
android:layout_height="250dp"/>
</LinearLayout>

android, how to place a scroll view in the middle of the screen:

I'm trying to create a a screen layout of the following though:
A line with some TextView in it
A scroll view where the user actions will add or remove View from
A buttons line.
I have no problem with 1. or 3. But 2. gives me troubles. My scheme is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainX"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/labels"
android:layout_weight="1">
<!--Some text view of various sizes -->
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dataScroll">
<LinearLayout
android:id="#+id/dataShow"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttons"
android:layout_weight="1">
<!--some buttons-->
</LinearLayout>
</LinearLayout>
When I run the program, the buttons line is in the middle of the screen and When I add element to the screen (using the addView on the dataShow layout), the frist one is added without a problem but after that I can't tell what is happening.
Thanks
I'm not exactly sure what you are trying to do.
It seems like you want to dynamically add items to the dataShow layout. Instead of using a ScrollView containing a LinearLayout, you should be using a ListView (vertical scrolling) or Gallery (horizontal scrolling) for your dataShow layout. This will provide the scroll functionality and allow you to add items dynamically to the list/gallery.
Try using a RelativeLayout, then align the header at the top, and the footer at the bottom. Set the ScrollView paddingBottom.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="text"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/header"
android:layout_above="#+id/footer"
android:paddingBottom="100dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="center main content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
</ScrollView>
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/colorPrimary"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
</RelativeLayout>