010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
|
package com.zh.xiaodiantest; import java.util.ArrayList; import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView; public class MainActivity extends Activity { private ViewPager viewPager;
private ArrayList<View> pageViews;
private ImageView imageView;
private ImageView[] imageViews;
private ViewGroup main;
private ViewGroup group;
@SuppressWarnings ( "unused" ) private TextView content; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); int [] img = new int
[] { R.drawable.no1, R.drawable.no2, R.drawable.no3, R.drawable.no4, R.drawable.no5 }; LayoutInflater inflater = getLayoutInflater(); pageViews = new ArrayList<View>();
for (
int i =
0 ; i < img.length; i++) { LinearLayout layout = new LinearLayout(
this ); LayoutParams ltp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT); final ImageView imageView =
new ImageView(
this ); imageView.setScaleType(ScaleType.CENTER_INSIDE); imageView.setImageResource(img<i>); imageView.setPadding( 15 , 30 , 15 , 30 ); layout.addView(imageView, ltp); pageViews.add(layout); } imageViews = new ImageView[pageViews.size()];
main = (ViewGroup) inflater.inflate(R.layout.pager_layout, null ); group = (ViewGroup) main.findViewById(R.id.viewGroup); viewPager = (ViewPager) main.findViewById(R.id.guidePages); content = (TextView) findViewById(R.id.photo_content); /** * 有几张图片 下面就显示几个小圆点 */ for (
int i =
0 ; i < pageViews.size(); i++) { LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); margin.setMargins( 10 , 0 , 0 , 0 ); imageView = new ImageView(MainActivity.
this ); imageView.setLayoutParams( new LayoutParams(
15 , 15 )); imageViews<i> = imageView; if (i ==
0 ) { imageViews<i> .setBackgroundResource(R.drawable.page_indicator_focused); }
else {
imageViews<i> .setBackgroundResource(R.drawable.page_indicator_unfocused); } group.addView(imageViews<i>, margin); } setContentView(main); viewPager.setAdapter( new GuidePageAdapter());
viewPager.setOnPageChangeListener( new GuidePageChangeListener());
} class GuidePageAdapter
extends PagerAdapter { @Override public int getCount() { return pageViews.size();
} @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1;
} @Override public int getItemPosition(Object object) { return super
.getItemPosition(object); } @Override public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView(pageViews.get(arg1)); } @Override public Object instantiateItem(View arg0,
int arg1) {
((ViewPager) arg0).addView(pageViews.get(arg1)); return pageViews.get(arg1);
} @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { } @Override public Parcelable saveState() {
return null
; } @Override public void startUpdate(View arg0) { } @Override public void finishUpdate(View arg0) { } } class GuidePageChangeListener
implements OnPageChangeListener { @Override public void onPageScrollStateChanged( int arg0) { } @Override public void onPageScrolled( int arg0,
float arg1,
int arg2) { } @Override public void onPageSelected( int arg0) {
for (
int i =
0 ; i < imageViews.length; i++) { imageViews[arg0] .setBackgroundResource(R.drawable.page_indicator_focused); if (arg0 != i) {
imageViews<i> .setBackgroundResource(R.drawable.page_indicator_unfocused); } } } } } </i></i></i></i></i></i> |