例题:XListView上拉刷新,下拉加载
导入XListView工具类:{File>ImportModule...>导入工具类地址》》》》导完之后右键需要用到工具类的工程>Open Module Settings>Dependencies>工具类>确定OK}
加载权限:<uses-permission android:name="android.permission.INTERNET"></uses-permission>
布局:<com.bawei.jane.mxlistview.view.XListView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/xlv"/>
=====================================================================
主要代码:
private XListView xlv;
private int pageIndex=1;
private String urlpart="http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=";
private String url=urlpart+pageIndex;
private MyAdapter adapter;
private List<Result.DataBean> data=new ArrayList<>();
//操作类型 1:刷新操作 2:加载更多的操作
private int operType=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查找控件
xlv = findViewById(R.id.xlv);
//对xlistview进行设置
xlv.setPullLoadEnable(true);//上拉加载更多
xlv.setPullRefreshEnable(true);//下拉刷新
//设置接口
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {//刷新
//刷新时,重新请求第1页的数据
pageIndex=1;
url=urlpart+pageIndex;
//重新赋值
operType=1;
MyTask myTask=new MyTask();
myTask.execute(url);
}
@Override
public void onLoadMore() {//加载更多
//页数增加
pageIndex++;
url=urlpart+pageIndex;
//重新赋值
operType=2;//加载更多
Log.d("zzz","loadMore:"+url);
MyTask task=new MyTask();
task.execute(url);
}
});
//进行网络请求
MyTask myTask=new MyTask();
myTask.execute(url);
}
class MyTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
try {
URL url=new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setRequestMethod("GET");
if(urlConnection.getResponseCode()==200){
//获取数据
InputStream inputStream = urlConnection.getInputStream();
String s = StreamToString.streamToStr(inputStream, "utf-8");
Log.d("zzz","---:"+s);
return s;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
Gson gson = new Gson();
Result result = gson.fromJson(s, Result.class);
//data = result.getData();
if (operType == 1) {//代表刷新操作
data.clear();//清空原有的集合数据
}
//增加新的数据集合到原来的集合对象中
data.addAll(result.getData());
setAdapter();
if (operType == 1) {
xlv.stopRefresh();//关闭头布局
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String refreshTime = sf.format(date);
xlv.setRefreshTime(refreshTime);//设置刷新时间
} else {
xlv.stopLoadMore();//关闭尾布局
}
}
}
public void setAdapter(){
if(adapter==null){
adapter = new MyAdapter(data,MainActivity.this);
xlv.setAdapter(adapter);
}else{
adapter.notifyDataSetChanged();
}
}
====================================================================
解析类:Result
public class Result {
json解析类(快捷键ALT+S)GsonFormt
}
======================================================================
适配器类:
class MyAdapter extends BaseAdapter{
private List<Result.DataBean> data;
private Context context;
public MyAdapter(List<Result.DataBean> data, Context context) {
this.data = data;
this.context = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return data.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if(view==null){
view=View.inflate(context,R.layout.item,null);
holder=new ViewHolder();
holder.img=view.findViewById(R.id.img);
holder.textView=view.findViewById(R.id.tv);
view.setTag(holder);
}else{
holder=(ViewHolder) view.getTag();
}
holder.textView.setText(data.get(i).getTitle());
return view;
}
class ViewHolder{
ImageView img;
TextView textView;
}
}
====================================================================
适配器中的布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_height="80dp"
android:layout_width="80dp"
android:id="@+id/img"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tv"
android:layout_toRightOf="@+id/img"/>
</RelativeLayout>
================================================================
工具类:StreamToString
class StreamToString {
public static String streamToStr(InputStream inputStream, String chartSet){
StringBuilder builder=new StringBuilder();
try {
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream,chartSet));
String con;
while ((con=br.readLine())!=null){
builder.append(con);
}
br.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
=====================================================================