Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add listener callback to allow disabling SwipeRefreshLayout when scrolling ViewPager horizontally #421

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>