您好,登錄后才能下訂單哦!
廢話就不多說了,直接上效果圖和代碼
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來區分控件
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。