xListView的简单使用
来源:互联网 发布:.net b2b商城源码 编辑:程序博客网 时间:2024/05/06 05:33
xListView的使用:下面是写过的一段代码,加载网络图片适配listview1:将xlistview当成本工程的依赖包2:布局文件<com.limxing.xlistview.view.XListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"> </com.limxing.xlistview.view.XListView>3:主界面public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener { private static final String TAG ="MainActivity" ; private XListView lv; private int index=1; private MyListViewAdapter adapter; private boolean b = true; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (XListView) findViewById(R.id.lv); //设置支持上拉加载 lv.setPullLoadEnable(true); //设置支持下拉刷新 lv.setPullRefreshEnable(true); //设置监听事件,实现接口并实现接口里的两个方法 lv.setXListViewListener(this); getData(index); } //请求数据 private void getData(int index) { new AsyncTask<String,Void,String>(){ @Override protected void onPostExecute(String s) { super.onPostExecute(s); dialog.dismiss(); if (s!=null){ Gson gson = new Gson(); Foods foods = gson.fromJson(s, Foods.class); List<Foods.ResultBean.DataBean> list = foods.getResult().getData(); //当适配器为空时,创建适配器并设置 if (adapter == null){ adapter = new MyListViewAdapter(MainActivity.this,list); lv.setAdapter(adapter); }else{ //否则调用适配器里自定义的加载更多的方法 添加更多数据,将读取数据所得到的集合跟自定义的boolean值传过去 adapter.addMore(foods.getResult().getData(),b); //刷新适配器 adapter.notifyDataSetChanged(); } } } @Override protected String doInBackground(String... params) { try { String path = params[0]; URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); if (connection.getResponseCode() == 200){ InputStream is = connection.getInputStream(); String json = StreamToos.readStreamWork(is); return json; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }.execute("http://apis.juhe.cn/cook/query?key=de558ae7e2e2a24fc1f013fbb3327ae1&menu=红烧肉&pn="+index); } //下拉刷新 @Override public void onRefresh() { b=false; //index是页码,表示第几页 ++index; getData(index); lv.stopRefresh(true); } //上拉加载 @Override public void onLoadMore() { b=true; ++index; getData(index); lv.stopLoadMore(); }}4:适配器public class MyListViewAdapter extends BaseAdapter { private Context context; private List<Foods.ResultBean.DataBean> list; public MyListViewAdapter(Context context, List<Foods.ResultBean.DataBean> list) { this.context=context; this.list=list; } //自定义添加更多的方法 public void addMore(List<Foods.ResultBean.DataBean> lists,boolean b){ for (Foods.ResultBean.DataBean dataBeen:lists) { //遍历传过来的集合,当传过来的boolean值为true时,将数据添加到之前的适配器的集合(默认往后面添加) //否则将数据添加到集合的前面 if (b){ list.add(dataBeen); }else{ list.add(0,dataBeen); } } } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView==null){ convertView=View.inflate(context, R.layout.listview_item,null); } ImageView image_1 = (ImageView) convertView.findViewById(R.id.image_1); TextView text_1 = (TextView) convertView.findViewById(R.id.text_1); text_1.setText(list.get(position).getTitle());// 使用默认的ImageLoaderConfiguration ImageLoader.getInstance().displayImage(list.get(position).getAlbums().get(0),image_1); return convertView; }}
0 0
- xListView的简单使用
- XListView的简单使用
- XlistView 的简单使用
- XlistView的简单的使用
- XListView加载刷新数据的简单使用
- XListView简单使用
- Xlistview 简单使用
- XListView的简单Demo
- XListView的简单示例
- XListView的简单用法
- XListView的简单实用
- xlistview的使用
- xListView的使用步骤
- XLIstView的使用
- XlistView的使用
- XListView的使用
- xlistview的使用
- xListView的使用
- 数据结构之单链表的冒泡排序
- Ubuntu 16.04下Apache2+php7以及Nginx+php7迷醉记录
- jQuery对象转成DOM对象
- 爬取广州天气数据和数据可视化
- C++作业5
- xListView的简单使用
- 二十五、UI-Grid 导出数据
- 手机端推送实现原理解析
- CentOS 7下安装Python3.5
- 【腾讯TMQ】开源微服务TARS接口测试揭秘
- 重新开始学习android吧之一些基础(一)
- RPC通信框架——RCF介绍
- PopWindow:基本使用与自定义PopWindow
- SpringMVC以数据绑定方式做HTML、SQL防注入