PullToRefreshGridView上拉刷新,下拉加载

Posted 钱大少丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PullToRefreshGridView上拉刷新,下拉加载相关的知识,希望对你有一定的参考价值。

PullToRefreshGridView上拉刷新,下拉加载

 

布局:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:id="@+id/activity_main"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context="com.example.asus.pull_1.Activity.MainActivity">
 8 
 9    <com.handmark.pulltorefresh.library.PullToRefreshGridView
10        android:id="@+id/gridview"
11        android:layout_width="match_parent"
12        android:layout_height="match_parent">
13 
14    </com.handmark.pulltorefresh.library.PullToRefreshGridView>
15 </RelativeLayout>

Activity:

 1 public class MainActivity extends AppCompatActivity {
 2 
 3     @BindView(R.id.gridview)
 4     PullToRefreshGridView gridview;
 5     private int index=1;
 6     private ArrayList<Bean.DataBean> list;
 7     private AdapterListe adapterListe;
 8     private Retrofit builder;
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_main);
14         ButterKnife.bind(this);
15         gridview.setMode(PullToRefreshBase.Mode.BOTH);//能下拉刷新和上拉加载  
16         init();
17     }
18 
19     private void init() {
20         list = new ArrayList();
21         adapterListe = new AdapterListe(list, MainActivity.this);//适配器
22         gridview.setAdapter(adapterListe);
23         loadData(index);
24         gridview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
25            //刷新
26             @Override
27             public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
28                  list.clear();
29                 loadData(1);
30                 gridview.postDelayed(new Runnable() {
31                     @Override
32                     public void run() {
33                         gridview.onRefreshComplete();//加载完成后,设置刷新完成
34                     }
35                 },1000);
36             }
37             //加载
38             @Override
39             public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
40                 index++;
41                 list.clear();
42                 loadData(index);
43                 gridview.postDelayed(new Runnable() {
44                     @Override
45                     public void run() {
46                         gridview.onRefreshComplete();//加载完成后,设置刷新完成
47                     }
48                 },1000);
49             }
50         });
51 
52     }
53 
54     private void loadData(int index) {
55       //解析
56         builder = new Retrofit.Builder()
57                 .baseUrl("http://www.qubaobei.com/")
58                 .addConverterFactory(GsonConverterFactory.create())
59                 .build();
60         builder.create(HttpCall.class).getHttpCall(""+index).enqueue(new Callback<Bean>() {
61             @Override
62             public void onResponse(Call<Bean> call, Response<Bean> response) {
63                 list.addAll(response.body().getData());//把获取到的数据放到集合里
64                 adapterListe.notifyDataSetChanged();//加载完成后,设置刷新完成
65             }
66 
67             @Override
68             public void onFailure(Call<Bean> call, Throwable t) {
69                 Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
70             }
71         });
72     }
73 }

子布局:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="wrap_content">
 6     <TextView
 7         android:id="@+id/tv_title"
 8     android:layout_width="match_parent"
 9     android:layout_height="wrap_content" />
10     <ImageView
11         android:id="@+id/img"
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content" />
14     <TextView
15         android:id="@+id/tv_food_str"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content" />
18 </LinearLayout>

适配器:

 1 public class AdapterListe extends BaseAdapter{
 2     private List<Bean.DataBean> list;
 3     private Context context;
 4 
 5     public AdapterListe(List<Bean.DataBean> list, Context context) {
 6         this.list = list;
 7         this.context = context;
 8     }
 9 
10     @Override
11     public int getCount() {
12         return list.size();
13     }
14 
15     @Override
16     public Object getItem(int i) {
17         return list.get(i);
18     }
19 
20     @Override
21     public long getItemId(int i) {
22         return i;
23     }
24 
25     @Override
26     public View getView(int i, View view, ViewGroup viewGroup) {
27         view = LayoutInflater.from(context).inflate(R.layout.layout, viewGroup, false);
28         TextView tv_title,tv_food_str;
29         ImageView img;
30         tv_title=view.findViewById(R.id.tv_title);
31         tv_food_str=view.findViewById(R.id.tv_food_str);
32         img=view.findViewById(R.id.img);
33         tv_title.setText(list.get(i).getTitle());
34         tv_food_str.setText(list.get(i).getFood_str());
35         Glide.with(context).load(list.get(i).getPic()).into(img);
36         return view;
37     }
38 }

接口:

1 public interface HttpCall {
2     @GET("ios/cf/dish_list.php?stage_id=1&limit=10&page=")//注解(get)获取
3     Call<Bean> getHttpCall(@Query("page")String page);//注解(@Query)字符串拼接
4 }

Bean类就不发了记着加网络权限和Library包添加依赖

以上是关于PullToRefreshGridView上拉刷新,下拉加载的主要内容,如果未能解决你的问题,请参考以下文章

vue loadMore 上拉刷新不能实现的坑

今天终于学会了下拉刷新,上拉刷新

UI组件——用jQuery做一个上拉刷新

上拉刷新后台完整版

SwipeRefreshLayout + RecyclerView 实现 上拉刷新 和 下拉刷新

MUI - 上拉刷新/下拉加载