赞
踩
//页面布局
<?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="match_parent"
android:background="#f56f98"
>
<com.handmark.pulltorefresh.library.PullToRefreshGridView
android:id="@+id/fuli_gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
/>
</LinearLayout>
//java代码
public class FuLiFragment extends Fragment {
private View view;
private PullToRefreshGridView gridview;
private int page=1;
private List<FuLiBean.ResultsBean> list=new ArrayList<>();
private MyBaseAdapter02 base02;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fuli_shouye_fragment, null);
//初始化页面
initViews();
return view;
}
//初始化页面
private void initViews() {
//获取id
gridview = view.findViewById(R.id.fuli_gridview);
//开始上拉刷新/下拉加载
gridview.setMode(PullToRefreshBase.Mode.BOTH);
gridview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
//刷新
page=1;
//加载网络数据
getinitWl();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
page++;
//加载网络数据
getinitWl();
}
});
}
//默认懒加载
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
getinitWl();
}
}
//加载适配器
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//创建适配器
base02 = new MyBaseAdapter02(getActivity(), list);
gridview.setAdapter(base02);
}
//获取网络数据
public void getinitWl(){
HttpUtils httpUtils = HttpUtils.getHttpUtils();
httpUtils.get(HttpConfig.URL3);
httpUtils.setHttpListener(new HttpUtils.HttpListener() {
@Override
public void getSuccess(String json) {
Gson gson = new Gson();
FuLiBean fuli = gson.fromJson(json, FuLiBean.class);
if(page==1){
list.clear();
}
list.addAll(fuli.getResults());
//刷新适配器
base02.notifyDataSetChanged();
gridview.onRefreshComplete();
}
@Override
public void getError(String error) {
}
});
}
public static FuLiFragment create(String title) {
Bundle bundle = new Bundle();
bundle.putString("title",title);
FuLiFragment fl = new FuLiFragment();
fl.setArguments(bundle);
return fl;
}
}
//适配器
public class MyBaseAdapter02 extends BaseAdapter {
private Context context;
private List<FuLiBean.ResultsBean> list;
public MyBaseAdapter02(Context context, List<FuLiBean.ResultsBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView=View.inflate(context, R.layout.item_fuli,null);
holder = new ViewHolder();
holder.img=convertView.findViewById(R.id.fuli_img);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
String pic = list.get(position).getUrl();
ImageLoader.getInstance().displayImage(pic,holder.img, MyApp.getOptions());
return convertView;
}
class ViewHolder{
ImageView img;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。