效果图:
文件目录
布局文件.xml:
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.day06_lianxi.MainActivity"> <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>
Java代码文件:
package com.example.day06_lianxi; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /** * Created by 刘重阳 on 2018/1/2. */ public class RefreshRecyclerAdapter extends RecyclerView.Adapter{ 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 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 newDatas) { newDatas.addAll(mTitles); mTitles.removeAll(mTitles); mTitles.addAll(newDatas); notifyDataSetChanged(); } public void addMoreItem(List newData) { mTitles.addAll(newData); isLoadMore = false; notifyDataSetChanged(); } public void startLoad() { isLoadMore = true; notifyDataSetChanged(); } }
package com.example.day06_lianxi; import android.os.Bundle; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.OrientationHelper; import android.support.v7.widget.RecyclerView; import android.util.TypedValue; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import butterknife.Bind; import butterknife.ButterKnife; 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() { ListnewDatas = new ArrayList (); 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 newDatas = new ArrayList (); 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:26.0.0-alpha1'