MainActivity
public class MainActivity extends AppCompatActivity {
private String path = "http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=15&page=";
private int page = 1;
private XListView xlv;
private HttpUtils httpUtils = HttpUtils.getInstance();;
private List<UserBean.DataBean> list = new ArrayList<>();
private MyBase adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFromByID();
getDataFromNetWork();
}
private void initFromByID() {
xlv = findViewById(R.id.xlv);
xlv.setPullRefreshEnable(true);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
page = 1;
getDataFromNetWork();
}
@Override
public void onLoadMore() {
page += 1;
getDataFromNetWork();
}
});
adapter = new MyBase(list,MainActivity.this);
xlv.setAdapter(adapter);
}
public void getDataFromNetWork(){
String url = path+page;
httpUtils.getdata(url);
//接口回调
httpUtils.setHttpListener(new HttpUtils.HttpListener() {
@Override
public void getsuccers(String json) {
}
@Override
public void getjsondata(String json) {
//开始解析
Gson gson = new Gson();
UserBean userBean = gson.fromJson(json, UserBean.class);
List<UserBean.DataBean> data = userBean.getData();
if(page == 1 ){
list.clear();
}
//添加数据
list.addAll(data);
adapter.notifyDataSetChanged();
if(page ==1){
xlv.stopRefresh();//停止刷新
}else{
xlv.stopLoadMore();//停止加载
}
}
});
}
}
HttpUtils
public class HttpUtils {
private static HttpUtils httpUtils;
private HttpListener httpListener;
public static HttpUtils getInstance(){
if (httpUtils == null){
httpUtils = new HttpUtils();
}
return httpUtils;
}
public void getdata(String url){
Myasynctack myasynctack = new Myasynctack();
myasynctack.execute(url);
}
class Myasynctack extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
String json = "";
try {
URL url = new URL(params[0]);
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
if (urlConnection.getResponseCode() == 200){
InputStream inputStream=urlConnection.getInputStream();
json = StreamToString(inputStream,"utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
private String StreamToString(InputStream inputStream, String s) {
StringBuilder builder =null;
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,s);
BufferedReader reader = new BufferedReader(inputStreamReader);
String string = null;
builder = new StringBuilder();
while ((string = reader.readLine())!=null){
builder.append(string);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
httpListener.getjsondata(s);
}
}
public interface HttpListener{
void getsuccers(String json);
void getjsondata(String s);
}
public void setHttpListener(HttpListener httpListener){
this.httpListener = httpListener;
}
}
MyAppilaction
public class MyAppilaction extends Application {
private Context context;
@Override
public void onCreate() {
super.onCreate();
context = this;
initImageLoader();
}
private void initImageLoader() {
File cacheDir = StorageUtils.getCacheDirectory(context);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions 内存缓存文件的最大长宽
.diskCacheExtraOptions(480, 800, null) // 本地缓存的详细信息(缓存的最大长宽),最好不要设置这个
.threadPoolSize(3) // default 线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - 2) // default 设置当前线程的优先级
.memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值
.memoryCacheSizePercentage(13) // default
.diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值
.diskCacheFileCount(100) // 可以缓存的文件数量
// default为使用HASHCODE对UIL进行加密命名, 还可以用MD5(new Md5FileNameGenerator())加密
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
.writeDebugLogs() // 打印debug log
.build(); //开始构建
ImageLoader.getInstance().init(config);
}
//
public static DisplayImageOptions getOptions(){
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher) // 设置图片下载期间显示的图片
.showImageForEmptyUri(R.mipmap.ic_launcher) // 设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.mipmap.ic_launcher) // 设置图片加载或解码过程中发生错误显示的图片
.resetViewBeforeLoading(false) // default 设置图片在加载前是否重置、复位
.delayBeforeLoading(0) // 下载前的延迟时间
.cacheInMemory(true) // default 设置下载的图片是否缓存在内存中
.cacheOnDisk(true) // default 设置下载的图片是否缓存在SD卡中
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default 设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.ARGB_8888) // default 设置图片的解码类型
.displayer(new RoundedBitmapDisplayer(200)) // default 还可以设置圆角图片new RoundedBitmapDisplayer(20)
.handler(new Handler()) // default
.build();
return options;
}
}
MyBase
public class MyBase extends BaseAdapter{
private List<UserBean.DataBean> list;
private Context context;
private final int ONE_ITEM = 0;
@Override
public int getItemViewType(int position) {
String thumbnail_pic_s = list.get(position).getImg();
if (thumbnail_pic_s !=null){
return ONE_ITEM;
}else {
return ONE_ITEM;
}
}
public MyBase(List<UserBean.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = View.inflate(context,R.layout.mybase,null);
holder = new ViewHolder();
holder.textView = convertView.findViewById(R.id.textView);
holder.imageView = convertView.findViewById(R.id.imageView);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(list.get(position).getTitle());
ImageLoader.getInstance().displayImage(list.get(position).getImg(),holder.imageView,MyAppilaction.getOptions());
return convertView;
}
class ViewHolder{
TextView textView;
ImageView imageView;
}
}