3.实现下拉刷新,上滑加载
为了代码的重用效率高,我写了一个基类BaseActivity:
public abstract class BaseActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener { /*** * 处理下拉和刷新滴 */ protected SwipeRefreshLayout swipeRefreshLayout; /*** * 进化的ListView */ protected RecyclerView recyclerView; /*** * 该布局在没有网络的时候,显示的布局 */ protected LinearLayout linearLayout; /*** * RecyclerView的样式(网格,瀑布,线性) */ protected LinearLayoutManager mLayoutManager; /*** * 记录最后一项的位置 */ protected int lastVisibleItem=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.swipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.activity_main_swipe); this.recyclerView=(RecyclerView)findViewById(R.id.activity_main_recyclerview); this.linearLayout=(LinearLayout)findViewById(R.id.activity_main_linearlayout); initView(); recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); lastVisibleItem = mLayoutManager.findLastVisibleItemPosition(); } @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); onRecyclerViewStateChanged(newState); } }); } @Override public void onRefresh() { onRecyclerViewRefresh(); } /*** * 初始化界面 */ public abstract void initView(); /*** * 监听RecyclerView滑动事件 * @param newState 滑动状态 */ public abstract void onRecyclerViewStateChanged(int newState); /*** * 下拉刷新处理 */ public abstract void onRecyclerViewRefresh(); }
注释非常明确,布局中有一个linearlayout其中无任何控件,是为了扩展任何你需要的无网络时显示的界面的。你只需要继承该类实现这几个抽象方法。
㈠自定义适配器
代码如下:
public class LYJRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { /*** * 每项的数据集合 */ private List<String> messageItems; /*** * 监听item点击事件。 */ private LYJItemClickListener mItemClickListener; /*** * 一共显示多少条数据 */ private int totalSize; public LYJRecyclerViewAdapter(List<String> messageItems,int size){ this.messageItems=messageItems; this.totalSize=size; } /*** * 监听点击事件接口 */ public interface LYJItemClickListener { public void onItemClick(View view, int postion); } /*** * 设置item点击事件 * @param listener */ public void setOnItemClickListener(LYJItemClickListener listener) { this.mItemClickListener = listener; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { if (i == Constants.TYPE_ITEM) { View view = LayoutInflater.from(viewGroup.getContext()).inflate( R.layout.activity_main_adapter_item, null); view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); return new ItemViewHolder(view,this.mItemClickListener); } //滑动到底部返回footview else if (i == Constants.TYPE_FOOTER) { View view = LayoutInflater.from(viewGroup.getContext()).inflate( R.layout.activity_main_adapter_footview, null); view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); return new FooterViewHolder(view); } return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) { if(viewHolder instanceof ItemViewHolder){ ((ItemViewHolder) viewHolder).name.setText(messageItems.get(i)); }else{ if(this.totalSize==(getItemCount()-1)){ ((FooterViewHolder)viewHolder).flagTxt.setText("已经加载完全部内容"); }else{ ((FooterViewHolder)viewHolder).flagTxt.setText("正在加载中........"); } } } @Override public int getItemCount() { return messageItems.size()+1;//加1是多的footview那一项,也就是滑动到footview就加载,而不是最后数据项。 } @Override public int getItemViewType(int position) { if (position + 1 == getItemCount()) { return Constants.TYPE_FOOTER; } else { return Constants.TYPE_ITEM; } } /*** * 底部布局 */ public class FooterViewHolder extends RecyclerView.ViewHolder { private TextView flagTxt; public FooterViewHolder(View itemView) { super(itemView); this.flagTxt=(TextView)itemView.findViewById(R.id.activity_main_adapter_footview_txt); } } /*** * 数据项布局 */ class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private TextView name; private LYJItemClickListener mListener;//设置点击事件 public ItemViewHolder(View itemView, LYJItemClickListener listener) { super(itemView); this.name = (TextView) itemView.findViewById(R.id.activity_main_adapter_item_name); this.mListener = listener; itemView.setOnClickListener(this);//设置点击事件 } @Override public void onClick(View v) { if (mListener != null) { mListener.onItemClick(v, getPosition()); } } } }
㈡MainActivity的实现
其继承自BaseActivity,并且实现item点击事件接口LYJRecyclerViewAdapter.LYJItemClickListener:
public class MainActivity extends BaseActivity implements LYJRecyclerViewAdapter.LYJItemClickListener{ /*** * 数据项 */ private List<String> messageItems=new ArrayList<>(); /*** * 自定义adapter */ private LYJRecyclerViewAdapter adapter; /*** * 获取资源文件字符串中间转换集合 */ private List<String> strFlag; @Override public void initView() { Toolbar toolbar=(Toolbar)findViewById(R.id.activity_main_toolbar); toolbar.setTitle(""); setSupportActionBar(toolbar); this.swipeRefreshLayout.setColorSchemeColors(Color.RED);//设置加载内圈颜色 this.swipeRefreshLayout.setOnRefreshListener(this);//设置下拉刷新事件 this.swipeRefreshLayout.setProgressBackgroundColorSchemeColor(getResources().getColor(R.color.activity_main_tablayout_bg));//设置加载外圈颜色 // 这句话是为了,第一次进入页面的时候显示加载进度条 swipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources() .getDisplayMetrics())); mLayoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);//设置布局样式 recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(mLayoutManager); loadingRecyclerView(Constants.LISTVIEW_INIT);//初始化RecyclerView } @Override public void onRecyclerViewStateChanged(int newState) { if (messageItems == null || messageItems.size() <= 0) { Snackbar.make(swipeRefreshLayout, "没有数据得先下拉刷新", Snackbar.LENGTH_SHORT).show(); return; } //滚动事件结束并且到达最底端 if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) { loadingRecyclerView(Constants.LISTVIEW_DOWNLOAD);//下滑RecyclerView } } @Override public void onRecyclerViewRefresh() { loadingRecyclerView(Constants.LISTVIEW_REFRESH);//下拉刷新RecyclerView } public void loadingRecyclerView(int recyclerViewState){ swipeRefreshLayout.setRefreshing(true);//打开加载动画 if (!LYJNetwork.isNetworkAvailable(MainActivity.this)) { Snackbar.make(swipeRefreshLayout, "没有网络你逗我玩啊?", Snackbar.LENGTH_SHORT).show(); swipeRefreshLayout.setRefreshing(false);//没有网络时候直接关闭加载动画 return; } //当为初始化的时候 if(recyclerViewState==Constants.LISTVIEW_INIT){ addStringToList(); adapter=new LYJRecyclerViewAdapter(messageItems,100); recyclerView.setAdapter(adapter); }else if(recyclerViewState==Constants.LISTVIEW_REFRESH){ //当为下拉刷新的时候 messageItems.clear(); addStringToList(); recyclerView.setAdapter(null); adapter = new LYJRecyclerViewAdapter(messageItems,100); adapter.setOnItemClickListener(MainActivity.this); recyclerView.setAdapter(adapter); }else{ //当为下滑加载的时候 if(messageItems.size()!=100){ addStringToList(); adapter.notifyDataSetChanged(); } } swipeRefreshLayout.setRefreshing(false);//执行完成也要关闭加载动画 } @Override public void onItemClick(View view, int postion) { //每项的点击事件 } //模拟增加数据 public void addStringToList(){ strFlag= Arrays.asList(getResources().getStringArray(R.array.welltest_array_string)); for(int i=0;i<strFlag.size();i++){ messageItems.add(strFlag.get(i)); } } }
这样谷歌官方控件的下拉刷新,上滑动加载就完成了。
从这里可以看到,虽然说ListView有点击事件,有许多扩展,但你想扩展ListView就必须重构很多地方。而RecyclerView,虽然什么都没有,但你扩展起来要方便的多。这就是从0开始的优势。当一个框架继承了很多很多东西,那么你要修改其中的东西,那么就是牵一发而动全身。没有最适合的框架,只有最优解。
本文源码:(MainActivity为本文源码,自行修改)
http://download.csdn.net/detail/liyuanjinglyj/9418932
看看最后实现的效果: