xListView的使用:下面是写过的一段代码,加载网络图片适配listview
1:将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;
}
}
xListView的简单使用
猜你喜欢
转载自blog.csdn.net/qq_38619138/article/details/71124858
今日推荐
周排行