Spring实现上拉刷新和下拉加载效果

时间:2022-11-10 17:40:36

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

导依赖:

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

activity_main布局

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

MainActivity 主类 

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

下拉布局

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

刷新布局

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

MultiAdapter 适配器 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
public class MultiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
 
 
  private Context context;
  private List<String> list;
 
 
  public MultiAdapter(Context context, List<String> list) {
    this.context = context;
    this.list = list;
 
  }
 
  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 
    if (viewType == 0) {
 
      View view = LayoutInflater.from(context).inflate(R.layout.layout_fitem,parent, false);
      return new FViewHolder(view);
 
    } else {
      View view = LayoutInflater.from(context).inflate(R.layout.layout_sitem, parent, false);
 
      return new SViewHolder(view);
 
    }
 
 
  }
 
 
  @Override
  public int getItemViewType(int position) {
    return position % 2 ;
  }
 
  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
 
 
 
 
    if(holder instanceof FViewHolder){
 
      FViewHolder fViewHolder = (FViewHolder)holder ;
      fViewHolder.itemStextview.setText(position+"");
      Glide.with(context).load(list.get(position)).into(fViewHolder.itemSimageview);
 
//      ImageLoader.getInstance().displayImage(list.get(position),fViewHolder.itemSimageview);
 
 
    } else {
 
      SViewHolder sViewHolder = (SViewHolder)holder ;
      Glide.with(context).load(list.get(position)).into(sViewHolder.itemSimageview);
 
//      ImageLoader.getInstance().displayImage(list.get(position),sViewHolder.itemSimageview);
 
    }
 
 
  }
 
  @Override
  public int getItemCount() {
    return list.size();
  }
 
 
 
  static class FViewHolder extends RecyclerView.ViewHolder{
    @BindView(R.id.item_simageview)
    ImageView itemSimageview;
    @BindView(R.id.item_stextview)
    TextView itemStextview;
 
    FViewHolder(View view) {
      super(view);
      ButterKnife.bind(this, view);
    }
  }
 
  class SViewHolder extends RecyclerView.ViewHolder{
    @BindView(R.id.item_simageview)
    ImageView itemSimageview;
 
    SViewHolder(View view) {
      super(view);
      ButterKnife.bind(this, view);
    }
  }
}

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

原文链接:http://blog.csdn.net/AnGuoEr3344/article/details/78483360