Last active
July 29, 2019 10:09
-
-
Save VladSumtsov/ad4e13511a9b73ff3b13 to your computer and use it in GitHub Desktop.
RecycleView PullToRefresh SwipeRefreshLayout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:id="@+id/recycler_view" /> | |
</android.support.v4.widget.SwipeRefreshLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.support.v4.widget.SwipeRefreshLayout; | |
import android.support.v7.widget.DefaultItemAnimator; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import com.togethernetworks.basesdk.BaseActivity; | |
import com.togethernetworks.gallery.samples.mortar.UiModule; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class MyActivity extends BaseActivity { | |
private RecyclerView recyclerView; | |
private SwipeRefreshLayout refreshLayout; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_my); | |
refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout); | |
refreshLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW); | |
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { | |
@Override | |
public void onRefresh() { | |
} | |
}); | |
recyclerView = (RecyclerView) findViewById(R.id.recycler_view); | |
final LinearLayoutManager layoutParams = new LinearLayoutManager(this); | |
recyclerView.setLayoutManager(layoutParams); | |
SomeAdapter adapter = new SomeAdapter(this); | |
recyclerView.setAdapter(adapter); | |
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { | |
@Override | |
public void onScrollStateChanged(int newState) { | |
} | |
@Override | |
public void onScrolled(int dx, int dy) { | |
refreshLayout.setEnabled(layoutParams.findFirstCompletelyVisibleItemPosition() == 0); | |
} | |
}); | |
} | |
} |
@gintsgints, one thing I noticed is that if you don't enable the refresh only when the first item of the recycler view is completely visible, then, as of version 21.0.3 of the appcompat library, in Android 2.3 (who still supports that, right?) the swipe refresh layout will actually react to the pull down gesture and try to refresh, instead of delegating the touch event to the recycler view.
Thank you very much for this gist @VladSumtsov, your gist was very useful.
Thank you so much @VladSumtsov , very helpful gist
the last 2 method that is overrided should add a parameter in SDK 21:
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState){
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for example. But I have question. Why setOnScrollListener is used?