public class MainActivity extends AppCompatActivity { private static final String urL = "http://365jia.cn/news/api3/365jia/news/headline?page="; private int count = 1; private XListView xListView; private List<News.DataBeanX.DataBean> list; private NewsAdapter adapter ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xListView = findViewById(R.id.xlv_news); list = new ArrayList<>(); adapter = new NewsAdapter(this,list); xListView.setAdapter(adapter); xListView.setPullLoadEnable(true); xListView.setPullRefreshEnable(true); getData(1,false); xListView.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { count=1; getData(count,false); } @Override public void onLoadMore(){ if (count>=2){ Toast.makeText(MainActivity.this,"这是最后一条数据了",Toast.LENGTH_SHORT).show(); xListView.stopLoadMore(); }else { count++; getData(count,true); } } }); } private void getData(int page,final boolean flag) { new AsyncTask<String,Integer,String>(){ @Override protected String doInBackground(String... strings) { return HttpUtils.getFromHttp(strings[0]); }; @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); News news = gson.fromJson(s, News.class); if(news.getData().getData()!= null){ list.clear(); list.addAll(news.getData().getData()); adapter.notifyDataSetChanged(); } } }.execute(urL+count); } }
XlistView
最新推荐文章于 2018-12-07 13:45:11 发布