PullToRefreshGridView上拉刷新,下拉加载

布局:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.asus.pull_1.Activity.MainActivity"> <com.handmark.pulltorefresh.library.PullToRefreshGridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"> </com.handmark.pulltorefresh.library.PullToRefreshGridView>
</RelativeLayout>

Activity:

 public class MainActivity extends AppCompatActivity {

     @BindView(R.id.gridview)
PullToRefreshGridView gridview;
private int index=1;
private ArrayList<Bean.DataBean> list;
private AdapterListe adapterListe;
private Retrofit builder; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
gridview.setMode(PullToRefreshBase.Mode.BOTH);//能下拉刷新和上拉加载  
init();
} private void init() {
list = new ArrayList();
adapterListe = new AdapterListe(list, MainActivity.this);//适配器
gridview.setAdapter(adapterListe);
loadData(index);
gridview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
//刷新
@Override
public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
list.clear();
loadData(1);
gridview.postDelayed(new Runnable() {
@Override
public void run() {
gridview.onRefreshComplete();//加载完成后,设置刷新完成
}
},1000);
}
//加载
@Override
public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
index++;
list.clear();
loadData(index);
gridview.postDelayed(new Runnable() {
@Override
public void run() {
gridview.onRefreshComplete();//加载完成后,设置刷新完成
}
},1000);
}
}); } private void loadData(int index) {
//解析
builder = new Retrofit.Builder()
.baseUrl("http://www.qubaobei.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
builder.create(HttpCall.class).getHttpCall(""+index).enqueue(new Callback<Bean>() {
@Override
public void onResponse(Call<Bean> call, Response<Bean> response) {
list.addAll(response.body().getData());//把获取到的数据放到集合里
adapterListe.notifyDataSetChanged();//加载完成后,设置刷新完成
} @Override
public void onFailure(Call<Bean> call, Throwable t) {
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
}
});
}
}

子布局:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_food_str"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

适配器:

 public class AdapterListe extends BaseAdapter{
private List<Bean.DataBean> list;
private Context context; public AdapterListe(List<Bean.DataBean> list, Context context) {
this.list = list;
this.context = context;
} @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int i) {
return list.get(i);
} @Override
public long getItemId(int i) {
return i;
} @Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(context).inflate(R.layout.layout, viewGroup, false);
TextView tv_title,tv_food_str;
ImageView img;
tv_title=view.findViewById(R.id.tv_title);
tv_food_str=view.findViewById(R.id.tv_food_str);
img=view.findViewById(R.id.img);
tv_title.setText(list.get(i).getTitle());
tv_food_str.setText(list.get(i).getFood_str());
Glide.with(context).load(list.get(i).getPic()).into(img);
return view;
}
}

接口:

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

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

05-08 15:08