Skip to content

Commit

Permalink
Allow disabling SwipeRefreshLayout when scrolling ViewPager horizonta…
Browse files Browse the repository at this point in the history
…lly. Fixes roomorama#420
  • Loading branch information
grennis committed Aug 1, 2016
1 parent b50db7b commit 7a6c9ce
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -132,6 +137,10 @@ public void onCaldroidViewCreated() {
}
}

@Override
public void pagerScrolling(boolean scrolling) {
refresh.setEnabled(!scrolling);
}
};

// Setup Caldroid
Expand Down Expand Up @@ -277,4 +286,8 @@ protected void onSaveInstanceState(Bundle outState) {
}
}

@Override
public void onRefresh() {
refresh.setRefreshing(false);
}
}
9 changes: 8 additions & 1 deletion caldroidSampleActivity/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

Expand Down Expand Up @@ -44,3 +49,5 @@

</LinearLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

0 comments on commit 7a6c9ce

Please sign in to comment.