ViewPager的无限轮播,主要以实现为主。代码如下
首先定义activity_viewpager.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="200dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-20dp"
android:orientation="vertical">
<TextView
android:id="@+id/viewpager_head_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="@color/text_black"/>
<LinearLayout
android:id="@+id/points"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" />
</LinearLayout>
</LinearLayout>
point的效果
point_enable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="0.5dp" />
<solid android:color="#AAFFFFFF" />
</shape>
point_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<corners android:radius="0.5dp" />
<solid android:color="#55000000" />
</shape>
point_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/point_enable" android:state_enabled="true"></item>
<item android:drawable="@drawable/point_normal" android:state_enabled="false"></item>
</selector>
在ViewPagerActivity.java去实现具体功能,代码如下所示:
public class ViewPagerActivity extends Activity {
private ViewPager mViewPager;
private LinearLayout mLinearLayout;
private FirstVPageAdapter mFirstVPageAdapter;
private TextView mVPtitle;
private List<ImageView> mImageViewList;
// 圆圈标志位
private int pointIndex = 0;
private int[] listImages {R.mipmap.photo1,R.mipmap.phonto2,R.mipmap.house_photo3,R.mipmap.photo4, R.mipmap.photo5};
// 文字
private String[] listTexts = {"", "", "", ""};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager);
initView();
}
public void initView() {
mLinearLayout = (LinearLayout) findViewById(R.id.points);
mVPtitle = (TextView) findViewById(R.id.viewpager_head_title);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setCurrentItem(0);
// 定时循环
mHandler.postDelayed(mRunnable, 3000);
initData();
initAction();
}
final Handler mHandler = new Handler();
Runnable mRunnable = new Runnable() {
@Override
public void run() {
mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
mHandler.postDelayed(this, 3000);
}
};
View view;
private void initData() {
mImageViewList = new ArrayList<>();
for (int i = 0; i < listImages.length; i++) {
// imageView
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imageView.setBackgroundResource(listImages[i]);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//在这里做点击事件
Intent intent = new Intent();
intent.setClass(ViewPagerActivity.this, Activity.class);
startActivity(intent);
mHandler.removeCallbacks(mRunnable);
}
});
mImageViewList.add(imageView);
}
points();
mFirstVPageAdapter = new FirstVPageAdapter(mImageViewList);
mViewPager.setAdapter(mFirstVPageAdapter);
}
public void points() {
mLinearLayout.removeAllViews();
for (int i = 0; i < 5; i++) {
LayoutParams params;
view = new View(this);
params = new LayoutParams(20, 20);
params.leftMargin = 40;
view.setBackgroundResource(R.drawable.point_background);
view.setLayoutParams(params);
view.setEnabled(false);
mLinearLayout.addView(view);
}
}
/**
* 初始化事件
*/
private void initAction() {
PageListener mPageListener = new PageListener();
//监听事件
mViewPager.setOnPageChangeListener(mPageListener);
//计算启始位置
int index = (Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2 % mImageViewList.size());
//用来触发监听器
mViewPager.setCurrentItem(index);
mLinearLayout.getChildAt(pointIndex).setEnabled(true);
}
class PageListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position) {
int newPosition = position % listImages.length;
// mVPtitle.setText(titles[newPosition]);
mLinearLayout.getChildAt(newPosition).setEnabled(true);
mLinearLayout.getChildAt(pointIndex).setEnabled(false);
// 更新标志位
pointIndex = newPosition;
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mHandler.removeCallbacks(mRunnable);
}
}
接着实现ViewPagerAdapter.java适配器,代码如下所示:
public class FirstVPageAdapter extends PagerAdapter {
private List<ImageView> mList;
public FirstVPageAdapter(List<ImageView> list) {
this.mList = list;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(mList.get(position%mList.size()));
return mList.get(position%mList.size());
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mList.get(position%mList.size()));
}
}
好了,viewpager的无限轮播实现完成了。如有问题在下面评论我,看到就回复。