查看: 720|回复: 0

[手机开发] XRecyclerView实现下拉刷新、滚动到底部加载更多等功能

发表于 2017-12-25 08:00:00

介绍:
一个实现了下拉刷新,滚动到底部加载更多以及添加header功能的的RecyclerView。使用方式和RecyclerView完全一致,不需要额外的layout,不需要写特殊的adater。 加载效果内置了AVLoadingIndicatorView上的所有效果,可以根据需要指定。

项目地址:https://github.com/jianghejie/XRecyclerView

效果:


使用:

xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity">
  6. <com.jcodecraeer.xrecyclerview.XRecyclerView
  7. android:id="@+id/recyclerview"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent" />
  10. </RelativeLayout>
复制代码

2.MainActivity

  1. package com.example.xrecyclerview;
  2. import android.os.Bundle;
  3. import android.os.Handler;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.support.v7.widget.LinearLayoutManager;
  6. import android.view.LayoutInflater;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import com.jcodecraeer.xrecyclerview.ProgressStyle;
  12. import com.jcodecraeer.xrecyclerview.XRecyclerView;
  13. import java.util.ArrayList;
  14. public class MainActivity extends AppCompatActivity {
  15. private XRecyclerView mRecyclerView;
  16. private MyAdapter mAdapter;
  17. private ArrayList<String> listData;
  18. private int refreshTime = 0;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. mRecyclerView = (XRecyclerView)this.findViewById(R.id.recyclerview);
  24. LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  25. layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
  26. mRecyclerView.setLayoutManager(layoutManager);
  27. mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
  28. mRecyclerView.setLaodingMoreProgressStyle(ProgressStyle.Pacman);
  29. mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);
  30. View header = LayoutInflater.from(this).inflate(R.layout.recyclerview_header, (ViewGroup)findViewById(android.R.id.content),false);
  31. mRecyclerView.addHeaderView(header);
  32. mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
  33. @Override
  34. public void onRefresh() {
  35. refreshTime ++;
  36. new Handler().postDelayed(new Runnable(){
  37. public void run() {
  38. listData.clear();
  39. for(int i = 0; i < 15 ;i++){
  40. listData.add("item" + i + "after " + refreshTime + " times of refresh");
  41. }
  42. mAdapter.notifyDataSetChanged();
  43. mRecyclerView.refreshComplete();
  44. }
  45. }, 3000); //refresh data here
  46. }
  47. @Override
  48. public void onLoadMore() {
  49. new Handler().postDelayed(new Runnable(){
  50. public void run() {
  51. for(int i = 0; i < 15 ;i++){
  52. listData.add("item" + (i + listData.size()) );
  53. }
  54. mAdapter.notifyDataSetChanged();
  55. mRecyclerView.loadMoreComplete();
  56. }
  57. }, 3000);
  58. }
  59. });
  60. listData = new ArrayList<String>();
  61. mAdapter = new MyAdapter(listData);
  62. for(int i = 0; i < 15 ;i++){
  63. listData.add("item" + i);
  64. }
  65. mRecyclerView.setAdapter(mAdapter);
  66. }
  67. @Override
  68. public boolean onCreateOptionsMenu(Menu menu) {
  69. // Inflate the menu; this adds items to the action bar if it is present.
  70. getMenuInflater().inflate(R.menu.menu_main, menu);
  71. return true;
  72. }
  73. @Override
  74. public boolean onOptionsItemSelected(MenuItem item) {
  75. // Handle action bar item clicks here. The action bar will
  76. // automatically handle clicks on the Home/Up button, so long
  77. // as you specify a parent activity in AndroidManifest.xml.
  78. int id = item.getItemId();
  79. //noinspection SimplifiableIfStatement
  80. if (id == R.id.action_settings) {
  81. return true;
  82. }
  83. return super.onOptionsItemSelected(item);
  84. }
  85. }
复制代码

3. adapter

  1. package com.example.xrecyclerview;
  2. import android.support.v7.widget.RecyclerView;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.TextView;
  7. import java.util.ArrayList;
  8. /**
  9. * Created by jianghejie on 15/11/26.
  10. */
  11. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
  12. public ArrayList<String> datas = null;
  13. public MyAdapter(ArrayList<String> datas) {
  14. this.datas = datas;
  15. }
  16. //创建新View,被LayoutManager所调用
  17. @Override
  18. public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
  19. View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false);
  20. ViewHolder vh = new ViewHolder(view);
  21. return vh;
  22. }
  23. //将数据与界面进行绑定的操作
  24. @Override
  25. public void onBindViewHolder(ViewHolder viewHolder, int position) {
  26. viewHolder.mTextView.setText(datas.get(position));
  27. }
  28. //获取数据的数量
  29. @Override
  30. public int getItemCount() {
  31. return datas.size();
  32. }
  33. //自定义的ViewHolder,持有每个Item的的所有界面元素
  34. public static class ViewHolder extends RecyclerView.ViewHolder {
  35. public TextView mTextView;
  36. public ViewHolder(View view){
  37. super(view);
  38. mTextView = (TextView) view.findViewById(R.id.text);
  39. }
  40. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报