package com.example.week3.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.week3.R;
public class Five extends Fragment {
private View view;
private ViewPager viewpager;
private int []imgs=new int[]{R.drawable.a,R.drawable.b,R.drawable.c};
private int index=0;
private Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
index++;
viewpager.setCurrentItem(index);
mHandler.sendEmptyMessageDelayed(0,1000);
}
};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate( R.layout.five, null);
initView(view);
MyAdapter myAdapter = new MyAdapter();
viewpager.setAdapter(myAdapter);
mHandler.sendEmptyMessage(0);
return view;
//初始化数据
}
private void initView(View view) {
viewpager = view.findViewById(R.id.viewpager);
}
class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@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(getActivity());
imageView.setImageResource(imgs[position%imgs.length]);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//super.destroyItem(container, position, object);
container.removeView((View)object);
}
}
}
FlyBanner的使用https://blog.csdn.net/weixin_39738488/article/details/78148421