欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android簡單實現(xiàn)引導(dǎo)頁

 更新時間:2021年04月22日 09:37:58   作者:稚小白  
這篇文章主要為大家詳細介紹了Android簡單實現(xiàn)引導(dǎo)頁,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android簡單實現(xiàn)引導(dǎo)頁的具體代碼,供大家參考,具體內(nèi)容如下

一.思路

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

二.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:id="@+id/viewPager"
   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:id="@+id/image1"
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:layout_margin="10dp"/>
   
   <ImageView
    android:id="@+id/image2"
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:layout_margin="10dp"/>
   
   <ImageView
    android:id="@+id/image3"
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:layout_margin="10dp"/>
   
   <ImageView
    android:id="@+id/image4"
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:layout_margin="10dp"/>
   
  </LinearLayout>

</RelativeLayout>

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

三.實現(xiàn)代碼

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; // 判斷當前對象是否對應(yīng)相應(yīng)的視圖 并返回一個布爾值
  }

  @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類型所以需要轉(zhuǎn)型為View
  }
 }

2.自定義setImageView用來實現(xiàn)下方的紅點顯示

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);
  }
 }

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

3.設(shè)置ViewPager監(jiān)聽

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是為了獲取當前對應(yīng)的頁面并讓下方的小紅點跟隨變化。

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);
  }
 }
}

四.總結(jié)

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Flutter加載圖片流程之ImageCache源碼示例解析

    Flutter加載圖片流程之ImageCache源碼示例解析

    這篇文章主要為大家介紹了Flutter加載圖片流程之ImageCache源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • 詳解如何在Flutter中集成華為認證服務(wù)

    詳解如何在Flutter中集成華為認證服務(wù)

    這篇文章主要介紹了詳解如何在Flutter中集成華為認證服務(wù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • WheelView實現(xiàn)上下滑動選擇器

    WheelView實現(xiàn)上下滑動選擇器

    這篇文章主要為大家詳細介紹了WheelView實現(xiàn)上下滑動選擇器的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android 照片選擇區(qū)域功能實現(xiàn)示例

    Android 照片選擇區(qū)域功能實現(xiàn)示例

    這篇文章主要介紹了Android 照片選擇區(qū)域功能實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Android存儲訪問框架的使用小結(jié)

    Android存儲訪問框架的使用小結(jié)

    這篇文章主要介紹了Android存儲訪問框架的使用,存儲訪問框架API和MediaStore?API的差異,在于存儲訪問框架API,是基于系統(tǒng)文件選擇框的,用戶選擇了文件,那么相當于授權(quán)了,?可以訪問所有類型的文件,需要的朋友可以參考下
    2022-01-01
  • Android應(yīng)用的多語言支持的實現(xiàn)方法

    Android應(yīng)用的多語言支持的實現(xiàn)方法

    本篇文章主要介紹了Android應(yīng)用的多語言支持的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Android fragment實現(xiàn)多個頁面切換效果

    Android fragment實現(xiàn)多個頁面切換效果

    這篇文章主要為大家詳細介紹了fragment實現(xiàn)多個頁面切換效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Android編程實現(xiàn)可滑動的開關(guān)效果(附demo源碼下載)

    Android編程實現(xiàn)可滑動的開關(guān)效果(附demo源碼下載)

    這篇文章主要介紹了Android編程實現(xiàn)可滑動的開關(guān)效果,涉及Android的布局與控件設(shè)置技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下
    2016-04-04
  • Android實現(xiàn)美女拼圖游戲詳解

    Android實現(xiàn)美女拼圖游戲詳解

    這篇文章給大家用實例介紹了利用Android如何實現(xiàn)美女拼圖游戲,對大家開發(fā)拼圖游戲具有一定的參考借鑒價值,感興趣的朋友們一起來看看吧。
    2016-09-09
  • 關(guān)于android連續(xù)點擊出現(xiàn)多個Activity界面的解決方法

    關(guān)于android連續(xù)點擊出現(xiàn)多個Activity界面的解決方法

    這篇文章主要介紹了關(guān)于android連續(xù)點擊出現(xiàn)多個Activity界面的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03

最新評論