package com.example.com.lianxi226;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private PullToRefreshScrollView scc;
//private ListView lv;
private List<String> picUrls;
private ViewPager vp;
private GridView gv;
private DisplayImageOptions options;
private Handler myHandler =new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int position=vp.getCurrentItem();
position++;
//private ListView lv;
private List<String> picUrls;
private ViewPager vp;
private GridView gv;
private DisplayImageOptions options;
private Handler myHandler =new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int position=vp.getCurrentItem();
position++;
vp.setCurrentItem(position);
sendEmptyMessageDelayed(0,1000);
}
};
sendEmptyMessageDelayed(0,1000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scc = findViewById(R.id.scc);
vp = findViewById(R.id.vp);
gv = findViewById(R.id.gv);
//lv = findViewById(R.id.lv);
options=new DisplayImageOptions.Builder()
.cacheInMemory(true)//使用内存缓存
.cacheOnDisk(false)//使用磁盘缓存
.bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式
.imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式
.build();
}
options=new DisplayImageOptions.Builder()
.cacheInMemory(true)//使用内存缓存
.cacheOnDisk(false)//使用磁盘缓存
.bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式
.imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式
.build();
}
{
MyTask myTask = new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson = new Gson();
ReqBean reqBean = gson.fromJson(jsonstr, ReqBean.class);
List<ReqBean.DataBean> data = reqBean.getData();
picUrls = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
String icon = data.get(i).getIcon();
picUrls.add(icon);
}
//设置适配器
vp.setAdapter(new MyVpAdapter());
vp.setCurrentItem(5 * 10);//设置默认显示的页面,使之可以向左滑动
gv.setAdapter(new MyAdapter(data,MainActivity.this));
}
});
myTask.execute("https://www.zhaoapi.cn/ad/getAd");
myHandler.sendEmptyMessageDelayed(0,1000);
}
class MyVpAdapter extends PagerAdapter {
});
myTask.execute("https://www.zhaoapi.cn/ad/getAd");
myHandler.sendEmptyMessageDelayed(0,1000);
}
class MyVpAdapter extends PagerAdapter {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView=new ImageView(MainActivity.this);
//得到图片url路径
String s = picUrls.get(position%picUrls.size());
//异步加载图片
ImageLoader.getInstance().displayImage(s,imageView,options);
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView=new ImageView(MainActivity.this);
//得到图片url路径
String s = picUrls.get(position%picUrls.size());
//异步加载图片
ImageLoader.getInstance().displayImage(s,imageView,options);
//添加到容器中
container.addView(imageView);
return imageView;
}
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
}
}