package com.example.shengxin.scrollerdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; public class MainActivity extends AppCompatActivity { AutoScrollViewPager asvp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); asvp = (AutoScrollViewPager) findViewById(R.id.asvp); asvp.setShouldStop(false); int[] data = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}; asvp.setData(data); } public void click(View v) { int[] data = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}; asvp.setData(data); } @Override protected void onDestroy() { super.onDestroy(); asvp.setShouldStop(true); } @Override protected void onPause() { super.onPause(); asvp.setShouldStop(true); } @Override protected void onRestart() { super.onRestart(); asvp.setShouldStop(false); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="40dp" android:onClick="click" android:text="jiazai" /> <com.example.shengxin.scrollerdemo.AutoScrollViewPager android:id="@+id/asvp" android:layout_width="match_parent" android:layout_height="200dp"></com.example.shengxin.scrollerdemo.AutoScrollViewPager> </RelativeLayout>
package com.example.shengxin.scrollerdemo; import android.content.Context; import android.os.Handler; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; /** * Created by shengxin on 17/11/27. */ public class AutoScrollViewPager extends RelativeLayout implements Runnable { private ViewPager vp; private Handler handler = new Handler(); private int[] data; public AutoScrollViewPager(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.layout_autoscroll, this); //初始化控件 vp = (ViewPager) findViewById(R.id.vp); /*vp.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; } });*/ } public void setData(final int[] data) { this.data = data; if (data == null) { return; } vp.setAdapter(new PagerAdapter() { @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(container.getContext()); imageView.setImageResource(data[position]); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public int getCount() { return data.length; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } }); handler.postDelayed(this, 2000); } private boolean rightDirection; public boolean shouldStop ; public void setShouldStop(boolean shouldStop) { this.shouldStop = shouldStop; if (shouldStop == false) { handler.postDelayed(this, 2000); } } @Override public void run() { if (shouldStop) { return; } int currentItem = vp.getCurrentItem(); if (currentItem == 0) { vp.setCurrentItem(currentItem + 1); rightDirection = true; } else if (currentItem == data.length - 1) { vp.setCurrentItem(currentItem - 1); rightDirection = false; } else if (rightDirection) { vp.setCurrentItem(currentItem + 1); } else { vp.setCurrentItem(currentItem - 1); } handler.postDelayed(this, 2000); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> </RelativeLayout>