今天学习了XListView的上拉加载下拉刷新操作,在学习中,发现还是有很多问题的
一、在配置中,要记得更改 你导入的三个文件里的包名以及控件布局;
二、要记得添加权限,并把样式一定改掉。如果运行黑屏的话,在后面加个 .Light
三、工具类
package com.bwie.xlistviewdemo1;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by Administrator on 2017/9/7.
*/
public class NetWorkUtils {
public String getJsonByUrlConnection(String jsonUrl){
String str = "";
try {
URL url = new URL(jsonUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
int resCode = conn.getResponseCode();
if(resCode == 200){
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while((len=is.read(b)) != -1){
str += new String(b,0,len);
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
}
四、主函数
package com.bwie.xlistviewdemo1;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.bwie.adapters.MyBaseAdapter;
import com.bwie.beans.News;
import com.bwie.beans.SuperClass;
import com.google.gson.Gson;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{
private XListView xListView;
private ArrayList<News> newslist;
private MyBaseAdapter myAdapter;
private final static String JSON_URL = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xListView = (XListView) findViewById(R.id.xlv);
xListView.setPullLoadEnable(true);//设置可以刷新和加载
xListView.setXListViewListener(this);
//刚进来,加载第一版数据
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
//线程加载数据
String str = new NetWorkUtils().getJsonByUrlConnection(JSON_URL);
return str;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
SuperClass superClass = new Gson().fromJson(s,SuperClass.class);
newslist = superClass.getNewslist();//得到第一页数据
myAdapter = new MyBaseAdapter(newslist, MainActivity.this);
xListView.setAdapter(myAdapter);
}
}.execute();
}
/**
* 停止刷新或加载时
*/
public void onLoad(){
xListView.stopRefresh();
xListView.stopLoadMore();
xListView.setRefreshTime("刚刚");
}
/**
* 下拉会回调此方法,在次方法中写刷新的代码
*/
@Override
public void onRefresh() {
Toast.makeText(this,"刷新",Toast.LENGTH_SHORT).show();
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
//线程加载数据
String str = new NetWorkUtils().getJsonByUrlConnection(JSON_URL);
return str;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
SuperClass superClass = new Gson().fromJson(s,SuperClass.class);
newslist = superClass.getNewslist();//得到第一页数据
myAdapter = new MyBaseAdapter(newslist, MainActivity.this);
xListView.setAdapter(myAdapter);
onLoad();
}
}.execute();
}
/**
* 上拉到listview的最后一条显示出来,然后会调用此方法,在此方法中加载下一页数据
*/
@Override
public void onLoadMore() {
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
//线程加载数据
String str = new NetWorkUtils().getJsonByUrlConnection(JSON_URL);
return str;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
SuperClass superClass = new Gson().fromJson(s,SuperClass.class);
ArrayList<News> newData = superClass.getNewslist();//得到下一页数据(比如2,3,4.。。)
newslist.addAll(newData);//把新的一页的数据集合,加到总的数据集合
myAdapter.notifyDataSetChanged();//刷新适配器,展示全部数据
onLoad();
}
}.execute();
}
}
五、适配器
package com.bwie.adapters;
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.bwie.beans.News;
import com.bwie.xlistviewdemo1.R;
import java.util.ArrayList;
/**
* Created by Administrator on 2017/9/7.
*/
public class MyBaseAdapter extends BaseAdapter {
private Context context;
private ArrayList<News> news;
public MyBaseAdapter(ArrayList<News> news, Context context) {
this.news = news;
this.context = context;
}
@Override
public int getCount() {
return news.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
//多条目的数量
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return position%2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder1 holder1;
ViewHolder2 holder2;
int rs = getItemViewType(position);//获得当前条目使用的是第几种条目
News newsObj = news.get(position);//得到要展示的数据
if(rs == 0){//第一种条目
if (convertView == null){
holder1 = new ViewHolder1();
convertView = View.inflate(context, R.layout.lsitview_item_1,null);
holder1.textView = (TextView) convertView.findViewById(R.id.mytitle);
convertView.setTag(holder1);
}else{
holder1 = (ViewHolder1) convertView.getTag();
}
//赋值
holder1.textView.setText(newsObj.getTitle());
}else if(rs == 1){//第二种条目
if (convertView == null){
holder2 = new ViewHolder2();
convertView = View.inflate(context, R.layout.listview_item_2,null);
holder2.textView = (TextView) convertView.findViewById(R.id.mytitle);
holder2.imageView = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(holder2);
}else{
holder2 = (ViewHolder2) convertView.getTag();
}
//赋值
holder2.textView.setText(newsObj.getTitle());
//通过imageloader加载图片,显示在次行
// ImageLoader.getInstance().displayImage(newsObj.getPicUrl(),holder2.imageView);
}
return convertView;
}
class ViewHolder1{
TextView textView;
}
class ViewHolder2{
TextView textView;
ImageView imageView;
}
}