Android SwipeToRefresh - Confilct with RecyclerView when swiping down - android

So I have a fragment with 2 RecyclerView lists. I want to swipe fragment to refresh those lists but when I scroll one of those lists to see hidden elements SwipeToRefresh refreshes those lists. How to prevent it from that?
Here is my XML layout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_fragment">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/main_fragment_swipe_to_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/main_fragment_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="18sp"
android:textColor="#color/primary_dark"
android:text="Najpopularniejsze przepisy" />
<android.support.v7.widget.RecyclerView
android:id="#+id/top_recipes_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="18sp"
android:textColor="#color/primary_dark"
android:text="Najnowsze przepisy" />
<android.support.v7.widget.RecyclerView
android:id="#+id/latest_recipes_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</LinearLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
Any ideas?

You can consume the swipe down event in RecycleView when it can scroll up.
Just custom the RecycleView and intercept the event when you need.

Related

Strange behaviour of recyclerview in nestedscrollview

I have a layout like below. Problem is that the recyclerview height only shows one item.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="#dimen/activity_horizontal_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Live Transactions"
android:textStyle="bold"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_refresh"
android:background="#android:color/transparent"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Strange thing is that when I change the attribute
app:srcCompat="#drawable/ic_refresh"
in the ImageButton to something like
app:srcCompat="#android:drawable/ic_menu_search"
the recyclerview height becomes normal most items show. The ImageButton is on a layout above RecyclerView. Why does this happen?
You have to make your inner linear layout as match parent
<android.support.v4.widget.NestedScrollView 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Live Transactions"
android:textStyle="bold"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:background="#android:color/transparent"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Solved it simply by adding
android:fillViewport="true"
to the NestedScrollView
Make the layout_height on the Recycler View to match_parent
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

scroll views and RecyclerView at same time

want to scroll RecyclerView and views in scroll view, in this code scroll only work on views but not scroll the RecyclerView or scroll hide the RecyclerView.I am using nested scrollview but same not working.
scroll views and RecyclerView like as one layout is scrolling.
<?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">
<ScrollView
android:id="#+id/scrol"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/topRl5"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/nested_scrol"
android:orientation="vertical"
android:visibility="visible">
<com.github.mikephil.charting.charts.BarChart
android:id="#+id/chart1"
android:layout_width="match_parent"
android:layout_height="#dimen/_300sdp"
android:layout_below="#+id/weekly_summary"
android:visibility="visible" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/grey_light"
android:gravity="center"
android:padding="#dimen/_5sdp"
android:text="تفصیلی رپورٹ"
android:textSize="#dimen/_15sdp" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/daily_earning_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/scrol"
android:visibility="visible">
<include
layout="#layout/weekly_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
Here is my RecyclerView i include it in main class
<RelativeLayout
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="wrap_content"
tools:showIn="#layout/activity_barchart"
tools:context=".guaranteeDetails.baarChart.BarChartActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/weekly_recycler_view"
android:layout_marginRight="#dimen/_4sdp"
android:layout_marginLeft="#dimen/_4sdp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Use should use NestedScrollView instead of ScrollView
And I would also recommend setting
Recyclerview.setNestedScrollingEnabled(false);

How to use swipe refresh with recyclerivew

I have use swipe resfresh with recyclerivew. but I see one problem. I can't scroll my recyclerivew up. I can scroll recyclerview down normally but every time I try to scroll up it will not scroll it up it is work the function swift refresh how ever I am not scrol up to the top yet.
Here is my xml
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginBottom="-5dp"
android:id="#+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/buslayout"
android:focusableInTouchMode="true"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:gravity="center"
android:text="Loading..."
android:id="#+id/info"
android:visibility="visible"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/business_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Try this:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginBottom="-5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:gravity="center"
android:text="Loading..."
android:id="#+id/info"
android:visibility="visible"/>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/swiperefresh"
android:focusableInTouchMode="true"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/business_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>

SwipeRefreshLayout hides RecyclerView's upper part?

I'm beginner in android, and I don't know why this happens.
Without SwipeRefreshLayout the RecyclerView is fully visible, but with SwipeRefreshLayout, the first item isn't shown.
Maybe it's under the toolbar? (The hidden item exists, I checked it in numerous ways.)
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/swipe_layout">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/MainRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
List:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="horizontal"
android:paddingTop="8dip"
android:paddingBottom="8dip"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:weightSum="10"
android:gravity="center">
<ImageView
android:id="#+id/CharacterItemRankImageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="0"
android:src="#drawable/challengemode_medal_bronze"/>
<TextView android:id="#+id/CharacterItemNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3.5"
tools:text="Name"/>
<TextView android:id="#+id/CharacterItemPointsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_weight="6"
tools:text="Points"/>
<ImageButton
android:id="#+id/CharacterItemRemoveButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="0.5"
android:src="#drawable/ic_delete_grey600_48dp"
android:scaleType="fitXY" style="#style/Widget.AppCompat.Button.Borderless" />
</LinearLayout>
I guess you are using a CoordinatorLayout as root of your view. In that case, what is happenning is that the toolbar is being hidden by the first item of your RecyclerView.
To fix this issue, you just need to add the layout behavior to the SwipeRefreshLayout and remove from your RecyclerView.
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="#+id/MainRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>

Fragment onswipe up hide a view

I have a fragment,in which I have a calendar, text view and a listview. All these views are inside the scroll view. I need two things in that layout.
I want to disable the scrolling of the list view.
How scroll to invoke scroll view when I swipe up, I want to hide calendar and text view. And on swipe down. It shows the whole layout.
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:animateLayoutChanges="true"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_below="#+id/rel"
android:id="#+id/scroll"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/compactcalendar_view_container"
android:layout_below="#+id/rel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="#e1dcdc"
android:background="#011627"
android:id="#+id/date_picker_text_view"
android:textAlignment="center" />
<com.github.sundeepk.compactcalendarview.CompactCalendarView
android:id="#+id/cvCalendar"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="250dp"
app:compactCalendarTargetHeight="250dp"
android:layout_below="#+id/textView14"
app:compactCalendarBackgroundColor="#011627"
app:compactCalendarCurrentDayBackgroundColor="#FFC107"
app:compactCalendarCurrentSelectedDayBackgroundColor="#939393"
app:compactCalendarTextColor="#e1dcdc"
app:compactCalendarTextSize="12sp" />
</LinearLayout>
<ListView
android:id="#+id/lvList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#e9e9e9"
android:dividerHeight="10dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:scrollbars="none"
android:background="#eee"
android:layout_below="#+id/scroll"
android:layout_centerHorizontal="true" />
</LinearLayout>
</ScrollView>
</RelativeLayout>

Categories

Resources