package com.example.viewpagertext;
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.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* ViewPager实现画廊效果
*
*/
public class MianActivity extends Activity
{
private static int TOTAL_COUNT = 3;
private LinearLayout viewPagerContainer;
private ViewPager viewPager;
private TextView tvTitle;
private int[] imageIDs;//原来图片
private int[] imageIDsFocused;//选中的中间放入图片的颜色
private ImageView[] mImageViews;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.item1);
tvTitle = (TextView) findViewById(R.id.title);
viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPagerContainer = (LinearLayout) findViewById(R.id.pager_layout);
imageIDs = new int[] { R.drawable.sceneicon1, R.drawable.sceneicon2, R.drawable.sceneicon3, R.drawable.sceneicon4,
R.drawable.sceneicon5, R.drawable.sceneicon6, R.drawable.sceneicon7, R.drawable.sceneicon8, R.drawable.sceneicon9,
R.drawable.sceneicon10, R.drawable.sceneicon11, R.drawable.sceneicon12, R.drawable.sceneicon13, R.drawable.sceneicon14,
R.drawable.sceneicon15, R.drawable.sceneicon16, R.drawable.sceneicon17, R.drawable.sceneicon18
};
imageIDsFocused = new int[] { R.drawable.sceneiconsel1, R.drawable.sceneiconsel2, R.drawable.sceneiconsel3,
R.drawable.sceneiconsel4, R.drawable.sceneiconsel5, R.drawable.sceneiconsel6, R.drawable.sceneiconsel7,
R.drawable.sceneiconsel8, R.drawable.sceneiconsel9, R.drawable.sceneiconsel10, R.drawable.sceneiconsel11,
R.drawable.sceneiconsel12, R.drawable.sceneiconsel13, R.drawable.sceneiconsel14, R.drawable.sceneiconsel15,
R.drawable.sceneiconsel16, R.drawable.sceneiconsel17, R.drawable.sceneiconsel18
};
// 将图片装载到数组中
mImageViews = new ImageView[imageIDs.length];
for (int i = 0; i < mImageViews.length; i++)
{
ImageView imageView = new ImageView(this);
mImageViews[i] = imageView;
imageView.setBackgroundResource(imageIDs[i]);
}
int width = ScreenUtil.getScreenWidth(this);
int left = viewPagerContainer.getLeft();
viewPager.setPageMargin(20);
int pageWidth = (width - viewPager.getPageMargin() * 3 - left * 2) / 3;
LinearLayout.LayoutParams viewPagerPara = new LinearLayout.LayoutParams(pageWidth, pageWidth);
viewPager.setLayoutParams(viewPagerPara);
viewPager.setAdapter(new MyPagerAdapter());
viewPager.setOffscreenPageLimit(TOTAL_COUNT);
viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
viewPagerContainer.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return viewPager.dispatchTouchEvent(event);
}
});
}
class MyPagerAdapter extends PagerAdapter
{
@Override
public int getCount()
{
// return Integer.MAX_VALUE; // 循环滑动
return imageIDs.length;
// return TOTAL_COUNT;
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return (view == object);
}
@Override
public Object instantiateItem(ViewGroup container, int position)
{
System.out.println("pos:" + position);
((ViewPager) container).addView(mImageViews[position % mImageViews.length], 0);
return mImageViews[position % mImageViews.length];
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
((ViewPager) container).removeView((ImageView) object);
}
}
public class MyOnPageChangeListener implements OnPageChangeListener
{
@Override
public void onPageSelected(int position)
{
tvTitle.setText("position = " + position);
setImgBackground(position);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
// 刷新布局
if (viewPagerContainer != null)
{
viewPagerContainer.invalidate();
}
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
}
/**
* 改变背景图片
* @param position
*/
public void setImgBackground(int position)
{
mImageViews[position].setBackgroundResource(imageIDsFocused[position]);
for (int i = 0; i < mImageViews.length; i++)
{
if (i == position)
{
continue;
} else
{
mImageViews[i].setBackgroundResource(imageIDs[i]);
}
}
}
}