查看: 364|回复: 0

[Java学习] Spring实现上拉刷新和下拉加载效果

发表于 2018-1-5 08:00:00

本文实例为大家分享了Spring实现上拉刷新和下拉加载效果,供大家参考,具体内容如下

导依赖:

  1. compile 'com.android.support:recyclerview-v7:25.3.1'
  2. compile 'com.liaoinstan.springview:library:1.3.0'
  3. compile files('libs/glide-3.7.0.jar')
复制代码

activity_main布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context="muhanxi.recycleview.MainActivity">
  9. <com.liaoinstan.springview.widget.SpringView
  10. android:layout_width="match_parent"
  11. android:id="@+id/springview"
  12. android:layout_height="match_parent">
  13. <android.support.v7.widget.RecyclerView
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:id="@+id/recyclerview"/>
  17. </com.liaoinstan.springview.widget.SpringView>
  18. </LinearLayout>
复制代码

MainActivity 主类

  1. public class MainActivity extends Activity {
  2. private RecyclerView recyclerView;
  3. private List<String> list = new ArrayList<>();
  4. private SpringView springView;
  5. private LinearLayoutManager linearLayoutManager;
  6. @RequiresApi(api = Build.VERSION_CODES.GINGERBREAD)
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
  12. list.add("http://f10.baidu.com/it/u=2881303562,336932824&fm=72");
  13. list.add("http://f11.baidu.com/it/u=681755471,2018070071&fm=72");
  14. list.add("http://f10.baidu.com/it/u=960650584,863938083&fm=72");
  15. list.add("http://img0.imgtn.bdimg.com/it/u=783060973,4278100629&fm=27&gp=0.jpg");
  16. list.add("http://img1.imgtn.bdimg.com/it/u=3743124979,3234956668&fm=27&gp=0.jpg");
  17. list.add("http://img4.imgtn.bdimg.com/it/u=3468613159,957707785&fm=27&gp=0.jpg");
  18. list.add("http://img3.imgtn.bdimg.com/it/u=2971205354,485034289&fm=27&gp=0.jpg");
  19. list.add("http://f10.baidu.com/it/u=2881303562,336932824&fm=72");
  20. list.add("http://f11.baidu.com/it/u=681755471,2018070071&fm=72");
  21. list.add("http://f10.baidu.com/it/u=960650584,863938083&fm=72");
  22. list.add("http://img0.imgtn.bdimg.com/it/u=783060973,4278100629&fm=27&gp=0.jpg");
  23. list.add("http://img1.imgtn.bdimg.com/it/u=3743124979,3234956668&fm=27&gp=0.jpg");
  24. list.add("http://img4.imgtn.bdimg.com/it/u=3468613159,957707785&fm=27&gp=0.jpg");
  25. list.add("http://img3.imgtn.bdimg.com/it/u=2971205354,485034289&fm=27&gp=0.jpg");
  26. // 设置布局管理器
  27. // new GridLayoutManager() 九宫格布局管理器
  28. // new LinearLayoutManager() listview 布局管理器
  29. //
  30. // new StaggeredGridLayoutManager() 瀑布流 布局管理器
  31. // recyclerView.setLayoutManager(new GridLayoutManager(this,5));
  32. // recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, LinearLayout.VERTICAL));
  33. // RecyclerViewAdapter adapter = new RecyclerViewAdapter(this,list) ;
  34. MultiAdapter adapter = new MultiAdapter(this,list) ;
  35. linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
  36. recyclerView.setLayoutManager(linearLayoutManager);
  37. recyclerView.setAdapter(adapter);
  38. //
  39. // adapter.setIListener(new RecyclerViewAdapter.Listener() {
  40. // @Override
  41. // public void onClick(View view, int position) {
  42. // Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_SHORT).show();
  43. // }
  44. //
  45. // @Override
  46. // public void longClick(View view, int position) {
  47. //
  48. // }
  49. // });
  50. recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
  51. @Override
  52. public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  53. super.onScrollStateChanged(recyclerView, newState);
  54. // linearLayoutManager.findFirstVisibleItemPosition()
  55. }
  56. @Override
  57. public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  58. super.onScrolled(recyclerView, dx, dy);
  59. }
  60. });
  61. // recyclerView.setOnI
  62. // recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
  63. // .color(Color.RED).build());
  64. springView = (SpringView) findViewById(R.id.springview);
  65. springView.setHeader(new DefaultHeader(this));
  66. springView.setFooter(new DefaultFooter(this));
  67. springView.setListener(new SpringView.OnFreshListener() {
  68. @Override
  69. public void onRefresh() {
  70. //下啦
  71. springView.onFinishFreshAndLoad();
  72. }
  73. @Override
  74. public void onLoadmore() {
  75. // 上啦
  76. springView.onFinishFreshAndLoad();
  77. }
  78. });
  79. // adapter.notifyDataSetChanged();
  80. }
  81. }
复制代码

下拉布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:gravity="center"
  6. android:orientation="vertical">
  7. <ImageView
  8. android:src="@mipmap/ic_launcher"
  9. android:scaleType="centerCrop"
  10. android:id="@+id/item_simageview"
  11. android:layout_width="match_parent"
  12. android:layout_height="100dp" />
  13. </LinearLayout>
复制代码

刷新布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:gravity="center"
  6. android:orientation="vertical">
  7. <ImageView
  8. android:src="@mipmap/ic_launcher"
  9. android:id="@+id/item_simageview"
  10. android:layout_width="match_parent"
  11. android:layout_height="100dp" />
  12. <TextView
  13. android:background="@color/colorPrimary"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:id="@+id/item_stextview"/>
  17. </LinearLayout>
复制代码

MultiAdapter 适配器

  1. public class MultiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  2. private Context context;
  3. private List<String> list;
  4. public MultiAdapter(Context context, List<String> list) {
  5. this.context = context;
  6. this.list = list;
  7. }
  8. @Override
  9. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  10. if (viewType == 0) {
  11. View view = LayoutInflater.from(context).inflate(R.layout.layout_fitem,parent, false);
  12. return new FViewHolder(view);
  13. } else {
  14. View view = LayoutInflater.from(context).inflate(R.layout.layout_sitem, parent, false);
  15. return new SViewHolder(view);
  16. }
  17. }
  18. @Override
  19. public int getItemViewType(int position) {
  20. return position % 2 ;
  21. }
  22. @Override
  23. public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  24. if(holder instanceof FViewHolder){
  25. FViewHolder fViewHolder = (FViewHolder)holder ;
  26. fViewHolder.itemStextview.setText(position+"");
  27. Glide.with(context).load(list.get(position)).into(fViewHolder.itemSimageview);
  28. // ImageLoader.getInstance().displayImage(list.get(position),fViewHolder.itemSimageview);
  29. } else {
  30. SViewHolder sViewHolder = (SViewHolder)holder ;
  31. Glide.with(context).load(list.get(position)).into(sViewHolder.itemSimageview);
  32. // ImageLoader.getInstance().displayImage(list.get(position),sViewHolder.itemSimageview);
  33. }
  34. }
  35. @Override
  36. public int getItemCount() {
  37. return list.size();
  38. }
  39. static class FViewHolder extends RecyclerView.ViewHolder{
  40. @BindView(R.id.item_simageview)
  41. ImageView itemSimageview;
  42. @BindView(R.id.item_stextview)
  43. TextView itemStextview;
  44. FViewHolder(View view) {
  45. super(view);
  46. ButterKnife.bind(this, view);
  47. }
  48. }
  49. class SViewHolder extends RecyclerView.ViewHolder{
  50. @BindView(R.id.item_simageview)
  51. ImageView itemSimageview;
  52. SViewHolder(View view) {
  53. super(view);
  54. ButterKnife.bind(this, view);
  55. }
  56. }
  57. }
复制代码

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



回复

使用道具 举报