RecyclerView can't scroll up when embed under RelativeView and SwipeToRefresh - android

I have the below layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
It's basically having a recyclerView embedded in a SwipeToRefresh. This works well where I could scroll my recyclerView up and down. When reaching the top, and I pull down, the Refresh Happens.
Given I need to add an empty View dynamically, I have wrap the Empty View and RecyclerView under the RelativeLayout. This is as below.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:id="#+id/my_empty_view"
layout="#layout/fragment_empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
By adding this RelativeLayout between the RecyclerView and SwipeToRefreshLayout, the RecyclerView can only scroll down. It can not scroll up anymore, as whenever I try to scroll up, the SwipeToRefresh take place instead.
Why is this a problem? Is there any way to solve the problem while retaining my layout?
Thanks!

A scrollable view must be embedded in a SwipeRefreshLayout immediately.So you can let the RelativeLayout be embedded in a ScrollView and the ScrollView is embedded in the SwipeRefreshLayout.
Like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:id="#+id/my_empty_view"
layout="#layout/fragment_empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

Related

How to fix swiperefresh when one view is visible/invisible

I have recyclerview list.
If list is empty recyclerview goes to invisible or gone and another view appears to visible
If list is not empty recyclerview goes to visible.
It works fine, but when we have empty list another view appears I can't swipe to refresh in the right place (look at the screen).
I tried https://github.com/airbnb/epoxy/issues/74 but it hadn't help me.
my xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.deepdiveandroid.base.ui.view.FullScreenErrorStateView
android:id="#+id/vErrorStateNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:id="#+id/vSwipeToRefreshNotification"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvNotificationItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="#dimen/spacing_16"
android:paddingBottom="#dimen/spacing_72"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/layout_list_item_notification" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
</FrameLayout>
Try to change your xml layout to this
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.deepdiveandroid.base.ui.view.FullScreenErrorStateView
android:id="#+id/vErrorStateNotification"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvNotificationItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="#dimen/spacing_16"
android:paddingBottom="#dimen/spacing_72"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/layout_list_item_notification" />
</FrameLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Recyclerview inside coordinator layout not scrolls

I have following activity
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/coordinator">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite"
android:id="#+id/catalog_view">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="79dp"
android:background="#color/gray238"
android:id="#+id/top_separator"/>
..
</android.support.constraint.ConstraintLayout>
<include layout="#layout/check_view_sheet"/>
</android.support.design.widget.CoordinatorLayout>
check_view_sheet
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/check_bottom_sheet"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#color/colorWhite"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="80dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<FrameLayout
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:layout_width="400dp"
android:layout_height="235dp"
android:id="#+id/catalog_view_check_fragment"
/>
<TotalView.TotalView
android:layout_width="match_parent"
android:layout_height="#dimen/totalViewHeight"
android:id="#+id/catalog_total_view" />
</LinearLayout>
View inside framelayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/check_recycler_view"
android:paddingBottom="#dimen/checkBottomPadding"
android:clipToPadding="false"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
The problem is that recycler view is not scrolling.
I tried wrapping framelayout in nestedScrollView, but performance of scroll is horrible - its very laggy and it hides sheet when I try to scroll recycler view down.
How canto fix this ?
Because you are trying do nested scrolling, bottom sheet has scrollable behavior.
try this in RecyclerView
android:nestedScrollingEnabled="true"
if scrolling it in bottom sheet is really mandatory otherwise try by setting it to "false".

NestedScrollView smooth scroll to the top and to a view inside

First I need to scroll my NestedScrollView to the top, but smoothScrollTo(0, 0) doesn't work for me (page just jumping a bit). Second I wonder how can I scroll to a certain view inside my NestedScrollView. API 27, Support 27.0.2.
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/PageBackground"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
android:paddingBottom="#dimen/indent_page_bottom">
...
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</layout>
Try using fling to achieve smooth scrolling :
nestedScrollView.fling(0);
nestedScrollView.smoothScrollTo(0, 0);
I had a nestedscrollviewand inside it there is recyclerview. I tried smoothScrollTo(0, nsv_main.top). Didn't work. I gave an id to nestedscrollview like below
<android.support.v4.widget.NestedScrollView
android:id="#+id/nsv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/PageBackground"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
android:paddingBottom="#dimen/indent_page_bottom">
...
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
and gave nsv_main.scrollY = 0 in my click event. It worked, even though its not a smooth scroll.

Add View at top of recycler view

In my android application, there is a slideshow at top of page and below it, there is a recycler view, my problem is how to add the slideshow above recycler view in a way that slideshow can be scrolled with recycler view. I tried adding a scroll view as a parent but it seems that recycler lost the recycling power.
<RelativeLayout
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:background="#ececec">
<android.support.v4.view.ViewPager
android:id="#+id/ChefFragmentViewPager"
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_alignParentTop="true" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/ChefFragmentViewPager"
android:background="#ececec"
android:scrollbars="none" />
</RelativeLayout>
Try using NestedScrollView instead of ScrollView and also set recyclerView.setNestedScrollingEnabled(false); programmatically.
Try this code and let me know if it helped you or not:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
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:background="#ececec">
<android.support.v4.view.ViewPager
android:id="#+id/ChefFragmentViewPager"
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_alignParentTop="true" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/ChefFragmentViewPager"
android:background="#ececec"
android:scrollbars="none" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>

Bottom navigation overlapping recycler view contents in android

I am using bottom navigation view and displaying three tabs,In one of the tab i have added recycler view, but the contents are overlapped by bottom navigation.Not able to scroll recycler view.Is there any way that i can hide the bottom navigation when i scroll item on recycler view.
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:sv="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".activity.patient.PatientsActivity">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="#+id/asd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:orientation="vertical">
<co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView
android:id="#+id/appointmentsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:rrvEmptyLayoutId="#layout/empty_view"
app:rrvLayoutType="LinearLayout"
app:rrvSwipeToDelete="true" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/white"
app:layout_behavior=".BottomNavigationBehavior"
app:menu="#menu/bottom_navigation_main" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout> </android.support.v4.widget.DrawerLayout>
image
First i think you could add an:
android:layout_above="#id/bottom_navigation"
To the Recyclerview
For the Scrolling part i would suggest looking for a ScrollingBehaviour
like in this question:
How to scroll up/down of bottom bar on scrolling of RecyclerView
add NestedScrollView just before RecyclerView this may work like above
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView
android:id="#+id/appointmentsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:rrvEmptyLayoutId="#layout/empty_view"
app:rrvLayoutType="LinearLayout"
app:rrvSwipeToDelete="true" />
</android.support.v4.widget.NestedScrollView>

Categories

Resources