ViewPager无限轮播图

时间:2022-05-02 10:12:22

先上效果图:

ViewPager无限轮播图

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.viewpager.MainActivity">

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="230dp" />

<LinearLayout
android:layout_alignBottom="@id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#44000000"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">

<TextView
android:id="@+id/title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="111111111"
android:textColor="#ffffff"
android:textSize="18sp" />

<LinearLayout
android:id="@+id/point_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="5dp"/>
</LinearLayout>
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/normal_point" android:state_enabled="false" />
<item android:drawable="@drawable/active_point" android:state_enabled="true" />
</selector>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#55000000" />
<size
android:width="15dp"
android:height="15dp" />
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#f00" />
<size
android:width="15dp"
android:height="15dp" />
</shape>

package com.example.administrator.viewpager;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "TAG";
private ViewPager view_pager;
private TextView title;
private LinearLayout point;
private String[] titles;
private int[] images;
private int prePosition = 0;
/**
* 是否是拖拽状态
*/
private boolean isDRAGGING = false;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
int item = view_pager.getCurrentItem() + 1;
view_pager.setCurrentItem(item);
mHandler.sendEmptyMessageDelayed(0X110, 3000);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
initEvent();
}

private void initEvent() {
title.setText(titles[prePosition]);
for (int i = 0; i < images.length; i++) {
ImageView iv = new ImageView(MainActivity.this);
iv.setBackgroundResource(R.drawable.point);
point.addView(iv);
if (i == 0) {
iv.setEnabled(true);
} else {
iv.setEnabled(false);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) iv.getLayoutParams();
params.leftMargin = 10;
iv.setLayoutParams(params);
}
}
view_pager.addOnPageChangeListener(new MyOnPageChangeListener());
int item = Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % images.length;
view_pager.setCurrentItem(item);
}

private void initData() {
titles = new String[]{
"床前明月光",
"疑是地上霜",
"举头望明月",
"低头思故乡"
};
images = new int[]{R.drawable.pint01, R.drawable.point02, R.drawable.point03, R.drawable.point04};
}

private void initView() {
view_pager = (ViewPager) findViewById(R.id.view_pager);
title = (TextView) findViewById(R.id.title_tv);
point = (LinearLayout) findViewById(R.id.point_ll);
view_pager.setAdapter(new MyPagerAdapter());
mHandler.sendEmptyMessageDelayed(0X110, 3000);
}

class MyPagerAdapter extends PagerAdapter {

@Override
public int getCount() {
return Integer.MAX_VALUE;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
int realPosition = position % images.length;
ImageView imageView = new ImageView(MainActivity.this);
imageView.setBackgroundResource(images[realPosition]);
imageView.setOnTouchListener(new MyOnTouchListener());
container.addView(imageView);
return imageView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}

class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
int realPosition = position % images.length;
point.getChildAt(prePosition).setEnabled(false);
point.getChildAt(realPosition).setEnabled(true);
title.setText(titles[realPosition]);
prePosition = realPosition;
}

@Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_DRAGGING) {//拖拽状态
isDRAGGING = true;
mHandler.removeCallbacksAndMessages(null);
} else if (state == ViewPager.SCROLL_STATE_SETTLING) {//滚动状态

} else if (state == ViewPager.SCROLL_STATE_IDLE && isDRAGGING) {//空闲状态
mHandler.removeCallbacksAndMessages(null);
mHandler.sendEmptyMessageDelayed(0X110, 3000);
}
}
}

class MyOnTouchListener implements View.OnTouchListener {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG, "onTouch: ACTON_DOWN---------------");
mHandler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_MOVE:
Log.e(TAG, "onTouch: ACTION_MOVE---------------");
break;
case MotionEvent.ACTION_UP:
Log.e(TAG, "onTouch: ACTION_UP---------------");
mHandler.removeCallbacksAndMessages(null);
mHandler.sendEmptyMessageDelayed(0X110, 3000);
break;
}
return true;
}
}
}