码迷,mamicode.com
首页 > 其他好文 > 详细

RecyclerView 案例

时间:2017-05-31 18:47:54      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:enable   scrolltop   linear   ini   animator   length   convert   rri   his   

RecyclerView基本使用Demo

MainActivity

  1. public class MainActivity extends ListActivity {
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. String[] array = {"基础,点击下试试",
  5. "添加",
  6. "删除",
  7. "ListView",
  8. "横向的GridView",
  9. "横向瀑布流",
  10. "纵向瀑布流",
  11. "分割线 ItemDecoration",
  12. "BaseRecyclerViewAdapterHelper基本使用"};
  13. setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<>(Arrays.asList(array))));
  14. }
  15. @Override
  16. protected void onListItemClick(ListView l, View v, int position, long id) {
  17. if (position <= 6) {
  18. Intent intent = new Intent(this, RecyclerViewActivity.class);
  19. intent.putExtra("type", position);
  20. startActivity(intent);
  21. } else if (position == 7) {
  22. startActivity(new Intent(this, RV_ItemDecorationActivity.class));
  23. } else if (position == 8) {
  24. startActivity(new Intent(this, BRVAHActivity.class));
  25. }
  26. }
  27. }

RecyclerViewActivity

  1. public class RecyclerViewActivity extends Activity implements MyOnItemClickLitener {
  2. private RecyclerView mRecyclerView;
  3. private MyRecyclerViewAdapter mRecyclerViewAdapter;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. mRecyclerView = (RecyclerView) findViewById(R.id.rv);
  9. init();
  10. }
  11. protected void init() {
  12. List<String> mDatas = new ArrayList<String>();
  13. for (int i = ‘A‘; i < ‘z‘; i++) {
  14. mDatas.add("" + (char) i);
  15. }
  16. mRecyclerViewAdapter = new MyRecyclerViewAdapter(this, mDatas);
  17. mRecyclerViewAdapter.setOnItemClickLitener(this);
  18. mRecyclerViewAdapter.setMargins(5);//addItemDecoration一会单独拿出来说
  19. mRecyclerView.setAdapter(mRecyclerViewAdapter);//设置adapter
  20. mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));//设置布局管理器
  21. mRecyclerView.setItemAnimator(new DefaultItemAnimator());//设置Item增加、移除动画
  22. }
  23. @Override
  24. public void onItemClick(View view, int position) {
  25. Toast.makeText(this, position + " 被点击了", Toast.LENGTH_SHORT).show();
  26. reInit(position);
  27. }
  28. @Override
  29. public void onItemLongClick(View view, int position) {
  30. Toast.makeText(this, position + "被长按了", Toast.LENGTH_SHORT).show();
  31. }
  32. private void reInit(int position) {
  33. int type = getIntent().getIntExtra("type", 0);
  34. switch (type) {
  35. case 0:
  36. mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));//添加间隔线
  37. mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));//添加间隔线
  38. break;
  39. case 1:
  40. mRecyclerViewAdapter.addDataAt(position, "添加一条数据\nposition=" + position);
  41. mRecyclerView.scrollToPosition(position);
  42. break;
  43. case 2:
  44. mRecyclerViewAdapter.removeDataAt(position);
  45. mRecyclerView.scrollToPosition(position);//删除最后一条数据时也不会报错
  46. break;
  47. case 3:
  48. mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
  49. break;
  50. case 4:
  51. mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4, GridLayoutManager.HORIZONTAL, false));//横向的GridView
  52. break;
  53. case 5://Staggered:错列的,叉排的
  54. mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(1 + new Random().nextInt(8), StaggeredGridLayoutManager.HORIZONTAL));//行
  55. mRecyclerViewAdapter.setRandomWidth(true);
  56. break;
  57. case 6:
  58. mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(1 + new Random().nextInt(5), StaggeredGridLayoutManager.VERTICAL));//列
  59. mRecyclerViewAdapter.setRandomHeight(true);
  60. break;
  61. }
  62. }
  63. }

MyRecyclerViewAdapter

  1. public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {
  2. private Context context;
  3. private List<String> mDatas;
  4. private MyOnItemClickLitener mOnItemClickLitener;
  5. private int margins = 0;
  6. private boolean randomWidth = false;
  7. private boolean randomHeight = false;
  8. public MyRecyclerViewAdapter(Context context, List<String> mDatas) {
  9. this.context = context;
  10. this.mDatas = mDatas;
  11. }
  12. @Override
  13. public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  14. return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item, parent, false));
  15. }
  16. @Override
  17. public void onBindViewHolder(final MyViewHolder holder, int position) {
  18. holder.tv.setText(mDatas.get(position));
  19. RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) holder.tv.getLayoutParams();
  20. lp.setMargins(margins, margins, margins, margins);//设置边距,这里完全可以放在RecyclerView的addItemDecoration方法中
  21. //横向时,item的宽度需要设置;纵向时,item的高度需要设置
  22. if (randomHeight) lp.height = 200 + new Random().nextInt(5) * 100;//**************************************************唯一的区别在这
  23. if (randomWidth) lp.width = 200 + new Random().nextInt(5) * 100;//***************************************************唯一的区别在这
  24. holder.tv.setLayoutParams(lp);
  25. // 如果设置了回调,则设置点击事件
  26. holder.itemView.setOnClickListener(new OnClickListener() {
  27. @Override
  28. public void onClick(View v) {
  29. if (mOnItemClickLitener != null) mOnItemClickLitener.onItemClick(holder.itemView, holder.getAdapterPosition());
  30. }
  31. });
  32. holder.itemView.setOnLongClickListener(new OnLongClickListener() {
  33. @Override
  34. public boolean onLongClick(View v) {
  35. if (mOnItemClickLitener != null) mOnItemClickLitener.onItemLongClick(holder.itemView, holder.getAdapterPosition());
  36. return false;
  37. }
  38. });
  39. }
  40. @Override
  41. public int getItemCount() {
  42. return mDatas.size();
  43. }
  44. /**
  45. * 添加并更新数据,同时具有动画效果
  46. */
  47. public void addDataAt(int position, String data) {
  48. mDatas.add(position, data);
  49. notifyItemInserted(position);//更新数据集,注意如果用adapter.notifyDataSetChanged()将没有动画效果
  50. }
  51. /**
  52. * 移除并更新数据,同时具有动画效果
  53. */
  54. public void removeDataAt(int position) {
  55. mDatas.remove(position);
  56. notifyItemRemoved(position);
  57. }
  58. public void setOnItemClickLitener(MyOnItemClickLitener mOnItemClickLitener) {
  59. this.mOnItemClickLitener = mOnItemClickLitener;
  60. }
  61. public void setMargins(int margins) {
  62. this.margins = margins;
  63. }
  64. public void setRandomWidth(boolean randomWidth) {
  65. this.randomWidth = randomWidth;
  66. }
  67. public void setRandomHeight(boolean randomHeight) {
  68. this.randomHeight = randomHeight;
  69. }
  70. class MyViewHolder extends RecyclerView.ViewHolder {
  71. TextView tv;
  72. public MyViewHolder(View view) {
  73. super(view);
  74. tv = (TextView) view.findViewById(R.id.id_num);
  75. }
  76. }
  77. }

MyOnItemClickLitener

  1. /**系统没有提供ClickListener和LongClickListener,我们自己通过接口回调处理 */
  2. public interface MyOnItemClickLitener {
  3. void onItemClick(View view, int position);
  4. void onItemLongClick(View view, int position);
  5. }

RV_ItemDecorationActivity

  1. public class RV_ItemDecorationActivity extends Activity implements MyOnItemClickLitener {
  2. private RecyclerView mRecyclerView;
  3. private MyRecyclerViewAdapter mRecyclerViewAdapter;
  4. private RecyclerView.ItemDecoration decoration;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. mRecyclerView = (RecyclerView) findViewById(R.id.rv);
  10. init();
  11. }
  12. protected void init() {
  13. List<String> mDatas = new ArrayList<String>();
  14. mDatas.add("0.添加VERTICAL间隔线");
  15. mDatas.add("1.添加HORIZONTAL间隔线");
  16. mDatas.add("2.移除VERTICAL间隔线");
  17. mDatas.add("3.recreate");
  18. mDatas.add("4.Horizontal_Divider_ItemDecoration");
  19. mDatas.add("5.Vertical_Divider_ItemDecoration");
  20. mDatas.add("6.不包含边界的空白间隔");
  21. mDatas.add("7.包含边界的空白间隔");
  22. mDatas.add("8.自定义图片间隔 true,false");
  23. mDatas.add("9.自定义颜色间隔 false,true");
  24. mDatas.add("10.显示但不绘制边界间隔");
  25. mDatas.add("11.不绘制左右边界间隔");
  26. mDatas.add("12.不绘制上下边界间隔");
  27. for (int i = ‘A‘; i < ‘z‘; i++) {
  28. mDatas.add("" + (char) i);
  29. }
  30. mRecyclerViewAdapter = new MyRecyclerViewAdapter(this, mDatas);
  31. mRecyclerViewAdapter.setOnItemClickLitener(this);
  32. decoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
  33. mRecyclerView.setAdapter(mRecyclerViewAdapter);//设置adapter
  34. mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));//设置布局管理器
  35. mRecyclerView.setItemAnimator(new DefaultItemAnimator());//设置Item增加、移除动画
  36. }
  37. @Override
  38. public void onItemClick(View view, int position) {
  39. Toast.makeText(this, position + " 被点击了", Toast.LENGTH_SHORT).show();
  40. reInit(position);
  41. }
  42. @Override
  43. public void onItemLongClick(View view, int position) {
  44. Toast.makeText(this, position + "被长按了", Toast.LENGTH_SHORT).show();
  45. }
  46. private void reInit(int position) {
  47. switch (position) {
  48. case 0:
  49. mRecyclerView.addItemDecoration(decoration);//添加间隔线
  50. break;
  51. case 1:
  52. mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));
  53. break;
  54. case 2:
  55. mRecyclerView.removeItemDecoration(decoration);//移除间隔线,即使没有添加也可以移除
  56. break;
  57. case 3:
  58. recreate();
  59. break;
  60. case 4://For layout manager having 【vertical orientation】 to draw horizontal divider
  61. mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)//不能和Vertical*同时使用
  62. .margin(20)//距离【左右】边界的距离margin(leftMargin, rightMargin)
  63. .size(5)//高度
  64. .color(0xff0000ff)
  65. //.drawable(R.mipmap.ic_launcher)//间隔线不使用颜色而使用drawable,color()会把drawable()覆盖掉
  66. .positionInsideItem(false)//间隔线是否绘制在item内容的上面,默认为false
  67. .showLastDivider()//默认不显示最后一条间隔线
  68. .build());
  69. break;
  70. case 5://For layout manager having 【horizontal orientation】 to draw vertical divider
  71. mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4, GridLayoutManager.HORIZONTAL, false));
  72. mRecyclerView.addItemDecoration(new VerticalDividerItemDecoration.Builder(this)//不能和Horizontal*同时使用
  73. .margin(20)//距离【上下】边界的距离margin(leftMargin, rightMargin)
  74. .size(5).color(0xff0000ff).build());
  75. break;
  76. case 6:
  77. mRecyclerView.addItemDecoration(new GridItemDecoration.Builder().spanCount(4).spaceSize(1).build());
  78. break;
  79. case 7:
  80. mRecyclerView.addItemDecoration(new GridItemDecoration.Builder().spanCount(4).spaceSize(50)
  81. .includeLREdge(true).includeTBEdge(true).build());//
  82. break;
  83. case 8:
  84. mRecyclerView.addItemDecoration(new GridItemDecoration.Builder().spanCount(4).spaceSize(12)
  85. .includeLREdge(true)
  86. .mDivider(getResources().getDrawable(R.mipmap.ic_launcher)).build());//
  87. break;
  88. case 9:
  89. mRecyclerView.addItemDecoration(new GridItemDecoration.Builder().spanCount(4).spaceSize(6)
  90. .includeTBEdge(true)
  91. .mDivider(new ColorDrawable(0x880000ff)).build());//
  92. break;
  93. case 10:
  94. mRecyclerView.addItemDecoration(new GridItemDecoration.Builder().spanCount(4).spaceSize(50)
  95. .includeLREdge(true).includeTBEdge(true).drawLREdge(false).drawTBEdge(false)
  96. .mDivider(new ColorDrawable(0x880000ff)).build());//
  97. break;
  98. case 11:
  99. mRecyclerView.addItemDecoration(new GridItemDecoration.Builder().spanCount(4).spaceSize(50)
  100. .includeLREdge(true).includeTBEdge(true).drawLREdge(false)
  101. .mDivider(new ColorDrawable(0x880000ff)).build());//
  102. break;
  103. case 12:
  104. mRecyclerView.addItemDecoration(new GridItemDecoration.Builder().spanCount(4).spaceSize(50)
  105. .includeLREdge(true).includeTBEdge(true).drawTBEdge(false)
  106. .mDivider(new ColorDrawable(0x880000ff)).build());//
  107. break;
  108. }
  109. }
  110. }

GridItemDecoration

  1. /**
  2. * 适用于GridView,支持在item之间设置任何类型的间距,支持控制是否显示上下左右间隔及是否绘制上下左右背景
  3. */
  4. public class GridItemDecoration extends RecyclerView.ItemDecoration {
  5. private Drawable mDivider;
  6. private int spanCount;
  7. private int spaceSize;
  8. private boolean includeLREdge;
  9. private boolean includeTBEdge;
  10. private boolean drawLREdge;
  11. private boolean drawTBEdge;
  12. private GridItemDecoration(Builder builder) {
  13. mDivider = builder.mDivider;
  14. spanCount = builder.spanCount;
  15. spaceSize = builder.spaceSize;
  16. includeLREdge = builder.includeLREdge;
  17. includeTBEdge = builder.includeTBEdge;
  18. drawLREdge = builder.drawLREdge;
  19. drawTBEdge = builder.drawTBEdge;
  20. }
  21. @Override
  22. public String toString() {
  23. return "GridItemDecoration{" +
  24. "spanCount=" + spanCount +
  25. ", spaceSize=" + spaceSize +
  26. ", includeLREdge=" + includeLREdge +
  27. ", includeTBEdge=" + includeTBEdge +
  28. ‘}‘;
  29. }
  30. @Override
  31. public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
  32. int position = parent.getChildAdapterPosition(view); // item position
  33. int column = position % spanCount; // item column
  34. if (includeLREdge) {
  35. outRect.left = spaceSize - column * spaceSize / spanCount;
  36. outRect.right = (column + 1) * spaceSize / spanCount;
  37. } else {
  38. outRect.left = column * spaceSize / spanCount;
  39. outRect.right = spaceSize - (column + 1) * spaceSize / spanCount;
  40. }
  41. if (includeTBEdge) {
  42. if (position < spanCount) outRect.top = spaceSize; // top edge
  43. outRect.bottom = spaceSize; // item bottom
  44. } else {
  45. if (position >= spanCount) outRect.top = spaceSize; // item top
  46. }
  47. }
  48. @Override
  49. public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
  50. if (mDivider != null) {
  51. drawHorizontal(c, parent);
  52. drawVertical(c, parent);
  53. if (includeLREdge && drawLREdge) drawLR(c, parent);
  54. if (includeTBEdge && drawTBEdge) drawTB(c, parent);
  55. }
  56. }
  57. private void drawLR(Canvas c, RecyclerView parent) {
  58. int childCount = parent.getChildCount();
  59. for (int i = 0; i < childCount; i++) {
  60. View child = parent.getChildAt(i);
  61. //最左边那条线
  62. if (i % spanCount == 0) {
  63. int left = child.getLeft() - spaceSize;
  64. int right = left + spaceSize;
  65. int bottom = child.getBottom();
  66. int top = child.getTop() - spaceSize;
  67. if (i == 0) top = child.getTop(); //最上边那条线
  68. mDivider.setBounds(left, top, right, bottom);
  69. mDivider.draw(c);
  70. }
  71. //最右边那条线
  72. if ((i + 1) % spanCount == 0) {
  73. int left = child.getRight();
  74. int right = left + spaceSize;
  75. int bottom = child.getBottom();
  76. int top = child.getTop() - spaceSize;
  77. if (i == spanCount - 1) top = child.getTop(); //最上边那条线
  78. mDivider.setBounds(left, top, right, bottom);
  79. mDivider.draw(c);
  80. }
  81. }
  82. }
  83. private void drawTB(Canvas c, RecyclerView parent) {
  84. int childCount = parent.getChildCount();
  85. for (int i = 0; i < childCount; i++) {
  86. View child = parent.getChildAt(i);
  87. //最上边那条线
  88. if (i < spanCount) {
  89. int top = child.getTop() - spaceSize;
  90. int bottom = top + spaceSize;
  91. int left = child.getLeft();
  92. int right = child.getRight() + spaceSize;
  93. if ((i + 1) % spanCount == 0 ||
  94. (childCount < spanCount && i == childCount - 1)) right = child.getRight(); //最右边那条线
  95. mDivider.setBounds(left, top, right, bottom);
  96. mDivider.draw(c);
  97. }
  98. //最下边那条线
  99. if (childCount % spanCount == 0 && i >= spanCount * (childCount / spanCount - 1)) {
  100. int top = child.getBottom();
  101. int bottom = top + spaceSize;
  102. int left = child.getLeft() - spaceSize;
  103. int right = child.getRight();
  104. if ((i + 1) % spanCount == 0) right = child.getRight() + spaceSize; //最右边那条线
  105. mDivider.setBounds(left, top, right, bottom);
  106. mDivider.draw(c);
  107. } else if (i >= spanCount * (childCount / spanCount)) {
  108. int top = child.getBottom();
  109. int bottom = top + spaceSize;
  110. int right = child.getRight();
  111. int left = child.getLeft() - spaceSize;
  112. if (!drawLREdge && i % spanCount == 0) left = child.getLeft(); //最左边那条线
  113. mDivider.setBounds(left, top, right, bottom);
  114. mDivider.draw(c);
  115. }
  116. }
  117. }
  118. private void drawHorizontal(Canvas c, RecyclerView parent) {
  119. int childCount = parent.getChildCount();
  120. for (int i = 0; i < childCount; i++) {
  121. if (i >= spanCount) {
  122. View child = parent.getChildAt(i);
  123. int left = child.getLeft() - spaceSize;
  124. if (i % spanCount == 0) left = child.getLeft();
  125. int right = child.getRight();
  126. int top = child.getTop() - spaceSize;
  127. int bottom = top + spaceSize;
  128. mDivider.setBounds(left, top, right, bottom);
  129. mDivider.draw(c);
  130. }
  131. }
  132. }
  133. private void drawVertical(Canvas c, RecyclerView parent) {
  134. int childCount = parent.getChildCount();
  135. for (int i = 0; i < childCount; i++) {
  136. if (i % spanCount != 0) {
  137. View child = parent.getChildAt(i);
  138. int top = child.getTop();
  139. int bottom = child.getBottom();
  140. int left = child.getLeft() - spaceSize;
  141. int right = left + spaceSize;
  142. mDivider.setBounds(left, top, right, bottom);
  143. mDivider.draw(c);
  144. }
  145. }
  146. }
  147. static final class Builder {
  148. private Drawable mDivider = null;
  149. private int spanCount;
  150. private int spaceSize;
  151. private boolean includeLREdge = false;
  152. private boolean includeTBEdge = false;
  153. private boolean drawLREdge = true;
  154. private boolean drawTBEdge = true;
  155. public Builder() {
  156. }
  157. /**
  158. * 若不指定,则使用空白代替
  159. */
  160. public Builder mDivider(Drawable val) {
  161. mDivider = val;
  162. return this;
  163. }
  164. /**
  165. * 行数或列数
  166. */
  167. public Builder spanCount(int val) {
  168. spanCount = val;
  169. return this;
  170. }
  171. /**
  172. * 行列间距大小
  173. */
  174. public Builder spaceSize(int val) {
  175. spaceSize = val;
  176. return this;
  177. }
  178. /**
  179. * 是否包含左右边界
  180. */
  181. public Builder includeLREdge(boolean val) {
  182. includeLREdge = val;
  183. return this;
  184. }
  185. /**
  186. * 是否包含上下边界
  187. */
  188. public Builder includeTBEdge(boolean val) {
  189. includeTBEdge = val;
  190. return this;
  191. }
  192. /**
  193. * 是否绘制左右边界
  194. */
  195. public Builder drawLREdge(boolean val) {
  196. drawLREdge = val;
  197. return this;
  198. }
  199. /**
  200. * 是否绘制上下边界
  201. */
  202. public Builder drawTBEdge(boolean val) {
  203. drawTBEdge = val;
  204. return this;
  205. }
  206. public GridItemDecoration build() {
  207. return new GridItemDecoration(this);
  208. }
  209. }
  210. }

BRVAHActivity

  1. public class BRVAHActivity extends Activity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
  2. private List<String> mDatas;
  3. private RecyclerView mRecyclerView;
  4. private PullToRefreshAdapter mAdapter;
  5. private SwipeRefreshLayout mSwipeRefreshLayout;
  6. private static final int TOTAL_COUNTER = 18;
  7. private static final int PAGE_SIZE = 6;
  8. private static final int DELAY_MILLIS = 1000;
  9. private int mCurrentCounter = 0;
  10. private boolean isErr;
  11. private boolean mLoadMoreEndGone = false;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
  17. mSwipeRefreshLayout.setOnRefreshListener(this);
  18. mSwipeRefreshLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
  19. mRecyclerView = (RecyclerView) findViewById(R.id.rv);
  20. mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
  21. initDate();
  22. initAdapter();
  23. ImageView headView = new ImageView(this);
  24. headView.setImageResource(R.mipmap.ic_launcher);
  25. mAdapter.addHeaderView(headView);
  26. }
  27. private void initDate() {
  28. mDatas = new ArrayList<String>();
  29. for (int i = ‘A‘; i < ‘z‘; i++) {
  30. mDatas.add("" + (char) i);
  31. }
  32. }
  33. private void initAdapter() {
  34. mAdapter = new PullToRefreshAdapter();
  35. mAdapter.setOnLoadMoreListener(this, mRecyclerView);
  36. mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);//动画
  37. mAdapter.setPreLoadNumber(3);//预加载
  38. mRecyclerView.setAdapter(mAdapter);
  39. mCurrentCounter = mAdapter.getData().size();
  40. //item点击事件
  41. mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
  42. @Override
  43. public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
  44. Toast.makeText(BRVAHActivity.this, "onItemClick" + position, Toast.LENGTH_SHORT).show();
  45. }
  46. });
  47. //item中View的点击事件
  48. mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
  49. @Override
  50. public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
  51. Toast.makeText(BRVAHActivity.this, "onItemChildClick" + position, Toast.LENGTH_SHORT).show();
  52. }
  53. });
  54. }
  55. @Override
  56. public void onLoadMoreRequested() {
  57. mSwipeRefreshLayout.setEnabled(false);
  58. if (mAdapter.getData().size() < PAGE_SIZE) {
  59. mAdapter.loadMoreEnd(true);
  60. } else {
  61. if (mCurrentCounter >= TOTAL_COUNTER) {
  62. //pullToRefreshAdapter.loadMoreEnd();//default visible
  63. mAdapter.loadMoreEnd(mLoadMoreEndGone);//true is gone,false is visible
  64. } else {
  65. if (isErr) {
  66. mAdapter.addData("新数据");
  67. mCurrentCounter = mAdapter.getData().size();
  68. mAdapter.loadMoreComplete();
  69. } else {
  70. isErr = true;
  71. Toast.makeText(BRVAHActivity.this, "错误", Toast.LENGTH_LONG).show();
  72. mAdapter.loadMoreFail();
  73. }
  74. }
  75. mSwipeRefreshLayout.setEnabled(true);
  76. }
  77. }
  78. @Override
  79. public void onRefresh() {
  80. mAdapter.setEnableLoadMore(false);//禁止加载
  81. new Handler().postDelayed(new Runnable() {
  82. @Override
  83. public void run() {
  84. mAdapter.setNewData(mDatas);
  85. isErr = false;
  86. mCurrentCounter = PAGE_SIZE;
  87. mSwipeRefreshLayout.setRefreshing(false);
  88. mAdapter.setEnableLoadMore(true);//启用加载
  89. }
  90. }, DELAY_MILLIS);
  91. }
  92. class PullToRefreshAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
  93. public PullToRefreshAdapter() {
  94. super(R.layout.item2, mDatas);
  95. }
  96. @Override
  97. protected void convert(BaseViewHolder helper, String item) {
  98. helper.setText(R.id.id_num, item)
  99. .setImageResource(R.id.iv, R.drawable.actionbar_add_icon)
  100. .addOnClickListener(R.id.iv);
  101. }
  102. }
  103. }

依赖

  1. dependencies {
  2. compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
  3. androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2‘, {
  4. exclude group: ‘com.android.support‘, module: ‘support-annotations‘
  5. })
  6. compile ‘com.android.support:appcompat-v7:25.3.1‘
  7. compile ‘com.android.support:recyclerview-v7:25.3.1‘
  8. testCompile ‘junit:junit:4.12‘
  9. compile ‘com.yqritc:recyclerview-flexibledivider:1.4.0‘
  10. compile ‘com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.17‘
  11. }





附件列表

     

    RecyclerView 案例

    标签:enable   scrolltop   linear   ini   animator   length   convert   rri   his   

    原文地址:http://www.cnblogs.com/baiqiantao/p/6925096.html

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!