获取类的时候又一个返回值
private final int SUCCESS_CODE=1;
private int status;
public boolean isSuccess(){
return status==SUCCESS_CODE;
}
MainActivity
private ViewPager viewPager;
private TextView textView;
private LinearLayout yuan;
private ViewPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=findViewById(R.id.viewpager);
textView=findViewById(R.id.textView);
yuan=findViewById(R.id.yuan);
adapter = new ViewPagerAdapter(this);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//缓存的点
private int cacheIndex=-1;
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
textView.setText(adapter.getItem(i).getNews_title());
yuan.getChildAt(i%yuan.getChildCount()).setSelected(true);
if(cacheIndex>=0) {
yuan.getChildAt(cacheIndex % yuan.getChildCount()).setSelected(false);
}
cacheIndex=i;
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
loadData();
}
private String dataUrl="http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
private void loadData() {
Util.getInstance().getRequset(dataUrl, UsersBean.class, new Util.CallBack<UsersBean>() {
@Override
public void onSuccess(UsersBean o) {
if(o==null||!o.isSuccess()){
Toast.makeText(MainActivity.this,"错误",Toast.LENGTH_LONG).show();
return;
}
adapter.setDatas(o.getData());
initDot(o.getData().size());
Log.i("TEST",""+o.getData().size());
int center=adapter.getCount()/2;
center = center - center % o.getData().size();
viewPager.setCurrentItem(center);
//开启轮播
startlooper();
}
});
}
@SuppressLint("HandlerLeak")
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
handler.sendEmptyMessageDelayed(0,1000);
}
};
private void startlooper() {
handler.removeMessages(0);
handler.sendEmptyMessageDelayed(0,1000);
}
//初始化小圆点
private void initDot(int size) {
yuan.removeAllViews();
for (int i = 0; i < size; i++) {
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource(R.drawable.selector_dot);
//布局参数: 宽高包裹 左右8dp
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
params.leftMargin = margin;
params.rightMargin = margin;
yuan.addView(imageView, params);
}
}
App
ImageLoader.getInstance().init(
new ImageLoaderConfiguration.Builder(this)
.memoryCacheSizePercentage(10)
.discCacheSize(50 * 1024 * 1024)
.defaultDisplayImageOptions(
new DisplayImageOptions.Builder()
.showImageOnFail(R.mipmap.ic_launcher)
.showImageOnLoading(R.mipmap.ic_launcher)
.cacheInMemory(true)
.cacheOnDisk(true)
.build()
)
.build()
);
ViewPagerAdapter
private List<UsersBean.Data> data;
private Context context;
public ViewPagerAdapter(Context context) {
this.context = context;
data=new ArrayList<>();
}
@Override
public int getCount() {
return data.size()>0?5000:0;
}
public UsersBean.Data getItem(int position){
return data.get(position%data.size());
}
public void setDatas(List<UsersBean.Data> datas) {
data.clear();
if (datas != null) {
data.addAll(datas);
}
notifyDataSetChanged();
}
@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.CENTER_CROP);
container.addView(imageView);
ImageLoader.getInstance().displayImage(getItem(position).getPic_url(),imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_sel_dot" android:state_selected="true" />
<item android:drawable="@drawable/shape_normal_dot" />
</selector>
shape
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="8dp"
android:height="8dp" />
<solid android:color="#909090" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="8dp"
android:height="8dp" />
<solid android:color="#900000" />
</shape>