ViewPager无限自动轮播
package com.bwie.view; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; public class MainActivity extends Activity { List<View> views=new ArrayList<View>(); private ViewPager vp; private LinearLayout ll_dot; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp = (ViewPager) findViewById(R.id.vp); ll_dot=(LinearLayout)findViewById(R.id.ll_dot); init(); loaddot(); vp.setAdapter(adapter); vp.setCurrentItem(views.size()*100000); sendmesg(); vp.setOnPageChangeListener(listener); } Handler handler=new Handler(){ public void handleMessage(android.os.Message msg) { int item=vp.getCurrentItem(); item++; vp.setCurrentItem(item); sendEmptyMessageDelayed(0, 2000); } }; private void sendmesg() { handler.sendEmptyMessageDelayed(0, 2000); } OnPageChangeListener listener=new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { for (int i = 0; i < imgs.size(); i++) { if(arg0%imgs.size()==i){ imgs.get(i).setImageResource(R.drawable.dot_fouces); }else{ imgs.get(i).setImageResource(R.drawable.dot_normal); } } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }; public void init(){ View v1=View.inflate(this, R.layout.v1, null); View v2=View.inflate(this, R.layout.v2, null); View v3=View.inflate(this, R.layout.v3, null); View v4=View.inflate(this, R.layout.v4, null); View v5=View.inflate(this, R.layout.v5, null); views.add(v1); views.add(v2); views.add(v3); views.add(v4); views.add(v5); } public void loaddot(){ imgs = new ArrayList<ImageView>(); imgs.clear(); for (int i = 0; i < views.size(); i++) { ImageView iView=new ImageView(this); if(i==0){ iView.setImageResource(R.drawable.dot_fouces); }else{ iView.setImageResource(R.drawable.dot_normal); } LayoutParams params=new LayoutParams(20, 20); params.setMargins(5, 0, 5, 0); ll_dot.addView(iView, params); imgs.add(iView); } } PagerAdapter adapter=new PagerAdapter() { @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public Object instantiateItem(ViewGroup container, int position) { View view= views.get(position%views.size()); view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: handler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_UP: handler.sendEmptyMessageDelayed(0, 2000); break; case MotionEvent.ACTION_CANCEL: handler.sendEmptyMessageDelayed(0, 2000); break; default: break; } return true; } }); container.addView(view); return views.get(position%views.size()); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }; private List<ImageView> imgs; }
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="8dp"></corners> <solid android:color="#fff"></solid> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="8dp"></corners> <solid android:color="#88000000"></solid> </shape>
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28