//java代码
package bwei.com.day_0517_123; import android.os.Bundle; import android.support.annotation.NonNull; 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 com.google.gson.Gson; import java.util.ArrayList; import java.util.List; import bwei.com.day_0517_123.adapter.MyBase03; import bwei.com.day_0517_123.bean.Player; import bwei.com.day_0517_123.https.ConHttp; import bwei.com.day_0517_123.utils.HttpUtils; import bwei.com.day_0517_123.view.XListView; public class Fragment03 extends Fragment implements XListView.IXListViewListener{ private static final String TAG = "Fragment03---------"; private XListView f3_xlistview; private List<Player.NewslistBean> list=new ArrayList<>(); private MyBase03 base03; private int page=10; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment03, container, false); //获取id f3_xlistview = view.findViewById(R.id.f3_xlistview); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //创建适配器 base03 = new MyBase03(getActivity(),list); f3_xlistview.setAdapter(base03); //获取网络数据 getMetods(); f3_xlistview.setXListViewListener(this); } public void getMetods() { HttpUtils utils = HttpUtils.getInstance(); String url = ConHttp.YULE_URL + "key=48a7d7193e11bd2dd4a683b6e2f90a4f&num=" + page; Log.d(TAG, "getMetods: "+url); utils.get(url); utils.setHttpUtilsListener(new HttpUtils.HttpUtilListener() { @Override public void getSuccess(String json) { Gson gson = new Gson(); Player player = gson.fromJson(json, Player.class); List<Player.NewslistBean> newslist = player.getNewslist(); if(page==10){ list.clear(); } list.addAll(newslist); //刷新适配器 base03.notifyDataSetChanged(); if(page==10){ f3_xlistview.stopRefresh(); }else{ f3_xlistview.stopLoadMore(); } } @Override public void getError(String error) { } }); } @Override public void onRefresh() { page=10; getMetods(); } @Override public void onLoadMore() { page+=10; getMetods(); } } //适配器
package bwei.com.day_0517_123.adapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; import bwei.com.day_0517_123.MyApp; import bwei.com.day_0517_123.R; import bwei.com.day_0517_123.bean.Player; public class MyBase03 extends BaseAdapter { private Context context; private List<Player.NewslistBean> list; public MyBase03(Context context, List<Player.NewslistBean> 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.mybase03, null); holder = new ViewHolder(); holder.image = (ImageView) convertView.findViewById(R.id.my03_img); holder.text = (TextView) convertView.findViewById(R.id.my03_text); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } String pic = list.get(position).getPicUrl(); ImageLoader.getInstance().displayImage(pic, holder.image, MyApp.getOptions()); holder.text.setText(list.get(position).getTitle()); return convertView; } class ViewHolder { ImageView image; TextView text; } }