RecyclerView in a scrollView - android

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 .

Related

CoordintatorLayout - scrollable content in both, AppBar and Layout

I am creating a view based on CoordinatorLayout. The view contains two elements: View in CollapsingToolbarLayout and RecyclerView as a content. The whole content is also wrapped in SwipleRefreshLayout - there was a problem with scrolling up, but easily fixed (via OnOffsetChangedListener in AppBar)
My problem is that a view in CollapsingToolbarLayout can be higher than a screen (very rare, but possible), so it has to be scrollable.
See this example:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
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"
tools:context=".MainActivity">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<View android:id="#+id/very_big_height"
android:layout_width="match_parent"
android:background="#color/design_default_color_primary"
android:layout_height="2000dp"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dummy_recycler_view"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.SwipeRefreshLayout>
When I scroll via an AppBar I can set my layout in such a state:
The blue area has id very_big_height and that white area is actually an empty space on a screen, as my RecyclerView has a wrap_content height and no items inside.
I still can scroll the AppBar via the blue area, and even over scroll it completely outside the screen. And if I over scroll it, there is no way to see it again.
My expected behaviour is:
AppBar should scroll only to the bottom of the screen. It should not hide if there is no content below.
Also, if content fits screen AppBar should not be scrollable (I've founded solution for disabling scroll when no content, but doesn't work when AppBar doesn't fit the screen.
Thanks for your help!
the scroll issue is caused by the parent view which is SwipetoRefreshLayout , the root view of your xml should be CoordinatorLayout .
i don't know what is the purpose of using SwipetoRefreshLayout , but having to much scrolling element may cause some bad scroll experience
First one the CoordinatorLayout can not send scroll event to SwipeRefreshLayout. You can try the code in ChildCoordinatorLayout
Second is that you can set RecyclerView match parent.

Recyclerview loses edge effect when placed inside NestedScrollview

I am trying to place a recyclerview inside a nested scroll view. Following is my layout:
<FrameLayout 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"
tools:context=".MainActivity">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/nestedscrollview">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.nestedscrollviewexample.ItemFragment"
android:layout_marginTop="84dp"
android:id="#+id/itemfragment"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
The fragment contains a simple Recylerview.
What i notice is that if the recyclerview is outside the NestedScrollView then it retains it edge effects. However when i place the recycler view inside the nestedscrollview the edge effects are gone.
Instead, when i add the following line-
ViewCompat.setNestedScrollingEnabled(recyclerView, false);
I notice that edge effects are visible but only for the edge of nested scrollview. How can i retain the edge effect for the recycler view as well?
For better clarity i am sharing 2 images -
First is Recyclerview edge effect before adding it to nested scroll view.
[]
The one below is when we add recycler view to a nested scroll view-
I would like to have the recyclerview inside the nested scrollview and also keep its edge effects. Is it possible?

How to draw a background for an endless scrolling RecyclerView

I'm developing a endless scrollable list for an android app, using the RecyclerView viewgroup and an OnScrollListener where i add the items after the user scrolled to the last item of the RecyclerView.
Now a want to add a black line in the middle behind the items as kind of a recycler view.
As you see, the items can have different heights.
Whats the best (and most performant) way to archieve this?
You might consider using a layout like the following.
<?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"
android:background="#drawable/your_background">
<android.support.v7.widget.RecyclerView
android:id="#+id/your_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" />
</RelativeLayout>

Nested recyclerviews inside nestedscrollview

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>

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 .

Categories

Resources