SoFunction
Updated on 2025-03-11

Android RecyclerView pull-up loading and drop-down refresh

I wrote a refresh loading article"RecyclerView pull-up loading and pull-down refresh (basic version)", this time it is renovated and improved.

The comments in the code are very detailed, so I just uploaded the code.

Core implementation

package ;

import ;
import ;
import .;
import .;
import .;
import ;
import ;
import ;

/**
  * Class name:
  * Class Description: Pull down refresh and pull-up load
  * Created by: fly
  * Created date: 2017/2/2.
  * Version: V1.0
  */
public abstract class RefreshActivity<T extends Object,A extends > extends BaseActivity implements  {
 protected SwipeRefreshLayout swipeRefreshLayout;
 protected RecyclerView recyclerView;
 protected LinearLayoutManager linearLayoutManager;
 protected A adapter;
 protected List<T> lists = new ArrayList<>();
 protected T dataBeanLoadMore = createBean();
 protected boolean isLoadDataIng; // Default is false Whether the data is loading, false is not loading, true is loading protected boolean isScrollY; // Whether to slide on the Y axis (vertical direction), false does not slide, true slide
 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(setLayout());
 swipeRefreshLayout = (SwipeRefreshLayout) findViewById();
 (this);
 (); // Set the changing color recyclerView = (RecyclerView) findViewById();
 linearLayoutManager = new LinearLayoutManager(this);
 (linearLayoutManager);
 loadMore();
 }

 /**The function is to initialize common controls in the parent class, such as the control initialized by onCreate(@Nullable Bundle savedInstanceState) above*/
 protected abstract int setLayout();

 public abstract T createBean();

// public abstract A createAdapter();

 /**Pull-up load*/
 protected void loadMore() {
 (new () {

 /**
  *
  * @param recyclerView The currently scrolling RecyclerView
  * @param newState Current scrolling status
  * newState has three values:
  //Still, no scrolling
  public static final int SCROLL_STATE_IDLE = 0;

  //It is being dragged outside, usually the user is scrolling with his fingers
  public static final int SCROLL_STATE_DRAGGING = 1;

  //Automatic scrolling starts
  public static final int SCROLL_STATE_SETTLING = 2;
  */
 @Override
 public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
 (recyclerView, newState);
 if (newState == RecyclerView.SCROLL_STATE_IDLE && isLoadDataIng == false ) {
  int lastVisibleItem = (); // Get the corner mark of the last item  int totalItemCount = (); // Get the total number of items  if (lastVisibleItem == (totalItemCount - 1)) { // Judgment slides to the last item  if (!()) { //No more pull-down refresh status  if (isScrollY) { // There is a sliding on the Y axis (vertical direction)  // TODO performs pull-up loading  onShowRefresh();
  loadMoreData();
  }
  }
  }
 }
 }

 /**
  *
  * @param recyclerView The currently scrolled view
  * @param dx horizontal scroll distance
  * @param dy Vertical scroll distance
  */
 @Override
 public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
 (recyclerView, dx, dy);
 if (dy > 0) {
  isScrollY = true;
 }else{
  isScrollY = false;
 }
 }
 });
 }

 /**Loading data*/
 public abstract void loadMoreData();

 /**Show added in more View*/
 protected void onShowRefresh() {
 isLoadDataIng = true; // Loading data (dataBeanLoadMore);
 ();
 }

 /**Hide added in more Views*/
 protected void onHintLoadMore() {
 (dataBeanLoadMore);
 isLoadDataIng = false; // Not loading data ();
 }
}

package ;

import ;
import ;
import ;
import ;
import ;
import ;

/**
  * Class name:
  * Class Description: RecyclerView Refresh Method 2
  * Created by: fly
  * Created date: 2017/2/2.
  * Version: V1.0
  */
public class MainActivity extends RefreshActivity<DataBean,RecyclerAdapter>{

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 for (int i = 0; i < 20; i++) {
 DataBean dataBean = new DataBean();
  = (i);
 (dataBean);
 }
 adapter = new RecyclerAdapter(lists,this);
 (adapter);
 }

 @Override
 public void onRefresh() {
 new Handler().postDelayed(new Runnable() {
 @Override
 public void run() {
 ();
 ();
 (false);
 }
 },1000);
 }

 @Override
 protected int setLayout() {return .activity_main;}

 @Override
 public DataBean createBean() {return new DataBean(Refresh.LOAD_MORE);}

 @Override
 public void loadMoreData() {
 for (int i = 0; i < 5; i++) {
 DataBean dataBean = new DataBean();
  = (i) + "fly";
 (dataBean);
 }
 ();
 onHintLoadMore();
 }
}

Source code download:RecyclerViewRefresh Refresh Load

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.