ViewPager左右轮滑
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ViewPager左右轮滑相关的知识,希望对你有一定的参考价值。
- package com.example.viewpagerdemo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.support.v4.view.PagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.support.v4.view.ViewPager.OnPageChangeListener;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- public class TwoActivity extends Activity implements OnPageChangeListener{
- /**
- * ViewPager
- */
- private ViewPager viewPager;
- /**
- * 装点点的ImageView数组
- */
- private ImageView[] tips;
- /**
- * 装ImageView数组
- */
- private ImageView[] mImageViews;
- /**
- * 图片资源id
- */
- private int[] imgIdArray ;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ViewGroup group = (ViewGroup)findViewById(R.id.viewGroup);
- viewPager = (ViewPager) findViewById(R.id.viewPager);
- //载入图片资源ID
- imgIdArray = new int[]{R.drawable.item01, R.drawable.item02, R.drawable.item03, R.drawable.item04,
- R.drawable.item05,R.drawable.item06, R.drawable.item07, R.drawable.item08};
- //将点点加入到ViewGroup中
- tips = new ImageView[imgIdArray.length];
- for(int i=0; i<tips.length; i++){
- ImageView imageView = new ImageView(this);
- imageView.setLayoutParams(new LayoutParams(10,10));
- tips[i] = imageView;
- if(i == 0){
- tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
- }else{
- tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
- }
- LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT));
- layoutParams.leftMargin = 5;
- layoutParams.rightMargin = 5;
- group.addView(imageView, layoutParams);
- }
- //将图片装载到数组中
- mImageViews = new ImageView[imgIdArray.length];
- for(int i=0; i<mImageViews.length; i++){
- ImageView imageView = new ImageView(this);
- mImageViews[i] = imageView;
- imageView.setBackgroundResource(imgIdArray[i]);
- }
- //设置Adapter
- viewPager.setAdapter(new MyAdapter());
- //设置监听,主要是设置点点的背景
- viewPager.setOnPageChangeListener(this);
- //设置ViewPager的默认项, 设置为长度的100倍,这样子开始就能往左滑动
- viewPager.setCurrentItem((mImageViews.length) * 100);
- }
- /**
- *
- * @author xiaanming
- *
- */
- public class MyAdapter extends PagerAdapter{
- @Override
- public int getCount() {
- return Integer.MAX_VALUE;
- }
- @Override
- public boolean isViewFromObject(View arg0, Object arg1) {
- return arg0 == arg1;
- }
- @Override
- public void destroyItem(View container, int position, Object object) {
- ((ViewPager)container).removeView(mImageViews[position % mImageViews.length]);
- }
- /**
- * 载入图片进去,用当前的position 除以 图片数组长度取余数是关键
- */
- @Override
- public Object instantiateItem(View container, int position) {
- ((ViewPager)container).addView(mImageViews[position % mImageViews.length], 0);
- return mImageViews[position % mImageViews.length];
- }
- }
- @Override
- public void onPageScrollStateChanged(int arg0) {
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- }
- @Override
- public void onPageSelected(int arg0) {
- setImageBackground(arg0 % mImageViews.length);
- }
- /**
- * 设置选中的tip的背景
- * @param selectItems
- */
- private void setImageBackground(int selectItems){
- for(int i=0; i<tips.length; i++){
- if(i == selectItems){
- tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
- }else{
- tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
- }
- }
- }
- }
- ViewPageAdapter
-
package com.tree.schoollife.adapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import java.util.List;
/**
* Created by tree on 2016/11/10.
*/
public class ViewPagerAdapter extends PagerAdapter {
private List<ImageView> mData;
public ViewPagerAdapter(List<ImageView> mData) {
this.mData = mData;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
//对ViewPager页号求模取出View列表中要显示的项
position %= mData.size();
if (position<0){
position = mData.size()+position;
}
ImageView view = mData.get(position);
//如果View已经在之前添加到了一个父组件,则必须先remove,否则会抛出IllegalStateException。
ViewParent vp =view.getParent();
if (vp!=null){
ViewGroup parent = (ViewGroup)vp;
parent.removeView(view);
}
container.addView(view);
//add listeners here if necessary
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//Warning:不要在这里调用removeView
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
}
以上是关于ViewPager左右轮滑的主要内容,如果未能解决你的问题,请参考以下文章
在 ViewPager 内设计一个 HorizontalScrollView 或使用片段: