再点击事件中打开侧栏drawerLayout.openDrawer(侧栏的视图);
关闭侧栏 dl.closeDrawer(lv);
XListView在OnCreate中配置完所有东西,之后再进行AsyncTask请求数据,刷新适配器.
XListView代码:
public class MainActivity extends Activity implements XListView.IXListViewListener{
ListView lv;
XListView xlv;
List<Zong.ResultBean.RowsBean> arr = new ArrayList<Zong.ResultBean.RowsBean>();
Handler hd;
String s = "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";
MyAdpt ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listview_show);
xlv = (XListView) findViewById(R.id.xlistview);
xlv.setPullLoadEnable(true);
ad = new MyAdpt();
xlv.setAdapter(ad);
xlv.setXListViewListener(this);
hd = new Handler();
Init();
}
private void Init() {
new MyAstncTask().execute(s);
}
class MyAstncTask extends AsyncTask<String,Void,String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Zong zong = gson.fromJson(s, Zong.class);
List<Zong.ResultBean.RowsBean> rows = zong.getResult().getRows();
arr.addAll(rows);
ad.notifyDataSetChanged();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
if(huc.getResponseCode()==200){
InputStream inputStream = huc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String s = "";
while((s = br.readLine())!=null){
sb.append(s);
}
String s1 = sb.toString();
return s1;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
public void onLoad(){
xlv.stopRefresh();
xlv.stopLoadMore();
xlv.setRefreshTime("刚刚");
}
@Override
public void onRefresh() {
hd.postDelayed(new Runnable() {
@Override
public void run() {
arr.clear();
Init();
onLoad();
}
},2000);
}
@Override
public void onLoadMore() {
hd.postDelayed(new Runnable() {
@Override
public void run() {
Init();
onLoad();
}
},2000);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){
this.finish();
}
return false;
}
class MyAdpt extends BaseAdapter{
@Override
public int getCount() {
return arr.size();
}
@Override
public Object getItem(int i) {
return arr.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Adpt ad;
Zong.ResultBean.RowsBean.InfoBean info = arr.get(i).getInfo();
if(view==null){
view = View.inflate(MainActivity.this,R.layout.xlistview_shitu,null);
ad = new Adpt();
ad.img = view.findViewById(R.id.xlistview_img);
ad.tv = view.findViewById(R.id.xlistview_tv);
view.setTag(ad);
}else{
ad = (Adpt) view.getTag();
}
ad.tv.setText(info.getLoupan_name());
ImageLoader.getInstance().displayImage(info.getDefault_image(),ad.img);
return view;
}
class Adpt{
ImageView img;
TextView tv;
}
}
}