Nested recyclerviews inside nestedscrollview - android

I'm trying to implement nested RecyclerView. I did that but parent RecyclerView scroll was not smooth on scroll. I did alot of optimization, but still didn't manage to be successful with scroll till I though of putting parent recyclerview inside NestedScrollView. Scroll is flawless now, but I've a problem.
If I scroll (even tiny bit) my inner RecyclerView (horizontal), I immediately get back to the start [of vertical recyclerview - parent].
This happens one time. Why is that and is it possible to fix it?
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.v7.widget.RecyclerView
android:id="#+id/parent_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>

As far as I have understood your problem, you parent RecyclerView gets updated and scrolls to position 0 when the inner horizontal RecyclerView items get updated. You have also stated that the horizontal RecyclerView stays in the same place update the items are updated. In this case, you need to save the state of the parent RecyclerView and then put it back where it was after the items in your horizontal RecyclerView get updated.
// Save state
private Parcelable recyclerViewState;
recyclerViewState = parentRecyclerView.getLayoutManager().onSaveInstanceState();
// Restore state
parentRecyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
Save the state before you call the API for updating the data in your horizontal RecyclerView and then restore the position of the parent RecyclerView when the update finishes.

I got a solution.
Basically, what I did was wrap recycler view inside of relative layout and set android:descendantFocusability="blocksDescendants" and now it works good.
<android.support.v4.widget.NestedScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="#+id/parent_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>

Related

Collapse recyclerview and show only last element

I have a recyclerview with 3 rows maximum. This RV located on the top of my view. Below located container for my fragment. And I want to make only last element of my rv visible, when I will scroll content inside my fragment and show all elemenets of rv when I will on the top of my fragment content. How to do this?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dp"/>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
First, you have to use nestedscrollview in your layout for proper scrolling of recyclerView and other views in your parent layout.
The second thing you can do is send only the last element of your list to the adapter.
like ,
ArrayList<Test> tempList=new ArrayList();
tempList.add(customList.get(customList.size()-1);
where customList is the Type of class data which are you sending as an argument to your adapter.
this way you can do your work.

How to use two recyclerView in single activity?

I want to implement two RecyclerView with different layout in single activity. The above RecyclerView should scroll vertical and the one below should scroll horizontal. But when I run the app, only either one RecyclerView is displayed. If first view is displayed then it works properly and scrolls vertical, while second RecyclerView is missing. And if second one is displayed then it scrolls vertical when it should do horizontal scroll and the first RecyclerView is missing.
Here is what i want. Source: Github,CardView-Recyclerview-Picasso
Here is my layout
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackgroundLight"
android:smoothScrollbar="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/CategoriesRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/videoRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="130dp"
android:layout_below="#+id/CategoriesRecyclerView"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
You may directly use the 2 Recycler Views without NestedScrollView.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/CategoriesRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/videoRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="130dp"
android:layout_below="#+id/CategoriesRecyclerView"/>
</RelativeLayout>
And in your CategoriesRecyclerView whose height is wrap_content, use setAutoMeasureEnabled(true) on the Layout manager used for the recyler view.
If you want to scroll the horizontal scrollview full upside on page scroll then use scrollview otherwise you can do without scrollview. Also to achieve your layout just give the horizontal recyclerview fixed height and then you can see both recyclerview .

How to add extra space to RecyclerView with clipToPadding=false to prevent premature recycling

I have the following layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentBottom="true"
android:layout_below="#+id/searchView"
android:clipChildren="false"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
<SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
which is a simple RecyclerView below a SearchView. I want the RecyclerView's views to scroll behind the SearchView, so clipChildren and clipToPadding are set to false.
My problem is that RecyclerView is (rightly) recycling its adapter's views when they are still visible, just because they are out of the RecyclerView's area. Is there a way to add an extra space to the RecyclerView within which views are not recycled?
EDIT, a gif showing the problem:
This method should help you setItemViewCacheSize(int size). It sets the number of offscreen views to retain before adding them to the potentially shared recycled view pool. More about it you can find here.

Recycle can not scroll inner ScrollView

I hava a layout below:
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</android.support.v7.widget.RecyclerView>
the recyclerView using LinearLayoutManager, a item of recyclerView is another recyclerView which using GridLayoutManager, set the gridLayoutRecyclerView's height, but the gridLayoutRecyclerView can not scroll inner
try this code
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
and also refer about this http://developer.android.com/training/material/lists-cards.html
After checking implementation, the reason appears to be the following. If RecyclerView gets put into a ScrollView, then during measure step its height is unspecified (because ScrollView allows any height) and gets equal to minimum height (as per implementation) which is apparently zero.
You have couple of options for fixing this:
Set a certain height to RecyclerView
Set ScrollView.fillViewport to true
Or keep RecyclerView outside of ScrollView.
If RecyclerView height is not limited - which is the case when it's put into ScrollView - then all Adapter's views have enough place vertically and get created all at once.
This is my out recyclerView's layout
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</android.support.v7.widget.RecyclerView>
then, a item of out recyclerView have another RecyclerView's which using GridLayoutManager, this is the code of the item below:
<android.support.v7.widget.RecyclerView
android:id="#+id/seatRecyclerView"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
the seatRecyclerView can't not to scroll

RecyclerView in a scrollView

When scrolling up to the first position item in RecyclerView, can ScrollView continue to scroll up? Here is the code:
<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.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="#dimen/margin_10dp_in_w1024dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
Having two scrollviews that scroll in the same direction is not possible , try to rethink your UI .
You can :
Use recyclerview to handle diffrent types of views , so that you no more need another scrollview .
Use parallax views in coordinatorLayout to show information every time you reach first position .

Categories

Resources