<com.stx.xhb.xbanner.XBanner
android:id="@+id/banner"
android:layout_below="@+id/button"
android:layout_width="match_parent"
android:layout_height="300dp"></com.stx.xhb.xbanner.XBanner>
<com.example.hasee.myshitong.view.XListView
android:id="@+id/xlistview"
android:layout_below="@+id/banner"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.example.hasee.myshitong.view.XListView>
Base
public class MyBase extends BaseAdapter{
private Context context;
List<UserBean.DataBean> list;
public MyBase(Context context, List<UserBean.DataBean> list) {
this.context = context;
this.list = list;
}
@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) {
ViewHolder holder;
if(view == null){
view = View.inflate(context, R.layout.mybase,null);
holder=new ViewHolder();
holder.textView1 = view.findViewById(R.id.textView1);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.textView1.setText(list.get(i).getTitle());
return view;
}
class ViewHolder{
TextView textView1;
}
private String path = "http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=15&page=";
private int page = 1;
private XListView xListView;
private HttpUtils httpUtils = HttpUtils.getInstance();;
private List<UserBean.DataBean> list = new ArrayList<>();
private MyBase adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFromByID();
//访问数据
getDataFromNetWork();
}
private void initFromByID() {
//获取资源IDUserBean.DataBean
xListView = (XListView) findViewById(R.id.xlistview);
//可以刷新
xListView.setPullRefreshEnable(true);
//可以加载
xListView.setPullLoadEnable(true);
xListView.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {//刷新
page = 1;
Log.i("TAG",page+"刷新");
getDataFromNetWork();
}
@Override
public void onLoadMore() {//加载
page += 1;
Log.i("TAG",page+"");
getDataFromNetWork();
}
});
adapter = new MyBase(list,MainActivity.this);
xListView.setAdapter(adapter);
}
public void getDataFromNetWork() {
//URL
String url = path+page;
Log.i("TAG",url+"");
httpUtils.getdata(url);
//接口回调
httpUtils.setHttpListener(new HttpUtils.HttpListener() {
@Override
public void getjsondata(String json) {
//开始解析
Gson gson = new Gson();
UserBean userBean = gson.fromJson(json, UserBean.class);
List<UserBean.DataBean> data = userBean.getData();
if(page == 1 ){
list.clear();
}
//添加数据
list.addAll(data);
adapter.notifyDataSetChanged();
if(page ==1){
xListView.stopRefresh();//停止刷新
}else{
xListView.stopLoadMore();//停止加载
}
}
});
}