午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

Android簡單實現引導頁

瀏覽:121日期:2022-09-19 10:08:07

本文實例為大家分享了Android簡單實現引導頁的具體代碼,供大家參考,具體內容如下

一.思路

我們選擇ViewPager + View + ImageView 來實現引導頁效果,ViewPager用來實現滑動,View則是用來顯示每頁的圖像,而ImageView則是用來實現下面的小紅點。

二.XML代碼

<?xml version='1.0' encoding='utf-8'?><RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical'> <android.support.v4.view.ViewPager android: android:layout_width='match_parent' android:layout_height='match_parent' android:background='#000000'/> <LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:gravity='center' android:orientation='horizontal' android:layout_alignParentBottom='true'> <ImageView android: android:layout_width='15dp' android:layout_height='15dp' android:layout_margin='10dp'/> <ImageView android: android:layout_width='15dp' android:layout_height='15dp' android:layout_margin='10dp'/> <ImageView android: android:layout_width='15dp' android:layout_height='15dp' android:layout_margin='10dp'/> <ImageView android: android:layout_width='15dp' android:layout_height='15dp' android:layout_margin='10dp'/> </LinearLayout></RelativeLayout>

有多少個頁面就寫多少個ImageView,這里用相對布局的主要原因是為了能讓小紅點位于父布局的底部。

三.實現代碼

1.自定義ViewPagerAdapter

class ViewPagerAdapter extends PagerAdapter { @Override public int getCount() { return list.size(); // List<View> list = new ArrayList<>(); } @Override public boolean isViewFromObject(View p1, Object p2) { return p1 == p2; // 判斷當前對象是否對應相應的視圖 并返回一個布爾值 } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); // 添加一個View return list.get(position); // 返回一個View } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View)object); // 銷毀一個View 因為是Object類型所以需要轉型為View } }

2.自定義setImageView用來實現下方的紅點顯示

private void setImageView(boolean bool1, boolean bool2, boolean bool3, boolean bool4){ if(bool1){ image1.setBackgroundColor(Color.RED); image2.setBackgroundColor(Color.WHITE); image3.setBackgroundColor(Color.WHITE); image4.setBackgroundColor(Color.WHITE); }else if(bool2){ image2.setBackgroundColor(Color.RED); image1.setBackgroundColor(Color.WHITE); image3.setBackgroundColor(Color.WHITE); image4.setBackgroundColor(Color.WHITE); }else if(bool3){ image3.setBackgroundColor(Color.RED); image2.setBackgroundColor(Color.WHITE); image1.setBackgroundColor(Color.WHITE); image4.setBackgroundColor(Color.WHITE); }else if(bool4){ image4.setBackgroundColor(Color.RED); image2.setBackgroundColor(Color.WHITE); image3.setBackgroundColor(Color.WHITE); image1.setBackgroundColor(Color.WHITE); } }

這里很好理解,有幾個頁面就傳入幾個參數,參數類型都是布爾型,當在第一個頁面是就應該第一個參數是true后面都為false,后面的原理都一樣,然后就是ImageView的顯示,可以直接用兩張圖片來設置,而我沒有圖片就直接用的顏色。

3.設置ViewPager監聽

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){ @Override public void onPageScrolled(int p1, float p2, int p3) { } @Override public void onPageSelected(int p1) { switch(p1){ case 0: setImageView(true,false,false,false); break; case 1: setImageView(false,true,false,false); break; case 2: setImageView(false,false,true,false); break; case 3: setImageView(false,false,false,true); break; } } @Override public void onPageScrollStateChanged(int p1) { } });

在onPageSelected里面寫了一個switch是為了獲取當前對應的頁面并讓下方的小紅點跟隨變化。

4.完整代碼

public class MainActivity extends AppCompatActivity { ViewPager viewPager; List<View> list = new ArrayList<>(); View view1, view2, view3, view4; ImageView image1, image2, image3, image4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView(){ view1 = View.inflate(this, R.layout.view1, null); view2 = View.inflate(this, R.layout.view2, null); view3 = View.inflate(this, R.layout.view3, null); view4 = View.inflate(this, R.layout.view4, null); image1 = findViewById(R.id.image1); image2 = findViewById(R.id.image2); image3 = findViewById(R.id.image3); image4 = findViewById(R.id.image4); viewPager = findViewById(R.id.viewPager); list.add(view1); list.add(view2); list.add(view3); list.add(view4); viewPager.setAdapter(new ViewPagerAdapter()); setImageView(true,false,false,false); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){ @Override public void onPageScrolled(int p1, float p2, int p3) { } @Override public void onPageSelected(int p1) { switch(p1){ case 0: setImageView(true,false,false,false); break; case 1: setImageView(false,true,false,false); break; case 2: setImageView(false,false,true,false); break; case 3: setImageView(false,false,false,true); break; } } @Override public void onPageScrollStateChanged(int p1) { } }); } private void setImageView(boolean bool1, boolean bool2, boolean bool3, boolean bool4){ if(bool1){ image1.setBackgroundColor(Color.RED); image2.setBackgroundColor(Color.WHITE); image3.setBackgroundColor(Color.WHITE); image4.setBackgroundColor(Color.WHITE); }else if(bool2){ image2.setBackgroundColor(Color.RED); image1.setBackgroundColor(Color.WHITE); image3.setBackgroundColor(Color.WHITE); image4.setBackgroundColor(Color.WHITE); }else if(bool3){ image3.setBackgroundColor(Color.RED); image2.setBackgroundColor(Color.WHITE); image1.setBackgroundColor(Color.WHITE); image4.setBackgroundColor(Color.WHITE); }else if(bool4){ image4.setBackgroundColor(Color.RED); image2.setBackgroundColor(Color.WHITE); image3.setBackgroundColor(Color.WHITE); image1.setBackgroundColor(Color.WHITE); } } class ViewPagerAdapter extends PagerAdapter { @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View p1, Object p2) { return p1 == p2; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); return list.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View)object); } }}四.總結

我們使用了ViewPager + View + ImageView簡單的實現了引導頁效果,當然我們也可以使用ViewPager + Fragment + ImageView也可以,這個看個人習慣罷了,引導頁的實現并不難我們只要能熟練掌握ViewPager的使用方法就行。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 国产成人精品av在线观 | 免费视频一二三区 | 亚洲色图校园春色 | 久久av导航 | 久久久亚洲成人 | 日本不卡视频一区二区 | 色呦呦影院 | 91视频免费在线观看 | 激情久久五月天 | 久久影音 | 欧美天堂在线观看 | 黄色1级毛片| 四虎影视在线播放 | 欧美视频三区 | 欧美专区在线观看 | 中文字幕avav | 午夜小视频在线观看 | 久久天堂精品 | 天天操免费视频 | 成人精品999 | 亚洲综合成人网 | 91成人免费网站 | 日韩亚洲一区二区三区 | 最新中文字幕在线视频 | 国产精品九九九九 | 久操伊人 | 国产精品秘 | 精品国产乱码久久久久久久 | 欧美hdse| av大片在线观看 | 久草五月 | 四虎在线观看 | 黄色大片网站 | 欧美 日韩 国产 精品 | 极品少妇av | 国精品人伦一区二区三区蜜桃 | 成人免费视频国产免费麻豆 | 四虎影视最新网址 | 成人免费在线播放视频 | 热久久网站 | 99综合色|