网址https转换http
public String getIconNoHttps() {
return icon.replace("https", "http");
}
MainActivity
package com.example.demo.ui.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.demo.R;
import com.example.demo.model.BannerResponse;
import com.example.demo.model.NewsResponse;
import com.example.demo.ui.adapter.ContentsAdapter;
import com.example.demo.utils.NetUtils;
import me.maxwin.view.XListView;
public class MainActivity extends AppCompatActivity {
private XListView contents;
private ContentsAdapter mAdapter;
private int mPage;
private final int LOAD_COUNT = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPage = 1;
contents = findViewById(R.id.contents);
mAdapter = new ContentsAdapter(this);
contents.setAdapter(mAdapter);
contents.setPullRefreshEnable(true);
contents.setPullLoadEnable(true);
contents.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
mPage = 1;
loadData();
}
@Override
public void onLoadMore() {
mPage++;
loadData();
}
});
loadData();
}
private void loadData() {
if(mPage == 1) {
loadBannerData();
loadNesData();
} else {
loadNesData();
}
}
private void loadBannerData() {
NetUtils.getInstance().getRequest(bannerUrl, BannerResponse.class, new NetUtils.Callback<BannerResponse>() {
@Override
public void onSuccess(BannerResponse o) {
mAdapter.setBanner(o.getData());
}
});
}
private void loadNesData() {
NetUtils.getInstance().getRequest(String.format(newsUrl, LOAD_COUNT, mPage), NewsResponse.class, new NetUtils.Callback<NewsResponse>() {
@Override
public void onSuccess(NewsResponse o) {
if(mPage == 1) {
mAdapter.setDatas(o.getResult());
} else {
mAdapter.addDatas(o.getResult());
}
//停止刷新加载
contents.stopRefresh();
contents.stopLoadMore();
if(o.getResult().size() < LOAD_COUNT) {
contents.setPullLoadEnable(false);
}
}
});
}
//banner url
private String bannerUrl = "http://www.zhaoapi.cn/quarter/getAd";
//news url
private String newsUrl = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=%d&page=%d";
}
viewpageAdapter
package com.example.demo.ui.adapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.demo.model.BannerResponse;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import java.util.List;
public class BannerPagerAdapter extends PagerAdapter {
private List<BannerResponse.DataBean> mDatas;
private Context context;
public BannerPagerAdapter(List<BannerResponse.DataBean> mDatas, Context context) {
this.mDatas = mDatas;
this.context = context;
}
@Override
public int getCount() {
//TODO 无限
//return mDatas.size();
//return Integer.MAX_VALUE;
return 5000;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
container.addView(imageView);
ImageLoader.getInstance().displayImage(mDatas.get(position % mDatas.size()).getIconNoHttps(), imageView, null, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
Log.i("TEST", "start: " + imageUri);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
Log.i("TEST", "failed: " + imageUri);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
Log.i("TEST", "complete: " + imageUri);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
Log.i("TEST", "cancelled: " + imageUri);
}
});
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
listAdapter
package com.example.demo.ui.adapter;
import android.content.Context;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.demo.R;
import com.example.demo.model.BannerResponse;
import com.example.demo.model.NewsResponse;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
public class ContentsAdapter extends BaseAdapter {
//banner数据
private List<BannerResponse.DataBean> mBanner;
private List<NewsResponse.ResultBean> mDatas;
private Context mContext;
public ContentsAdapter(Context context) {
this.mContext = context;
mBanner = new ArrayList<>();
mDatas = new ArrayList<>();
}
//更新banner
public void setBanner(List<BannerResponse.DataBean> banner) {
mBanner.clear();
if (banner != null) {
mBanner.addAll(banner);
}
notifyDataSetChanged();
}
//更新新闻数据
public void setDatas(List<NewsResponse.ResultBean> datas) {
mDatas.clear();
if(datas != null) {
mDatas.addAll(datas);
}
notifyDataSetChanged();
}
//追加新闻数据
public void addDatas(List<NewsResponse.ResultBean> datas) {
if(datas != null) {
mDatas.addAll(datas);
}
notifyDataSetChanged();
}
@Override
public int getCount() {
//return mDatas.size() + 1;
return hasBanner() ? mDatas.size() + 1 : mDatas.size();
}
//是否有banner
private boolean hasBanner() {
return mBanner.size() > 0;
}
@Override
public NewsResponse.ResultBean getItem(int position) {
return mDatas.get(hasBanner() ? position - 1 : position);
}
@Override
public long getItemId(int position) {
return position;
}
private final int ITEM_COUNT = 2;
private final int BANNER_TYPE = 0;
private final int NEWS_TYPE = 1;
@Override
public int getViewTypeCount() {
//banner + news
return ITEM_COUNT;
}
@Override
public int getItemViewType(int position) {
//return super.getItemViewType(position);
return position == 0 && hasBanner() ? BANNER_TYPE : NEWS_TYPE;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position) == BANNER_TYPE) {
BannerViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_banner, parent, false);
viewHolder = new BannerViewHolder(convertView);
} else {
viewHolder = (BannerViewHolder) convertView.getTag();
}
viewHolder.bindData(mContext, mBanner);
} else {
NewsViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_news, parent, false);
viewHolder = new NewsViewHolder(convertView);
} else {
viewHolder = (NewsViewHolder) convertView.getTag();
}
viewHolder.bindData(getItem(position));
}
return convertView;
}
class BannerViewHolder {
ViewPager viewPager;
private android.os.Handler mHandler = new android.os.Handler(){
@Override
public void handleMessage(Message msg) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
mHandler.sendEmptyMessageDelayed(0, 2000);
}
};
public BannerViewHolder(View itemView) {
viewPager = itemView.findViewById(R.id.banner);
itemView.setTag(this);
}
public void bindData(Context context, List<BannerResponse.DataBean> datas) {
//先取消掉切换任务
mHandler.removeCallbacksAndMessages(null);
viewPager.setAdapter(new BannerPagerAdapter(datas, context));
//2147483648 10000000
//让ViewPager位于中间
//左右都伪无限轮播
// Banner LooperViewPager
// 4 + 2
// 0 1 2 3 4 0
viewPager.setCurrentItem(viewPager.getAdapter().getCount() / 2);
//开始轮播
mHandler.sendEmptyMessageDelayed(0, 2000);
}
}
class NewsViewHolder {
ImageView icon;
TextView title;
TextView date;
public NewsViewHolder(View itemView) {
icon = itemView.findViewById(R.id.icon);
title = itemView.findViewById(R.id.title);
date = itemView.findViewById(R.id.date);
itemView.setTag(this);
}
public void bindData(NewsResponse.ResultBean data) {
title.setText(data.getName());
date.setText(data.getSummary());
ImageLoader.getInstance().displayImage(data.getImageUrl(), icon);
}
}
}
imageIoader
package com.example.demo;
import android.app.Application;
import android.graphics.Bitmap;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import java.io.InputStream;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoader.getInstance().init(
new ImageLoaderConfiguration.Builder(this)
.diskCacheSize(50*1024*1024)
.memoryCacheSizePercentage(13)
.defaultDisplayImageOptions(
new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.ARGB_4444)
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.displayer(new RoundedBitmapDisplayer(16))
.build()
)
.build()
);
}
}