From 7a6c9ced695687bfb2b3b6d21e8b9599bbc40c2d Mon Sep 17 00:00:00 2001 From: Greg Ennis Date: Mon, 1 Aug 2016 07:12:27 -0400 Subject: [PATCH] Allow disabling SwipeRefreshLayout when scrolling ViewPager horizontally. Fixes #420 --- .../roomorama/caldroid/CaldroidFragment.java | 19 +++++++++++++++++++ .../roomorama/caldroid/CaldroidListener.java | 9 +++++++++ .../CaldroidSampleActivity.java | 15 ++++++++++++++- .../src/main/res/layout/activity_main.xml | 9 ++++++++- 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/caldroid/src/main/java/com/roomorama/caldroid/CaldroidFragment.java b/caldroid/src/main/java/com/roomorama/caldroid/CaldroidFragment.java index 478d148..c61d58b 100644 --- a/caldroid/src/main/java/com/roomorama/caldroid/CaldroidFragment.java +++ b/caldroid/src/main/java/com/roomorama/caldroid/CaldroidFragment.java @@ -11,6 +11,7 @@ import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; +import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.text.format.DateUtils; import android.text.format.Time; @@ -1386,6 +1387,24 @@ private void setupDateGridPages(View view) { // height correctly dateViewPager.setDatesInMonth(dateInMonthsList); + // Set callback to allow app to disable swiperefresh or other uses + dateViewPager.addOnPageChangeListener(new OnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + } + + @Override + public void onPageSelected(int position) { + } + + @Override + public void onPageScrollStateChanged(int state) { + if (caldroidListener != null) { + caldroidListener.pagerScrolling(state != ViewPager.SCROLL_STATE_IDLE); + } + } + }); + // MonthPagerAdapter actually provides 4 real fragments. The // InfinitePagerAdapter only recycles fragment provided by this // MonthPagerAdapter diff --git a/caldroid/src/main/java/com/roomorama/caldroid/CaldroidListener.java b/caldroid/src/main/java/com/roomorama/caldroid/CaldroidListener.java index 4a5bf46..b3d4420 100644 --- a/caldroid/src/main/java/com/roomorama/caldroid/CaldroidListener.java +++ b/caldroid/src/main/java/com/roomorama/caldroid/CaldroidListener.java @@ -54,4 +54,13 @@ public void onChangeMonth(int month, int year) { public void onCaldroidViewCreated() { // Do nothing } + + /** + * Inform client that the viewpager is scrolling. Give the app the chance to + * disable a swipe refresh layout, or do other checking + * @param scrolling + */ + public void pagerScrolling(boolean scrolling) { + + } } diff --git a/caldroidSampleActivity/src/main/java/com/caldroidsample/CaldroidSampleActivity.java b/caldroidSampleActivity/src/main/java/com/caldroidsample/CaldroidSampleActivity.java index ee52cf6..3454f49 100644 --- a/caldroidSampleActivity/src/main/java/com/caldroidsample/CaldroidSampleActivity.java +++ b/caldroidSampleActivity/src/main/java/com/caldroidsample/CaldroidSampleActivity.java @@ -5,6 +5,7 @@ import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; +import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.View.OnClickListener; @@ -21,8 +22,9 @@ import java.util.Date; @SuppressLint("SimpleDateFormat") -public class CaldroidSampleActivity extends AppCompatActivity { +public class CaldroidSampleActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener { private boolean undo = false; + private SwipeRefreshLayout refresh; private CaldroidFragment caldroidFragment; private CaldroidFragment dialogCaldroidFragment; @@ -55,6 +57,9 @@ protected void onCreate(Bundle savedInstanceState) { final SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy"); + refresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh); + refresh.setOnRefreshListener(this); + // Setup caldroid fragment // **** If you want normal CaldroidFragment, use below line **** caldroidFragment = new CaldroidFragment(); @@ -132,6 +137,10 @@ public void onCaldroidViewCreated() { } } + @Override + public void pagerScrolling(boolean scrolling) { + refresh.setEnabled(!scrolling); + } }; // Setup Caldroid @@ -277,4 +286,8 @@ protected void onSaveInstanceState(Bundle outState) { } } + @Override + public void onRefresh() { + refresh.setRefreshing(false); + } } diff --git a/caldroidSampleActivity/src/main/res/layout/activity_main.xml b/caldroidSampleActivity/src/main/res/layout/activity_main.xml index 2fdbc27..c7f3080 100644 --- a/caldroidSampleActivity/src/main/res/layout/activity_main.xml +++ b/caldroidSampleActivity/src/main/res/layout/activity_main.xml @@ -1,6 +1,11 @@ - + + @@ -44,3 +49,5 @@ + +