直接上代码,代码里边都有注释。
自定义ViewPager,自定义主要是为了处理用户手指按下停止轮播,抬起继续轮播事件 。
package com.example.administrator.ximalayademo.view;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
* 自定义ViewPage
*/
public class BannerViewPage extends ViewPager {
private OnBannerViewPageTouchListener mTouchListener;
public BannerViewPage(Context context) {
super(context);
}
public BannerViewPage(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 判断触摸事件
*
* @param ev
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: //手指按下
setIsTouch(true);
break;
case MotionEvent.ACTION_UP: //手指抬起
setIsTouch(false);
break;
}
return super.onTouchEvent(ev);
}
public void setOnBannerViewPageTouchListener(OnBannerViewPageTouchListener listener) {
mTouchListener = listener;
}
public interface OnBannerViewPageTouchListener {
void onBannerPageTouch(boolean isTouch);
}
private void setIsTouch(boolean isTouch) {
if (mTouchListener != null) {
mTouchListener.onBannerPageTouch(isTouch);
}
}
}
adapter:
package com.example.administrator.ximalayademo.adapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.List;
/**
* Created by Administrator on 2019/4/12.
*/
public class LooperPageAdapter extends PagerAdapter {
private List<Integer> mImage = null;
@Override
public int getCount() {
if (mImage != null) {
return Integer.MAX_VALUE;
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
int rePosition = position % mImage.size();
//创建ImageView并且设置ImageView的背景图
ImageView imageView = new ImageView(container.getContext());
imageView.setImageResource(mImage.get(rePosition));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//将设置好的ImageView设置到容器中
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
/**
* 设置数据
*
* @param image
*/
public void setData(List<Integer> image) {
mImage = image;
}
/**
* 获取数据大小,此方法是用来设置用户往左边滑动时的操作
*
* @return
*/
public int getDataSize() {
if (mImage != null) {
return mImage.size();
}
return 0;
}
}
普通shape 文件(指示点,白色):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="30px"
android:height="30px" />
<solid android:color="#FFFFFF" />
</shape>
当前shape文件(指示点,橙色):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size
android:width="30px"
android:height="30px"/>
<solid android:color="#EE3B3B"/>
</shape>
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.ximalayademo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LooperPageActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
布局文件:
<?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">
<!--重写了onTouchEvent的ViewPage-->
<com.example.administrator.ximalayademo.view.BannerViewPage
android:id="@+id/looper_page"
android:layout_width="match_parent"
android:layout_height="200dp"
android:overScrollMode="never" />
<!--用户放置指示点的容器-->
<LinearLayout
android:id="@+id/linearLayout_point"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/looper_page"
android:layout_marginTop="-20dp"
android:gravity="center"
android:orientation="horizontal" />
</RelativeLayout>
Activity:
package com.example.administrator.ximalayademo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import com.example.administrator.ximalayademo.adapter.LooperPageAdapter;
import com.example.administrator.ximalayademo.view.BannerViewPage;
import java.util.ArrayList;
import java.util.List;
/**
* 轮播Activity
*/
public class LooperPageActivity extends Activity {
private BannerViewPage mLooperPage;
private LooperPageAdapter mLooperPageAdapter;
private static final List<Integer> sImage = new ArrayList<>();
private boolean mIsTouch = false;
private LinearLayout linearLayout_point;
//加载图片资源
static {
sImage.add(R.mipmap.banner1);
sImage.add(R.mipmap.banner2);
sImage.add(R.mipmap.banner3);
}
private Handler mHandler;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_looper_page);
initView();
//创建一个Handler实现自动轮播
mHandler = new Handler();
}
/**
* 当界面绑定到窗口
*/
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
mHandler.post(mRunnableTask);
}
/**
* 当界面从窗口解绑
*/
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
mHandler.removeCallbacks(mRunnableTask);
}
/**
* 自动轮播线程
* mIsTouch:如果 = false,表示用户手指没触摸按下,则轮播,如果 = true 表示用户手指触摸按下,停止轮播
*/
private Runnable mRunnableTask = new Runnable() {
@Override
public void run() {
if (!mIsTouch) {
int currentItem = mLooperPage.getCurrentItem();
mLooperPage.setCurrentItem(++currentItem, true);
}
mHandler.postDelayed(this, 3000);
}
};
/**
* 初始化控件
*/
private void initView() {
mLooperPage = (BannerViewPage) findViewById(R.id.looper_page);
mLooperPageAdapter = new LooperPageAdapter();
mLooperPageAdapter.setData(sImage);
mLooperPage.setAdapter(mLooperPageAdapter);
//设置初始位置,最好不要是第一个,如果是第一个将不能向前滑动。
mLooperPage.setCurrentItem(mLooperPageAdapter.getDataSize() * 100, false);
//设置自定义接口
mLooperPage.setOnBannerViewPageTouchListener(OnTouchListener);
mLooperPage.addOnPageChangeListener(OnPageChangeListener);
//获取页面的容器控件
linearLayout_point = (LinearLayout) findViewById(R.id.linearLayout_point);
insertPoint();
}
/**
* 根据图片的数量动态创建View的数量,然后AddView到页面的容器控件(linearLayout_point)
*/
private void insertPoint() {
for (int i = 0; i < sImage.size(); i++) {
View point = new View(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
point.setBackground(getResources().getDrawable(R.drawable.shape_point_normal));
params.leftMargin = 20;
point.setLayoutParams(params);
linearLayout_point.addView(point);
}
}
/**
* 捕获到当前显示轮播图位置
*/
private ViewPager.OnPageChangeListener OnPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
int rePosition;
if (mLooperPageAdapter.getDataSize() != 0) {
rePosition = position % mLooperPageAdapter.getDataSize();
} else {
rePosition = 0;
}
setPosition(rePosition);
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
private void setPosition(int position) {
for (int i = 0; i < linearLayout_point.getChildCount(); i++) {
View point = linearLayout_point.getChildAt(i);
if (i != position) {
//普通颜色的点
point.setBackground(getResources().getDrawable(R.drawable.shape_point_normal));
} else {
//选中颜色的点
point.setBackground(getResources().getDrawable(R.drawable.shape_point_selected));
}
}
}
/**
* 实现自定义接口
*/
private BannerViewPage.OnBannerViewPageTouchListener OnTouchListener = new BannerViewPage.OnBannerViewPageTouchListener() {
@Override
public void onBannerPageTouch(boolean isTouch) {
mIsTouch = isTouch;
}
};
}
Screenrecorder-2019-04-16-10-18-25-549[1].gif
网友评论