使用SwipeRefreshLayout和RecyclerView实现下拉刷新上拉加载更多

时间:2022-12-22 22:09:55
  • 效果图:
    使用SwipeRefreshLayout和RecyclerView实现下拉刷新上拉加载更多

  • 文件目录
    使用SwipeRefreshLayout和RecyclerView实现下拉刷新上拉加载更多

  • 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/demo_swiperefreshlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">


<android.support.v7.widget.RecyclerView
android:id="@+id/demo_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />


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

</LinearLayout>
  • RefreshRecyclerAdapter
public class RefreshRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_ITEM = 0;
private static final int TYPE_FOOTER = 1;

public boolean isLoadMore = false;

private Context mContext;
private LayoutInflater mInflater;
private List<String> mTitles = null;

public RefreshRecyclerAdapter(Context context) {
this.mContext = context;
this.mInflater = LayoutInflater.from(context);
this.mTitles = new ArrayList<>();
for (int i = 0; i < 20; i++) {
int index = i + 1;
mTitles.add("item" + index);
}
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(-1, -2);
if (viewType == TYPE_ITEM) {
final TextView view = new TextView(mContext);
view.setGravity(Gravity.CENTER);
view.setLayoutParams(lp);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
final ProgressBar bar = new ProgressBar(mContext);
bar.setLayoutParams(lp);
ProgressViewHoler barViewHolder = new ProgressViewHoler(bar);
return barViewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolder) {
((ViewHolder) holder).item_tv.setText(mTitles.get(position));
((ViewHolder) holder).item_tv.setTag(position);
}
// ((ProgressViewHoler)(holder)).bar.setScrollBarStyle(ProgressBar.);
}

@Override
public int getItemCount() {
if (isLoadMore) {
return mTitles.size() + 1;
}
return mTitles.size();
}

@Override
public int getItemViewType(int position) {
if (position + 1 == getItemCount() && isLoadMore) {
return TYPE_FOOTER;
}
return TYPE_ITEM;
}

public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView item_tv;

public ViewHolder(View itemView) {
super(itemView);
item_tv = (TextView) itemView;
}
}

public static class ProgressViewHoler extends RecyclerView.ViewHolder {
public ProgressBar bar;

public ProgressViewHoler(View itemView) {
super(itemView);
bar = (ProgressBar) itemView;
}
}

public void addItem(List<String> newDatas) {
newDatas.addAll(mTitles);
mTitles.removeAll(mTitles);
mTitles.addAll(newDatas);
notifyDataSetChanged();
}

public void addMoreItem(List<String> newData) {
mTitles.addAll(newData);
isLoadMore = false;
notifyDataSetChanged();
}

public void startLoad() {
isLoadMore = true;
notifyDataSetChanged();
}
}
  • MainActivity.java
public class MainActivity extends AppCompatActivity {

@Bind(R.id.demo_swiperefreshlayout)
SwipeRefreshLayout swipeRefreshLayout;

@Bind(R.id.demo_recycler)
RecyclerView recyclerView;

private RefreshRecyclerAdapter adapter;
private LinearLayoutManager linearLayoutManager;
private int lastVisibleItem;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);

swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
android.R.color.holo_red_light, android.R.color.holo_orange_light,
android.R.color.holo_green_light);
swipeRefreshLayout.setProgressViewOffset(false, 0,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter = new RefreshRecyclerAdapter(this));

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
List<String> newDatas = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
int index = i + 1;
newDatas.add("new item" + index);
}
adapter.addItem(newDatas);
swipeRefreshLayout.setRefreshing(false);
Toast.makeText(MainActivity.this, "更新了5条数据", Toast.LENGTH_SHORT).show();
}
}, 5000);
}
});

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(swipeRefreshLayout.isRefreshing()) return;
if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem == adapter.getItemCount() - 1) {
adapter.startLoad();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
List<String> newDatas = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
int index = i + 1;
newDatas.add("more item" + index);
}
adapter.addMoreItem(newDatas);
}
}, 1000);
}
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
}
});
}
}
  • gradle依赖
   compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.android.support:recyclerview-v7:25.1.1'