import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.bwlq.yuekaodemo1.R;
import com.bwlq.yuekaodemo1.bean.Products;
import com.bwlq.yuekaodemo1.utils.Utils;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshExpandableListView;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
import com.youth.banner.loader.ImageLoaderInterface;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by liqiang on 2017/11/15.
*/
public class Frag1 extends Fragment {
private View view;
private PullToRefreshListView plv;
private List<Products.DataBean> list = new ArrayList<>();
private Banner banner;
String urlString = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=";
String[] urls = {"https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg","http://pic31.photophoto.cn/20140519/0010023932607971_b.jpg"
,"http://pic.58pic.com/58pic/15/23/09/74T58PICZjg_1024.jpg","http://pic7.nipic.com/20100517/2474391_114652343079_2.jpg"
,"http://pic.58pic.com/58pic/12/21/22/54P58PICBkX.jpg"};
private ListView refreshableList;
private int i = 0;
private MAdapter mAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag1, container, false);
//获取pulltorefresh控件
plv = view.findViewById(R.id.plv);
//设置上下拉
plv.setMode(PullToRefreshBase.Mode.BOTH);
//刷新加载监听事件
plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
list.clear();
getNetData(0);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
getNetData(i++);
}
});
//banner轮播视图
View inflate = View.inflate(getContext(), R.layout.banner_layout, null);
//banner控件
banner = inflate.findViewById(R.id.banner);
//设置banner图片
banner.setImages(Arrays.asList(urls));
//设置ImageLoader请求网络显示图片
banner.setImageLoader(new MLoader());
//通过pulltorefresh控件转换为refreshableList(listview控件)
refreshableList = plv.getRefreshableView();
//给refreshableList(listview控件)设置头部视图
refreshableList.addHeaderView(inflate);
//banner开始轮播
banner.start();
//baseadapter适配器
mAdapter = new MAdapter();
//给refreshableList设置适配器
refreshableList.setAdapter(mAdapter);
//请求网络获取数据
getNetData(i);
return view;
}
private void getNetData(int i) {
new MAsyncTask().execute(urlString+i);
}
private class MLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage((String)path,imageView,Utils.getDisplayImageOption());
}
}
private class MAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = View.inflate(getContext(),R.layout.list_item,null);
}
ImageView imageView = view.findViewById(R.id.imageView);
TextView textView = view.findViewById(R.id.textView);
Products.DataBean dataBean = list.get(i);
com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage((String) dataBean.getIMAGEURL(),imageView,Utils.getDisplayImageOption());
textView.setText(dataBean.getTITLE());
return view;
}
}
private class MAsyncTask extends AsyncTask {
@Override
protected Object doInBackground(Object[] objects) {
return Utils.getNetJson((String)objects[0]);
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
Gson gson = new Gson();
Products products = gson.fromJson((String) o, Products.class);
List<Products.DataBean> data = products.getData();
list.addAll(data);
mAdapter.notifyDataSetChanged();
plv.onRefreshComplete();
}
}
}
PullToRefresh添加HeaderView(Banner轮播)
最新推荐文章于 2020-10-22 21:55:52 发布