Recyclerview loses edge effect when placed inside NestedScrollview - android

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?

Related

Bottomsheet with Viewpager2 on Android

I am developing an app that uses a vertical bottom sheet. To be more specific, it is a LinearLayout with bottomSheetBehavior. This bottom sheet comprises two elements: a view pager that displays images and can be scrolled horizontally, and, below the view pager, a Recycler View whose elements can be scrolled vertically.
The good layout file looks like this:
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?android:attr/colorBackground"
android:orientation="vertical">
<!-- works with and without frame layout-->
<FrameLayout
android:id="#+id/play_queue_square_frame"
android:layout_width="match_parent"
android:layout_height="350dp">
<androidx.viewpager.widget.ViewPager
android:id="#+id/play_queue_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/play_queue_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layoutManager="LinearLayoutManager"
android:background="?android:attr/colorBackground"
tools:listitem="#layout/item_view_song_debug" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
With the above layout everything is working as expected. In particular, the view pager scrolls in horizontal direction, the recycler view scrolls in vertical direction, and dragging either the view pager or the recycler view in horizontal direction closes the bottom sheet.
However, when replacing the view pager by androidx.viewpager2.widget.ViewPager2 yields odd behaviour. Scrolling the view pager in horizontal direction is still possible, but vertical scrolling does no longer work. Any swipes downward simply close the bottom sheet.
Using a NestedScrollView yields better results but still has erroneous behaviour.
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?android:attr/colorBackground"
android:orientation="vertical">
<FrameLayout
android:id="#+id/play_queue_square_frame"
android:layout_width="match_parent"
android:layout_height="300dp">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/play_queue_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/play_queue_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layoutManager="LinearLayoutManager"
android:background="?android:attr/colorBackground"
tools:listitem="#layout/item_view_song_debug" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
Scrolling in vertical direction is still possible, and also the horizontal scrolling for the view pager. However, dragging the view pager down does no longer close the bottom sheet, whereas a dragging the recycler view down does.
If I add the following code
viewPager.setUserInputEnabled(false)
the view pager does no longer allow horizontal scrolling (as expected). So my question is simply what I have to do in order to allow horizontal scrolling and make sure that a vertical swipe downwards (on the view pager) closes the bottom sheet.
I know that a somewhat similar question has already been asked (in this post)
but as there is no answer so far I take the liberty to ask this question. What confuses me most is that it is working with the old viewpager but not with viewpager2.
Any help is greatly appreciated!

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.

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 .

Horizontal RecyclerView with nested HorizontalScrollView

Im trying to rig a horizontally oriented RecyclerView with child items that contain a HorizontalScrollView. What I have configured right now is not allowing the nested HorizontalScrollView to scroll its content.
Heres an abbreviated version of my activity layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RecyclerView
android:id="#+id/home_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
And here's an abbreviated version of the item layout:
<android.support.v7.widget.CardView
android:id="#+id/main_card"
android:layout_width="335dp"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</HorizontalScrollView>
</android.support.v7.widget.CardView
So Im trying to get the Text content inside the Horizontal ScrollView to be scrollable.
EDIT :
Below is a screenshot of the textview that will not scroll. As you can see, the text is cut off at the end of the view, and this is why Im looking to implement horizontal scrolling.
NOTE: I tried changing the LayoutManager associated with my RecyclerView to have vertical orientation, and horizontal scrolling works as expected. Knowing this, it seems that the RecyclerView is absorbing all scroll events and not allowing the nested HorizontalScrollView to do its thing

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