您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Android ViewPager小圓點指示器的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
一個很常用的功能,一個ViewPager會自動滾動,并且有一排小圓點黑和白來指示當前的滾動進度
首先寫一個ViewPager的適配器,這里這個適配器為了方便里面的元素全都是ImageView
import android.content.Context; import android.os.Handler; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; import android.view.animation.Interpolator; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Scroller; /** * Created by Administrator on 2016/2/24. */ public class HomeHomeBannerAdapter extends PagerAdapter{ private Context context; private ImageView[] eventImageViews; private String[] eventUrls; int destWidth,destHeight; public HomeHomeBannerAdapter(Context context, String[] eventUrls,int destWidth,int destHeight) { this.context = context; this.eventUrls = eventUrls; this.destHeight = destHeight; this.destWidth = destWidth; initImageViews(); } /** * 初始化viewPager里的幾張圖 */ private void initImageViews(){ if(eventUrls==null)return; eventImageViews = new ImageView[eventUrls.length]; for (int i=0;i<eventUrls.length;i++) { eventImageViews[i] = new ImageView(context); eventImageViews[i].setLayoutParams(new LinearLayout.LayoutParams(destWidth, destHeight)); eventImageViews[i].setPadding(0, 0, 0, 0); eventImageViews[i].setScaleType(ImageView.ScaleType.FIT_XY); JImageUtils.loadImageFromServerByUrl(context,eventImageViews[i],eventUrls[i]); }//顯示圖片 } @Override public void destroyItem(ViewGroup container, int position, Object object) { if (eventImageViews != null && eventImageViews.length > position && position >= 0) container.removeView(eventImageViews[position]); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(eventImageViews[position], 0); return eventImageViews[position]; } @Override public int getCount() {return eventUrls==null?0:eventUrls.length;} @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } //ViewPager監聽器集成到內部類去 static public class EventViewPagerChangeListener implements ViewPager.OnPageChangeListener { LinearLayout llGuideGroup; int oldEventPosition; int currentItem; public EventViewPagerChangeListener(LinearLayout llGuideGroup){ this.llGuideGroup = llGuideGroup; } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} /** * 控制小點的白色還是黑色的顯示 * @param position */ public void onPageSelected(int position) { llGuideGroup.getChildAt(oldEventPosition).setBackgroundResource(R.drawable.dot_normal);//黑色點 llGuideGroup.getChildAt(position).setBackgroundResource(R.drawable.dot_focused);//白色點 oldEventPosition = position; currentItem = position; } @Override public void onPageScrollStateChanged(int state) { } } /** * 控制viewpager自動滑動的定時任務 */ public static class ScrollTask implements Runnable { EventViewPagerChangeListener listener; ViewPager vpEvent; int eventSize; Handler handler; public ScrollTask(EventViewPagerChangeListener listener,final ViewPager vpEvent, int eventSize){ this.listener = listener; this.vpEvent = vpEvent; this.eventSize = eventSize; handler = new Handler(); } public void run() { if(listener==null||vpEvent==null||eventSize==0)return; listener.currentItem = (listener.currentItem + 1) % eventSize; Log.i("Alex","currentItem是"+listener.currentItem); handler.post(new Runnable() { @Override public void run() { vpEvent.setCurrentItem(listener.currentItem); // 通過Handler切換圖片 } }); } } public static class FixedSpeedScroller extends Scroller { private static final int mDuration = 400; private int eventCount; public FixedSpeedScroller(Context context, Interpolator interpolator,int eventCount) { super(context, interpolator); this.eventCount = eventCount; } @Override public void startScroll(int startX, int startY, int dx, int dy, int duration) { if (duration % 100 == 0 && duration > 0) { //"現在是自動劃" if (duration / 100 == eventCount) super.startScroll(startX, startY, dx, dy, 1);//如果是最后一張 else super.startScroll(startX, startY, dx, dy, mDuration); } else { // "現在是手動劃" super.startScroll(startX, startY, dx, dy, 80); } } } }
在上面適配器的內部類有一個監聽器,這個監聽器里有一個成員LinearLayout llGuideGroup,這個線性布局里面裝有幾個小點,下面是這個布局的定義:
<RelativeLayout android:id="@+id/rlEvents" android:layout_width="match_parent" android:layout_height="200dp" android:visibility="gone"> <android.support.v4.view.ViewPager android:id="@+id/vpEvent" android:layout_width="match_parent" android:layout_height="200dp" android:background="@color/black666666" /> <LinearLayout android:id="@+id/llGuideGroup" android:layout_width="match_parent" android:layout_height="10dp" android:layout_alignParentBottom="true" android:layout_marginBottom="10dp" android:layout_centerHorizontal="true" android:gravity="center" android:orientation="horizontal" > </LinearLayout> </RelativeLayout>
設置小圓點的數量并初始化監聽器
/** * 向一個線性布局里添加小圓點,具體的控制邏輯在listener里 * @param llGuideGroup * @param count 要添加多少個小圓點 */ public EventViewPagerChangeListener addViewPagerDots(LinearLayout llGuideGroup,ViewPager vpEvents,int count){ if(llGuideGroup==null||vpEvents==null||count<1)return null; LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(15, 15); lp.leftMargin = 5; lp.rightMargin = 5; for(int i=0;i<count;i++){ ImageView imageView = new ImageView(llGuideGroup.getContext()); imageView.setLayoutParams(lp); imageView.setBackgroundResource(i==0?R.drawable.dot_focused:R.drawable.dot_normal); llGuideGroup.addView(imageView); } //控制小圓點顯示的監聽器 EventViewPagerChangeListener listener = new EventViewPagerChangeListener(llGuideGroup); vpEvents.addOnPageChangeListener(listener); return listener; }
通過多線程實現定時切換頁面
HomeHomeBannerAdapter.EventViewPagerChangeListener listener = producer.addViewPagerDots(holder.llGuideGroup,holder.viewPager,eventUrls.length);//添加用于指示的小圓點 // 當Activity顯示出來后,每3秒鐘切換一次圖片顯示 ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); scheduledExecutorService.scheduleAtFixedRate(new HomeHomeBannerAdapter.ScrollTask(listener,holder.viewPager,eventUrls.length), 3, 3, TimeUnit.SECONDS);
設置自定義滾動器降低viewPager的切換速度
/** * 給ViewPager設置自定義的滾動器,降低默認的滾動速度 * @param vpEvent */ public void setViewPagerScroller(ViewPager vpEvent){ if(vpEvent==null)return; Field mField; Scroller mScroller; try { mField = ViewPager.class.getDeclaredField("mScroller"); mField.setAccessible(true); mScroller = new HomeHomeBannerAdapter.FixedSpeedScroller(vpEvent.getContext(), new AccelerateInterpolator(),vpEvent.getChildCount()); try { mField.set(vpEvent, mScroller); } catch (IllegalAccessException e) { e.printStackTrace(); } } catch (NoSuchFieldException e) { e.printStackTrace(); } }
感謝各位的閱讀!關于“Android ViewPager小圓點指示器的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。