废话就不多说了,直接上效果图和代码
fry.Activity01
package fry; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import com.example.viewPager.R; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.LinearLayout; public class Activity01 extends Activity implements OnPageChangeListener,OnClickListener{ private ViewPager viewPager; private List<View> views;//用来存放放进ViewPager里面的ImageView private int[] imageID;// private ImagePagerAdapter adapter;//适配器 private LinearLayout linearLayout; private int currentPoint;//当前被选中中页面的下标 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity01); initData(); initView(); } //初始化view void initView(){ adapter=new ImagePagerAdapter(views); viewPager=(ViewPager) findViewById(R.id.viewPager); viewPager.setAdapter(adapter); linearLayout=(LinearLayout) findViewById(R.id.linearLayout); initPoint(); viewPager.setOnPageChangeListener(this); } //初始化数据 void initData(){ //将图片的id全部放到imageID集合中 imageID=new int[]{R.drawable.guide1,R.drawable.guide2,R.drawable.guide3,R.drawable.guide4}; views=new ArrayList<View>(); //给iamgeView设置LayoutParams参数 LayoutParams layoutParams=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); for(int i:imageID){ //不确定1 ImageView imageView=new ImageView(this); imageView.setImageResource(i); imageView.setLayoutParams(layoutParams); views.add(imageView); } } /* * 初始化页面下方的点 */ private void initPoint(){ ImageView[] imageView=new ImageView[imageID.length]; for(int i=0;i<linearLayout.getChildCount();i++){ imageView[i]=(ImageView) linearLayout.getChildAt(i); imageView[i].setImageResource(R.drawable.point_normal); imageView[i].setOnClickListener(this); imageView[i].setTag(i); } currentPoint=0; imageView[currentPoint].setImageResource(R.drawable.point_select); } /* * 将页面下方的点全部置为未选中状态 */ private ImageView[] clearPoint(){ ImageView[] imageView=new ImageView[imageID.length]; for(int i=0;i<linearLayout.getChildCount();i++){ imageView[i]=(ImageView) linearLayout.getChildAt(i); imageView[i].setImageResource(R.drawable.point_normal); } return imageView; } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } /* * 页面被选中时候执行的方法 * (non-Javadoc) * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int) */ @Override public void onPageSelected(int position) { // TODO Auto-generated method stub ImageView[] imageView= clearPoint(); imageView[position].setImageResource(R.drawable.point_select); } //小圆点点击事件 @Override public void onClick(View v) { // TODO Auto-generated method stub int i=(Integer) v.getTag(); viewPager.setCurrentItem(i); } }
fry.ImagePagerAdapter
package fry; import java.util.ArrayList; import java.util.List; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; public class ImagePagerAdapter extends PagerAdapter{ private List<View> views; public ImagePagerAdapter(List<View> views) { this.views=views; } @Override public int getCount() { // TODO Auto-generated method stub return this.views.size(); } /** * position是在viewPager中显示图片的位置 * 我对应的图片放到对应的位置就好了 * instantiateItem和destroyItem是对应的 * 一个是创建item,一个是销毁item */ @Override public Object instantiateItem(ViewGroup container, int position) { ((ViewPager)container).addView(views.get(position)); return views.get(position); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0==arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager)container).removeView(views.get(position)); } }
/viewPager/res/layout/activity01.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 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="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/linearLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="bottom|center" > <ImageView android:id="@+id/point1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/point_select" android:padding="25dip" /> <ImageView android:id="@+id/point2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/point_normal" android:padding="25dip" /> <ImageView android:id="@+id/point3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/point_normal" android:padding="25dip" /> <ImageView android:id="@+id/point4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/point_normal" android:padding="25dip" /> </LinearLayout> </FrameLayout>
1、多个控件(这里指小圆点)可以用List来一起访问
2、可用动态的控件设置tag来区分控件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。