前提
写权限
写name属性 android:name=".App"
写依赖 或导xlistview module
NewsBean
public class NewsBean {
/**
* result : [{"followMovie":false,"id":20,"imageUrl":"http://172.17.8.100/images/movie/stills/ws/ws1.jpg","name":"无双","rank":1,"summary":"以代号\u201c画家\u201d(周润发 饰)为首的犯罪团伙,掌握了制造伪钞技术,难辨真伪,并在全球进行交易获取利益,引起警方高度重视。然而\u201c画家\u201d和其他成员的身份一直成谜,警方的破案进度遭受到了前所未有的挑战。在关键时刻,擅长绘画的李问(郭富城 饰)打开了破案的突破口,而\u201c画家\u201d的真实身份却让众人意想不到\u2026\u2026"},{"followMovie":false,"id":19,"imageUrl":"http://172.17.8.100/images/movie/stills/jhen/jhen1.jpg","name":"江湖儿女","rank":2,"summary":"故事起始于2001年的山西大同,模特巧巧(赵涛 饰)与出租车公司老板斌哥(廖凡 饰)是一对恋人,斌哥每天在外面呼朋唤友,巧巧却希望两人能够尽快步入婚姻的殿堂。然而一次斌哥在街头遭到竞争对手的袭击,巧巧为了保护斌哥在街头开枪,被判刑五年。巧巧出狱以后,开始寻找斌哥以便重新开始,然而事情却发生了意想不到的变化。"},{"followMovie":false,"id":16,"imageUrl":"http://172.17.8.100/images/movie/stills/dzd6qmwj/dzd6qmwj1.jpg","name":"碟中谍6:全面瓦解","rank":3,"summary":"有时好意会造成恶果,人反而被自己所造成的结果所困扰。伊桑·亨特(汤姆·克鲁斯 饰)和他的IMF团队(亚历克·鲍德温、西蒙·佩吉、文·瑞姆斯)将在最新的电影《碟中谍6:全面瓦解》中再度回归,他们会与观众们熟悉的盟友(丽贝卡·弗格森、米歇尔·莫娜汉)一起与时间赛跑,应对一次任务中出现的意外。亨利·卡维尔、安吉拉·贝塞特和凡妮莎·柯比也将加入本片的演员阵容,电影制片人克里斯托夫·迈考利将会再度担任导演。"},{"followMovie":false,"id":17,"imageUrl":"http://172.17.8.100/images/movie/stills/ftfb3/ftfb(3)1.jpg","name":"反贪风暴3","rank":4,"summary":"ICAC (廉政公署) 陆志廉(古天乐 饰),JFIU (联合财富情报组) 刘保强(张智霖 饰)分别侦查贪污及洗黑钱案,但苦无线索,这时廉政公署L组 (内部纪律调查组) 程德明(郑嘉颖 饰)收到举报,指陆志廉收贿1200万,陆无法辩解实时停职。刘发现陆被诬陷,并跟一直调查的洗黑钱案有着千丝万缕关系,同时怀疑银行主任游子新(栢天男 饰)协助罪恶集团洗黑钱;陆冒险搜集罪证却遭禁锢,命悬一线......."},{"followMovie":false,"id":18,"imageUrl":"http://172.17.8.100/images/movie/stills/hjxd/hjxd1.jpg","name":"黄金兄弟","rank":5,"summary":"狮王(郑伊健 饰)、火山(陈小春 饰)、Bill(谢天华 饰)、淡定(钱嘉乐 饰)、老鼠(林晓峰 饰)五个出生入死的兄弟,在恩师曹sir(曾志伟 饰)的带领下,为了救济儿童而偷取特效药,却惨遭设局,陷入枪林弹雨的险境之中。兄弟们抱着视死如归的豪情,展开一连串的追查与激战。他们明白,即使无法活着回来,也比一人活着痛快!"},{"followMovie":false,"id":21,"imageUrl":"http://172.17.8.100/images/movie/stills/zdn/zdn1.jpg","name":"找到你","rank":6,"summary":"律师李捷(姚晨 饰)正在离婚进行时,与前夫争夺女儿抚养权,拼命工作为给孩子最好的生活,幸有保姆孙芳(马伊琍 饰)帮忙照顾孩子视如己出。一日下班,李捷发现保姆孙芳和女儿毫无预兆地消失了,她内心最大的恐惧变成了现实。在追寻孙芳和女儿的下落时,她收到来自家人的谴责声讨,甚至遭到警方的怀疑。几乎崩溃的李捷,靠着惊人的勇气,踏上独自寻访的旅程。在追踪过程中,李捷逐渐接近了另一个女人\u2014\u2014保姆孙芳的人生故事,她的身份原先都是谎言,而真相也将浮出水面\u2026\u2026"}]
* message : 查询成功
* status : 0000
*/
private String message;
private String status;
private List result;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List getResult() {
return result;
}
public void setResult(List result) {
this.result = result;
}
public static class ResultBean {
/**
* followMovie : false
* id : 20
* imageUrl : http://172.17.8.100/images/movie/stills/ws/ws1.jpg
* name : 无双
* rank : 1
* summary : 以代号“画家”(周润发 饰)为首的犯罪团伙,掌握了制造伪钞技术,难辨真伪,并在全球进行交易获取利益,引起警方高度重视。然而“画家”和其他成员的身份一直成谜,警方的破案进度遭受到了前所未有的挑战。在关键时刻,擅长绘画的李问(郭富城 饰)打开了破案的突破口,而“画家”的真实身份却让众人意想不到……
*/
private boolean followMovie;
private int id;
private String imageUrl;
private String name;
private int rank;
private String summary;
public boolean isFollowMovie() {
return followMovie;
}
public void setFollowMovie(boolean followMovie) {
this.followMovie = followMovie;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
}
}
PagerBean
public class PagerBean {
/**
* msg :
* code : 0
* data : [{"aid":1,"createtime":"2017-12-26T21:49:44","icon":"https://www.zhaoapi.cn/images/quarter/ad1.png","productId":null,"title":"第十三界瑞丽模特大赛","type":0,"url":"http://m.mv14449315.icoc.bz/index.jsp"},{"aid":2,"createtime":"2017-12-26T21:49:44","icon":"https://www.zhaoapi.cn/images/quarter/ad2.png","productId":null,"title":"文化艺术节","type":0,"url":"http://m.mv14449315.icoc.bz/index.jsp"},{"aid":3,"createtime":"2017-12-26T21:49:44","icon":"https://www.zhaoapi.cn/images/quarter/ad3.png","productId":null,"title":"直播封面标准","type":0,"url":"http://m.mv14449315.icoc.bz/index.jsp"},{"aid":4,"createtime":"2017-12-26T21:49:44","icon":"https://www.zhaoapi.cn/images/quarter/ad4.png","productId":"1","title":"人气谁最高,金主谁最豪气","type":1,"url":""}]
*/
private String msg;
private String code;
private List data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
public static class DataBean {
/**
* aid : 1
* createtime : 2017-12-26T21:49:44
* icon : https://www.zhaoapi.cn/images/quarter/ad1.png
* productId : null
* title : 第十三界瑞丽模特大赛
* type : 0
* url : http://m.mv14449315.icoc.bz/index.jsp
*/
private int aid;
private String createtime;
private String icon;
private Object productId;
private String title;
private int type;
private String url;
public String getIconNoHttps() {
return icon.replace("https", "http");
}
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public Object getProductId() {
return productId;
}
public void setProductId(Object productId) {
this.productId = productId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
}
NetUtil
public class NetUtil {
private static NetUtil netUtil;
public NetUtil(){
}
public static NetUtil getIntance(){
if(netUtil==null){
netUtil=new NetUtil();
}
return netUtil;
}
public interface CallBack{
void onsuccess(T t);
}
@SuppressLint("StaticFieldLeak")
public void requestData(String strurl, final Class clazz, final CallBack callback){
new AsyncTask() {
@Override
protected Object doInBackground(String... strings) {
return requestData(strings[0],clazz);
}
@Override
protected void onPostExecute(Object o) {
callback.onsuccess(o);
}
}.execute(strurl);
}
public T requestData(String str,Class cals){
return (T) new Gson().fromJson(requestData(str),cals);
}
public String requestData(String str){
try {
URL url = new URL(str);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
int responseCode = urlConnection.getResponseCode();
if(responseCode==200){
String s = streamTostring(urlConnection.getInputStream());
return s;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String streamTostring(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder=new StringBuilder();
for (String tmp=bufferedReader.readLine();tmp!=null;tmp=bufferedReader.readLine()){
builder.append(tmp);
}
return builder.toString();
}
}
App
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoader.getInstance().init(
new ImageLoaderConfiguration.Builder(this)
.diskCacheSize(50*1024*1024)
.memoryCacheSizePercentage(10)
.defaultDisplayImageOptions(new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.showImageOnLoading(R.mipmap.ic_launcher)
.displayer(new RoundedBitmapDisplayer(8))
.build()
)
.build()
);
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private XListView xListView;
private String vString="http://www.zhaoapi.cn/quarter/getAd";
private String xString="http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=10&page=";
private int i=1;
private ContentsAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xListView=findViewById(R.id.xlistview);
adapter = new ContentsAdapter(this);
xListView.setAdapter(adapter);
xListView.setPullLoadEnable(true);
xListView.setPullRefreshEnable(true);
xListView.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
i=1;
load();
}
@Override
public void onLoadMore() {
i++;
load();
}
});
load();
}
public void load(){
if(i==1){
loadDates();
loadDate();
}else{
loadDate();
}
}
public void loadDates(){
NetUtil.getIntance().requestData(vString,PagerBean.class, new NetUtil.CallBack() {
@Override
public void onsuccess(PagerBean pagerBean) {
adapter.setVlist(pagerBean.getData());
}
});
}
public void loadDate(){
NetUtil.getIntance().requestData(xString+i,NewsBean.class, new NetUtil.CallBack() {
@Override
public void onsuccess(NewsBean newsBean) {
if(i==1){
adapter.setXlist(newsBean.getResult());
}else{
adapter.setXlists(newsBean.getResult());
}
xListView.stopRefresh();
xListView.stopLoadMore();
if(newsBean.getResult().size()<10){
xListView.setPullLoadEnable(false);
}
}
});
}
}
ContentsAdapter
public class ContentsAdapter extends BaseAdapter {
private List xlist;
private List vlist;
private Context context;
private final int PAGE_TYPE=0;
private final int XLIST_TYPE=1;
private final int TYPE_COUNT=2;
private android.os.Handler mHandler = new android.os.Handler(){
@Override
public void handleMessage(Message msg) {
pholder.viewPager.setCurrentItem(pholder.viewPager.getCurrentItem() + 1);
mHandler.sendEmptyMessageDelayed(0, 2000);
}
};
private PViewHolder pholder;
public ContentsAdapter(Context context) {
this.context = context;
xlist=new ArrayList<>();
vlist=new ArrayList<>();
}
public void setXlist(List mxlist) {
xlist.clear();
xlist.addAll(mxlist);
notifyDataSetChanged();
}
public void setXlists(List mxlist) {
xlist.addAll(mxlist);
notifyDataSetChanged();
}
public void setVlist(List mvlist) {
vlist.clear();
vlist.addAll(mvlist);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return position==0&&hasVlit()?PAGE_TYPE:XLIST_TYPE;
}
@Override
public int getViewTypeCount() {
return TYPE_COUNT;
}
public boolean hasVlit(){
return vlist.size()>0;
}
@Override
public int getCount() {
return hasVlit()?xlist.size()+1:xlist.size();
}
@Override
public NewsBean.ResultBean getItem(int position) {
return xlist.get(hasVlit()?position-1:position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(getItemViewType(position)==PAGE_TYPE){
if(convertView==null){
pholder =new PViewHolder();
convertView=LayoutInflater.from(context).inflate(R.layout.viewpager_item,parent,false);
pholder.viewPager=convertView.findViewById(R.id.viewpager);
convertView.setTag(pholder);
}else{
pholder = (PViewHolder) convertView.getTag();
}
mHandler.removeCallbacksAndMessages(null);
pholder.viewPager.setAdapter(new BannerPagerAdapter(vlist,context));
mHandler.sendEmptyMessageDelayed(0,2000);
}else{
XViewHolder xholder;
if(convertView==null){
xholder=new XViewHolder();
convertView=LayoutInflater.from(context).inflate(R.layout.xlistview_item,parent,false);
xholder.image=convertView.findViewById(R.id.image);
xholder.intro=convertView.findViewById(R.id.intro);
xholder.name=convertView.findViewById(R.id.name);
convertView.setTag(xholder);
}else{
xholder= (XViewHolder) convertView.getTag();
}
xholder.name.setText(getItem(position).getName());
xholder.intro.setText(getItem(position).getSummary());
ImageLoader.getInstance().displayImage(getItem(position).getImageUrl(),xholder.image);
}
return convertView;
}
class XViewHolder{
TextView name,intro;
ImageView image;
}
class PViewHolder{
ViewPager viewPager;
}
}
BannerPagerAdapter
public class BannerPagerAdapter extends PagerAdapter {
private List plist;
private Context context;
public BannerPagerAdapter(List plist, Context context) {
this.plist = plist;
this.context = context;
}
@Override
public int getCount() {
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(plist.get(position % plist.size()).getIconNoHttps(), imageView, null, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
layout_main.xml
viewpager_item
xlistview_item