Mainactivity
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import com.example.day9_xlistviewcustom.adapter.MyAdapter;
import com.example.day9_xlistviewcustom.bean.MyBean;
import com.example.day9_xlistviewcustom.util.HttpUtil;
import com.example.xlistviewflush.view.XListView;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
private MyAdapter adapter;
private XListView xListView;
private List<MyBean.DataBean> data = new ArrayList<>();
private int i = 1;
private String mUrl = "http://www.xieast.com/api/news/news.php?page=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//上拉下拉 必写
xListView.setPullLoadEnable(true);
xListView.setXListViewListener(this);
adapter = new MyAdapter(this, data);
xListView.setAdapter(adapter);
new MyTask().execute(mUrl + i);
}
private void initView() {
xListView = findViewById(R.id.xListView);
}
//刷新
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new MyTask().execute(mUrl + 1);
data.clear();
close();
}
}, 2000);
}
//加载更多
@Override
public void onLoadMore() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new MyTask().execute(mUrl + (++i));
}
}, 2000);
}
//异步解析
class MyTask extends AsyncTask<String, Void, List<MyBean.DataBean>> {
@Override
protected List<MyBean.DataBean> doInBackground(String... strings) {
String str = null;
try {
str = HttpUtil.getData(strings[0]);
Gson gson = new Gson();
MyBean myBean = gson.fromJson(str, MyBean.class);
return myBean.getData();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<MyBean.DataBean> dataBeans) {
super.onPostExecute(dataBeans);
List<MyBean.DataBean> data = adapter.getData();
dataBeans.addAll(data);
adapter.setData(dataBeans);
close();
}
}
//关闭
private void close() {
xListView.stopLoadMore();
xListView.stopRefresh();
//显示上一次的刷新日期
Date date = new Date();
String dateStr = (String) android.text.format.DateFormat.format("EEEE, MMMM dd日, yyyy kk:mm:ss", date);
xListView.setRefreshTime(dateStr);
}
}
HttpUtil工具类 解析
注意联网权限
android:name="android.permission.INTERNET
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
//工具类
public class HttpUtil {
public static String getData(String mUrl) throws IOException {
//路径
URL url = new URL(mUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
String str = getStr(inputStream);
return str;
}
private static String getStr(InputStream inputStream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String con = "";
StringBuffer sb = new StringBuffer();
while ((con = br.readLine()) != null) {
sb.append(con);
}
return sb.toString();
}
}
bean类
package com.example.day9_xlistviewcustom.bean;
import java.util.List;
public class MyBean {
private String msg;
private int code;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String uniquekey;
private String title;
private String date;
private String category;
private String author_name;
private String url;
private String thumbnail_pic_s;
private String thumbnail_pic_s02;
private String thumbnail_pic_s03;
public String getUniquekey() {
return uniquekey;
}
public void setUniquekey(String uniquekey) {
this.uniquekey = uniquekey;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getAuthor_name() {
return author_name;
}
public void setAuthor_name(String author_name) {
this.author_name = author_name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getThumbnail_pic_s() {
return thumbnail_pic_s;
}
public void setThumbnail_pic_s(String thumbnail_pic_s) {
this.thumbnail_pic_s = thumbnail_pic_s;
}
public String getThumbnail_pic_s02() {
return thumbnail_pic_s02;
}
public void setThumbnail_pic_s02(String thumbnail_pic_s02) {
this.thumbnail_pic_s02 = thumbnail_pic_s02;
}
public String getThumbnail_pic_s03() {
return thumbnail_pic_s03;
}
public void setThumbnail_pic_s03(String thumbnail_pic_s03) {
this.thumbnail_pic_s03 = thumbnail_pic_s03;
}
}
}
多条目 适配器
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.day9_xlistviewcustom.R;
import com.example.day9_xlistviewcustom.bean.MyBean;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<MyBean.DataBean> data;
//两个值不能一样
private final int ITEM_ONE = 0;
private final int ITEM_TWO = 1;
public MyAdapter(Context mContext, List<MyBean.DataBean> data) {
this.mContext = mContext;
this.data = data;
}
public void setData(List<MyBean.DataBean> data) {
this.data = data;
notifyDataSetChanged();
}
public List<MyBean.DataBean> getData() {
return data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
HolderOne holderOne;
HolderTwo holderTwo;
int type = getItemViewType(position);
switch (type) {
case ITEM_ONE:
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_one, null);
holderOne = new HolderOne();
holderOne.Img_ONE = convertView.findViewById(R.id.Img_ONE);
holderOne.Text_ONE = convertView.findViewById(R.id.Text_ONE);
convertView.setTag(holderOne);
} else {
holderOne = (HolderOne) convertView.getTag();
}
holderOne.Text_ONE.setText(data.get(position).getTitle());
Glide.with(mContext).load(data.get(position).getThumbnail_pic_s()).into(holderOne.Img_ONE);
break;
case ITEM_TWO:
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_two, null);
holderTwo = new HolderTwo();
holderTwo.Img_TWO = convertView.findViewById(R.id.Img_TWO);
holderTwo.Text_TWO = convertView.findViewById(R.id.Text_TWO);
convertView.setTag(holderTwo);
} else {
holderTwo = (HolderTwo) convertView.getTag();
}
holderTwo.Text_TWO.setText(data.get(position).getTitle());
Glide.with(mContext).load(data.get(position).getThumbnail_pic_s()).into(holderTwo.Img_TWO);
break;
}
return convertView;
}
@Override
public int getViewTypeCount() {
return 2;
}
//返回类型
@Override
public int getItemViewType(int position) {
if (position % 2 == 0) {
return ITEM_ONE;
} else {
return ITEM_TWO;
}
}
class HolderOne {
TextView Text_ONE;
ImageView Img_ONE;
}
class HolderTwo {
TextView Text_TWO;
ImageView Img_TWO;
}
}
XListView 主布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.xlistviewflush.view.XListView
android:id="@+id/xListView"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.example.xlistviewflush.view.XListView>
</android.support.constraint.ConstraintLayout>
item_one布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/Img_ONE"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/Text_ONE"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
item_two布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/Img_TWO"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true" />
<TextView
android:id="@+id/Text_TWO"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>