XListView請求網路資料的使用;
阿新 • • 發佈:2019-02-08
匯入xListview
Activity實現 XListView.IXListViewListener
重寫 onRefresh ,onLoad 方法
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener { private Adater adater; private List<Goods.ResultBean.RowsBean> list = new ArrayList<>(); private XListView xListView; private Handler handler; private DrawerLayout drawerLayout; private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); handler = new Handler(); adater= new Adater(); xListView.setAdapter(adater); initdata(); } private void initView() { xListView = (XListView) findViewById(R.id.techan_xListView); drawerLayout = (DrawerLayout) findViewById(R.id.DrawerLayout); iv = (ImageView) findViewById(R.id.image); xListView.setPullLoadEnable(true); //介面回撥一定要配, 容易忘記; xListView.setXListViewListener(this); } private void initdata() { new MyAsyncTaskString().execute("http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0&qtime=20160411091603"); } class Adater extends BaseAdapter { public Adater() { super(); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View containerView, ViewGroup viewGroup) { containerView = View.inflate(MainActivity.this, R.layout.listview, null); ImageView imageView = (ImageView) containerView.findViewById(R.id.imageView); TextView tv = (TextView) containerView.findViewById(R.id.textView); tv.setText(list.get(i).getInfo().getTags()); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { drawerLayout.openDrawer(iv); } }); new MyAsyncTaskImage(imageView).execute(list.get(i).getInfo().getDefault_image()); return containerView; } } class MyAsyncTaskString extends AsyncTask<String, Void, String> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); Goods goods = gson.fromJson(s, Goods.class); List<Goods.ResultBean.RowsBean> tempList = goods.getResult().getRows(); Log.e("wzq", "onPostExecute:tempList "+tempList.size()); list.addAll(tempList); Log.e("wzq", "onPostExecute:list.size() "+list.size()); adater.notifyDataSetChanged(); } @Override protected String doInBackground(String... strings) { return new Utiles().getAsytemString(strings[0]); } @Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); } } class MyAsyncTaskImage extends AsyncTask<String, Void, Bitmap> { private ImageView image; public MyAsyncTaskImage(ImageView image) { this.image = image; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); image.setImageBitmap(bitmap); } @Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); } @Override protected Bitmap doInBackground(String... strings) { return new Utiles().getAsytemImage(strings[0]); } } // 關閉 public void onLoad() { xListView.stopRefresh(); xListView.stopLoadMore(); xListView.setRefreshTime("剛剛"); } //重新整理 @Override public void onRefresh() { list.clear(); initdata(); onLoad(); } //載入更多 @Override public void onLoadMore() { //1--保留之前集合中的資料 //2 把新資料新增到list中,更新介面卡; initdata(); onLoad(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { this.finish(); } return false; } }