您好,登錄后才能下訂單哦!
先上一張效果圖:
說(shuō)到ViewPager實(shí)現(xiàn)輪播圖效果,那么肯定會(huì)用到PagerAdapter,下面先介紹下這個(gè)類。
PagerAdapter簡(jiǎn)介
PagerAdapter是Android.support.v4包中的類,是一個(gè)抽象類,直接繼承于Object,導(dǎo)入包android.support.v4.view.PagerAdapter即可使用。
PagerAdapter主要是viewpager的適配器,而viewPager則也是在android.support.v4擴(kuò)展包中新添加的一個(gè)強(qiáng)大的控件,可以實(shí)現(xiàn)控件的滑動(dòng)效果,比如咱們?cè)谲浖谐R?jiàn)的廣告欄的滑動(dòng)效果,用viewPager就可以實(shí)現(xiàn)。今天主要介紹如何使用viewPagr并重寫PagerAdapter實(shí)現(xiàn)常見(jiàn)廣告欄的滑動(dòng)效果。
既然是個(gè)抽象類,那么我們新建一個(gè)類去繼承它,重寫四個(gè)方法:
1.public Object instantiateItem(ViewGroup container, int position)
2.public void destroyItem(ViewGroup container, int position, Object object)
3.public int getCount()
4.public boolean isViewFromObject(View arg0, Object arg1)
MyViewPagerAdapter類:
public class MyViewPagerAdapter extends PagerAdapter { private List<ImageView> mList; public MyViewPagerAdapter(List<ImageView> mList){ this.mList=mList; } //當(dāng)要顯示的圖片進(jìn)行緩存時(shí),會(huì)調(diào)用這個(gè)方法進(jìn)行顯示圖片的初始化 //我們將要顯示的ImageView加入到ViewGroup中 public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(mList.get(position)); return mList.get(position); } @Override //PagerAdapter只緩存三張要顯示的圖片,如果滑動(dòng)的圖片超出了緩存的范圍,就會(huì)調(diào)用這個(gè)方法,將圖片銷毀 public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(mList.get(position)); } //獲取要滑動(dòng)的控件的數(shù)量, public int getCount() { // TODO Auto-generated method stub return mList.size(); } //來(lái)判斷顯示的是否是同一張照片,這個(gè)我們將兩個(gè)圖片對(duì)比 再返回 public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0==arg1; } }
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="200dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBottom="@id/vp" android:background="#33000000" android:orientation="vertical" android:paddingBottom="10dp" > <LinearLayout android:id="@+id/ll_points" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" android:paddingBottom="10dp" > </LinearLayout> </LinearLayout> </RelativeLayout>
MainActivity:
public class MainActivity extends Activity { private ViewPager mVp; //private TextView tvTitle; private LinearLayout llPoints; //private String[] titles;// 存放所有要顯示的標(biāo)題 private int[] images;// 存放所有要顯示的圖片資源id private List<ImageView> list=new ArrayList<ImageView>();;// 存放要顯示在ViewPager對(duì)象中的所有Imageview對(duì)象 private int prevPosition = 0; private Handler handler = new Handler() { @SuppressLint("HandlerLeak") public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0: // 得到mvp當(dāng)前頁(yè)面的索引 int currentItem = mVp.getCurrentItem(); // 要顯示的下一個(gè)頁(yè)面的索引 currentItem++; // 設(shè)置ViewPager顯示的頁(yè)面 mVp.setCurrentItem(currentItem % list.size()); break; default: break; } }; }; @SuppressWarnings("deprecation") protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); // titles = getTitles(); images=getImages(); for (int i = 0; i < images.length; i++) { ImageView iv=new ImageView(this); iv.setBackgroundResource(images[i]); list.add(iv); //根據(jù)圖片的數(shù)量生成相對(duì)應(yīng)的數(shù)量的小圓點(diǎn) final View view=new View(this); view.setBackgroundResource(R.drawable.login__05); DisplayMetrics metrics=new DisplayMetrics(); float width=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,30, metrics); float height=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 30, metrics); LinearLayout.LayoutParams params=new LinearLayout.LayoutParams((int)width,(int)height); params.leftMargin=5; view.setLayoutParams(params); llPoints.addView(view); } //設(shè)置第一頁(yè)顯示的標(biāo)題 //tvTitle.setText(titles[0]); //設(shè)置第一頁(yè)的時(shí)候,小圓點(diǎn)顯示的背景圖 llPoints.getChildAt(0).setBackgroundResource(R.drawable.login__03); //下面封裝viewpager的適配器 MyViewPagerAdapter adapter=new MyViewPagerAdapter(list); mVp.setAdapter(adapter); //設(shè)置ViewPager對(duì)象頁(yè)面變化時(shí)的監(jiān)聽(tīng) mVp.setOnPageChangeListener(new OnPageChangeListener() { @Override //當(dāng)下一個(gè)頁(yè)面被選擇的時(shí)候 public void onPageSelected(int arg0) { // TODO Auto-generated method stub //tvTitle.setText(titles[arg0%list.size()]); llPoints.getChildAt(prevPosition).setBackgroundResource(R.drawable.login__05); llPoints.getChildAt(arg0).setBackgroundResource(R.drawable.login__03); //把當(dāng)前點(diǎn)位置做為下一次變化的前一個(gè)點(diǎn)的位置 prevPosition=arg0; } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (true) { SystemClock.sleep(3000); handler.sendEmptyMessage(0); } } }).start(); } private void initView() { // TODO Auto-generated method stub mVp = (ViewPager) findViewById(R.id.vp); //tvTitle = (TextView) findViewById(R.id.tv_title); llPoints = (LinearLayout) findViewById(R.id.ll_points); } private int[] getImages(){ return new int[]{R.drawable.banner_01,R.drawable.banner_02,R.drawable.banner_03}; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。