Xlistview多条目加载
public class Fraglist extends Fragment {
private XListView xListView;
private String str="http://www.xieast.com/api/news/news.php?page=1";
private ArrayList<JsonBean.DataS> data;
private MyAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fraglistview,container,false);
xListView = view.findViewById(R.id.xListview);
xListView.setPullLoadEnable(true);
xListView.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
HttpUtils.httpAsyncTask(str, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
Gson gson=new Gson();
JsonBean bean=gson.fromJson(s,JsonBean.class);
data = bean.getData();
adapter = new MyAdapter(getActivity(),data);
xListView.setAdapter(adapter);
onLoad();
}
});
}
@Override
public void onLoadMore() {
HttpUtils.httpAsyncTask(str, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
Gson gson=new Gson();
JsonBean bean=gson.fromJson(s,JsonBean.class);
ArrayList<JsonBean.DataS> list = bean.getData();
data.addAll(list);
adapter.notifyDataSetChanged();
onLoad();
}
});
}
});
HttpUtils.httpAsyncTask(str, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
Gson gson=new Gson();
JsonBean bean=gson.fromJson(s,JsonBean.class);
data = bean.getData();
adapter = new MyAdapter(getActivity(),data);
xListView.setAdapter(adapter);
}
});
return view;
}
private void onLoad(){
xListView.stopRefresh();
xListView.stopLoadMore();
xListView.setRefreshTime("刚刚刷新了");
}
}
GridView展示
public class Fraggrid extends Fragment {
private String str=“http://www.xieast.com/api/news/news.php?page=1”;
private GridView gridView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fraggridview,container,false);
gridView = view.findViewById(R.id.gridview);
HttpUtils.httpAsyncTask(str, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
Gson gson=new Gson();
JsonBean bean = gson.fromJson(s, JsonBean.class);
ArrayList<JsonBean.DataS> list = bean.getData();
gridView.setAdapter(new MyAdapter(getActivity(),list));
}
});
return view;
}
}
GridAdapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder1 holder1 =null;
if (convertView==null){
convertView= LayoutInflater.from(context).inflate(R.layout.item1,null);
holder1 = new ViewHolder1();
holder1.text1=convertView.findViewById(R.id.text1);
holder1.image1=convertView.findViewById(R.id.image1);
convertView.setTag(holder1);
}else {
holder1= (ViewHolder1) convertView.getTag();
}
JsonBean.DataBean bean=list.get(position);
holder1.text1.setText(bean.getTitle());
ImageLoader.getInstance().displayImage(bean.getThumbnail_pic_s03(),holder1.image1);
return convertView;
}
class ViewHolder1{
TextView text1;
ImageView image1;
}