//不墨迹,直接上
public class PagerAdapter extends android.support.v4.view.PagerAdapter { private List<pagerData> viewlist; private Context context; public PagerAdapter(Context context,List<pagerData> viewlist) { this.viewlist = viewlist; this.context=context; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { } @Override public Object instantiateItem(ViewGroup container, int position) { View view = View.inflate(context, R.layout.tulayout, null); ImageView ggtv =(ImageView) view.findViewById(R.id.imageView); //对ViewPager页号求模取出View列表中要显示的项 position %= viewlist.size(); if (position<0){ position = viewlist.size()+position; } pagerData data = viewlist.get(position); ggtv.setImageResource(data.getData()); container.addView(view); return view; } }
public class Main2Activity extends AppCompatActivity { private ViewPager vp; private LinearLayout ll_dots; private ArrayList<ImageView> dotsList; private List<pagerData> pagerlist; Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==1){ int db = vp.getCurrentItem(); db++; vp.setCurrentItem(db); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); vp=(ViewPager)findViewById(R.id.vp); ll_dots=(LinearLayout)findViewById(R.id.lllllll); pagerlist=new ArrayList<>(); pagerlist.add(new pagerData(R.drawable.aaa)); pagerlist.add(new pagerData(R.drawable.bbb)); pagerlist.add(new pagerData(R.drawable.ccc)); pagerlist.add(new pagerData(R.drawable.ddd)); vp.setAdapter(new PagerAdapter(this,pagerlist)); vp.setCurrentItem(pagerlist.size()*100); initDots(); vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { //遍历存放图片的数组 for (int i = 0; i < pagerlist.size(); i++) { //判断小点点与当前的图片是否对应,对应设置为亮色 ,否则设置为暗色 if (i == position % pagerlist.size()) { dotsList.get(i).setImageDrawable(getResources().getDrawable(R.drawable.yuandiana)); } else { dotsList.get(i).setImageDrawable(getResources().getDrawable(R.drawable.yuandianb)); } } } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } }); // 为了简单,这里选择了休眠 new Thread(){ @Override public void run() { super.run(); while (true){ try { sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } handler.sendEmptyMessage(1); } } }.start(); // 添加点击事件 vp.setOnTouchListener(new View.OnTouchListener() { int touchFlag = 0; float x = 0, y = 0; @Override public boolean onTouch(View view, MotionEvent motionEvent) { ViewConfiguration configuration = ViewConfiguration.get(Main2Activity.this); int mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration); switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: touchFlag = 0; x = motionEvent.getX(); y = motionEvent.getY(); break; case MotionEvent.ACTION_MOVE: float xDiff = Math.abs(motionEvent.getX() - x); float yDiff = Math.abs(motionEvent.getY() - y); if (xDiff < mTouchSlop && xDiff >= yDiff) touchFlag = 0; else touchFlag = -1; break; case MotionEvent.ACTION_UP: if (touchFlag == 0) { int item = vp.getCurrentItem(); int a = pagerlist.size(); for(int i=0;i<a;i++){ int b = item%a; Intent intent=new Intent(Main2Activity.this,MainActivity.class); // int ids = pagerlist.get(b).getData(); // intent.putExtra("id",id); startActivity(intent); break; } } break; } return false; } }); } private void initDots() { //创建存放小点点的集合 dotsList = new ArrayList<ImageView>(); //每次初始化之前清空集合 dotsList.clear(); // 每次初始化之前 移除 布局中的所有小点 ll_dots.removeAllViews(); for (int i = 0; i < pagerlist.size(); i++) { //创建小点点图片 ImageView imageView = new ImageView(this); Drawable drawable = null; if (i == 0) { // 亮色图片 drawable = getResources().getDrawable(R.drawable.yuandiana); } else { drawable = getResources().getDrawable(R.drawable.yuandianb); } imageView.setImageDrawable(drawable); // 考虑屏幕适配 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dip2px(this, 10), dip2px(this, 10)); //设置小点点之间的间距 params.setMargins(dip2px(this, 5), 0, dip2px(this, 5), 0); //将小点点添加大线性布局中 ll_dots.addView(imageView, params); // 将小点的控件添加到集合中 dotsList.add(imageView); } } /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } }