Android中ViewPager组件的基本用法及实现图片切换的示例
package com.example.showviewpager;
import java.util.ArrayList;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
private static final int VIEW_NUM = 5;
private ViewPager viewPager;
private ImageView[] tagImageViews = new ImageView[VIEW_NUM];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addTagImage();
initViewPager();
}
private void addTagImage() {
LinearLayout layout = (LinearLayout)findViewById(R.id.tagView);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(15, 0, 0, 0);
for (int i = 0; i < VIEW_NUM; i++) {
ImageView tagImageView = new ImageView(this);
tagImageView.setLayoutParams(params);
tagImageViews[i] = tagImageView;
if (i == 0) {
tagImageView.setBackgroundResource(R.drawable.page_current);
} else {
tagImageView.setBackgroundResource(R.drawable.page_not_current);
}
layout.addView(tagImageView);
}
}
private void initViewPager() {
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new MyAdapter());
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < tagImageViews.length; i++) {
if (i == arg0 % VIEW_NUM) {
tagImageViews[i].setBackgroundResource(R.drawable.page_current);
} else {
tagImageViews[i].setBackgroundResource(R.drawable.page_not_current);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
viewPager.setCurrentItem(0);
}
class MyAdapter extends PagerAdapter{
private ArrayList
public MyAdapter(){
viewList = new ArrayList
viewList.add(createPagerImageView(R.drawable.pic1));
viewList.add(createPagerImageView(R.drawable.pic2));
viewList.add(createPagerImageView(R.drawable.pic3));
viewList.add(createPagerImageView(R.drawable.pic4));
viewList.add(createPagerImageView(R.drawable.pic5));
}
private View createPagerImageView(int resid){
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setLayoutParams(params);
layout.setOrientation(LinearLayout.VERTICAL);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setLayoutParams(params);
imageView.setScaleType(ScaleType.CENTER_CROP);
imageView.setImageResource(resid);
layout.addView(imageView);
return layout;
}
@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) {
((ViewPager) container).addView(viewList.get(position % VIEW_NUM),0);
return viewList.get(position % VIEW_NUM);
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView(viewList
.get(position % VIEW_NUM));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播