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

Android 使用ViewPager實現(xiàn)輪播圖效果

 更新時間:2017年05月09日 09:23:20   作者:之2  
這篇文章主要介紹了Android 使用ViewPager實現(xiàn)輪播圖效果,通過實例代碼給大家講解了適配器和各個方法的作用介紹,需要的朋友可以參考下

寫這篇文章只是對今天所學的知識進行加深印象,對ViewPager的一些處理,比如適配器和各個方法的作用等。

先看效果圖

這里我是在xml中寫的圓點

Drawable文件夾下的xml代碼:

Shape_yes.xml:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
  <solid android:color="#ff6c67"></solid> 
  <corners android:radius="1000dp"></corners> 
</shape>

Shape_no.xml:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#f0f0f0"></solid> 
  <corners android:radius="1000dp"></corners> 
</shape>

選擇器

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/shape_no" 
  android:state_selected="false" 
  /> 
  <item android:drawable="@drawable/shape_yes" 
   android:state_selected="true" 
    /> 
</selector> 

Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
  <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="160dp"> 
    <android.support.v4.view.ViewPager 
      android:id="@+id/vp" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
    </android.support.v4.view.ViewPager> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:layout_alignParentBottom="true" 
      android:gravity="center_horizontal" 
      android:padding="5dp" 
      android:orientation="vertical" 
      android:background="#66000000"> 
      <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom" 
        android:gravity="center" 
        android:orientation="horizontal" 
        android:paddingBottom="10dp"> 
        <ImageView 
          android:id="@+id/iv_one" 
          android:layout_width="20dp" 
          android:layout_height="20dp" 
          android:layout_marginRight="20dp" 
          android:background="@drawable/selector_show" /> 
        <ImageView 
          android:id="@+id/iv_two" 
          android:layout_width="20dp" 
          android:layout_height="20dp" 
          android:layout_marginRight="20dp" 
          android:background="@drawable/selector_show" /> 
        <ImageView 
          android:id="@+id/iv_three" 
          android:layout_width="20dp" 
          android:layout_height="20dp" 
          android:background="@drawable/selector_show" /> 
      </LinearLayout> 
    </LinearLayout> 
  </RelativeLayout> 
</RelativeLayout> 

寫一個類ViewPagerAdapter_Circleextends PagerAdapter

public class ViewPagerAdapter_Circle extends PagerAdapter { 
  ArrayList<View> arrayList; 
  Context context; 
  public ViewPagerAdapter_Circle(ArrayList<View> arrayList, Context context) { 
    this.arrayList = arrayList; 
    this.context = context; 
  } 
  /***數(shù)據(jù)源的大?。l目)*/ 
  @Override 
  public int getCount() { 
    return arrayList.size(); 
  } 
  /** 
   * 頁面視圖是否關(guān)聯(lián)到特定的對象 
   */ 
  @Override 
  public boolean isViewFromObject(View view, Object object) { 
    return view == object;//判斷當前要顯示的頁面 
  } 
  /** 
   * 初始化頁面 
   * 1.頁面添加到container 
   * 2.將頁面返回 
   */ 
  @Override 
  public Object instantiateItem(ViewGroup container, int position) { 
    container.addView(arrayList.get(position));//頁面添加到container,添加位置 
    return arrayList.get(position); 
  } 
  /** 
   * 銷毀當前頁面 
   */ 
  @Override 
  public void destroyItem(ViewGroup container, int position, Object object) { 
    container.removeView(arrayList.get(position)); 
  } 
} 

MainActivity中代碼(含注解)

public class MainActivity extends AppCompatActivity { 
  ViewPager viewPager; 
  int imageResIds[]; 
  ArrayList<View> imageViewList;//存圖片的集合 
  ArrayList<ImageView> imageViews;//存白點的集合 
  ImageView iv_one,iv_two,iv_three; 
  int oldPostion;//記錄原來的位置 
  private Boolean flag; 
  int count=0; 
  Handler handler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
      super.handleMessage(msg); 
      if (msg.what == 0) { 
        viewPager.setCurrentItem(count); 
      } 
    } 
  }; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //初始化布局 
    initView(); 
    //初始化數(shù)據(jù) 
    initData(); 
    //設(shè)置適配器 
    initAdapter(); 
    //設(shè)置圓點 
    initPoint(); 
    flag = true; 
//循環(huán)設(shè)置播放 
    new Thread() { 
      @Override 
      public void run() { 
        while (flag) { 
          try { 
            Thread.sleep(3000); 
            count++; 
            if (count == 3) { 
              count = 0; 
            } 
          } catch (InterruptedException e) { 
            e.printStackTrace(); 
          } 
          /**傳遞信息*/ 
          Message msg = new Message(); 
          msg.what = 0; 
          msg.obj = count; 
          handler.sendMessage(msg); 
        } 
      } 
    }.start(); 
  } 
  private void initView() { 
    viewPager = (ViewPager) findViewById(R.id.vp); 
  } 
  //初始化要顯示的數(shù)據(jù) 
  private void initData() { 
    //圖片資源數(shù)組 
    imageResIds = new int[]{R.mipmap.a, R.mipmap.b, R.mipmap.c}; 
    imageViewList = new ArrayList<>();//存圖片的集合 
    ImageView imageView; 
    for (int i = 0; i < imageResIds.length; i++) { 
      imageView = new ImageView(this); 
      imageView.setBackgroundResource(imageResIds[i]); 
      imageViewList.add(imageView); 
    } 
  } 
  //設(shè)置適配器 
  private void initAdapter() { 
    ViewPagerAdapter_Circle myViewPagerAdapter = new ViewPagerAdapter_Circle(imageViewList, this); 
    viewPager.setAdapter(myViewPagerAdapter); 
  } 
  /** 
   * 設(shè)置圓點的 
   */ 
  private void initPoint() { 
    iv_one = (ImageView) findViewById(R.id.iv_one); 
    iv_two = (ImageView) findViewById(R.id.iv_two); 
    iv_three = (ImageView) findViewById(R.id.iv_three); 
    imageViews = new ArrayList<>(); 
    imageViews.add(iv_one); 
    imageViews.add(iv_two); 
    imageViews.add(iv_three); 
    iv_one.setSelected(true);//設(shè)置第一個圓點為true 
    /**viewPager監(jiān)聽事件*/ 
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
      /**滑動時調(diào)用*/ 
      @Override 
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
      } 
      /**停止時調(diào)用*/ 
      @Override 
      public void onPageSelected(int position) { 
        imageViews.get(position).setSelected(true);//滑動后顏色改變 
        imageViews.get(oldPostion).setSelected(false);//變?yōu)槌跏碱伾?
        oldPostion = position;//把滑動后的圓點的position給原來的oldPostion 
      } 
        /**滑動狀態(tài)改變時調(diào)用*/ 
      @Override 
      public void onPageScrollStateChanged(int state) { 
      } 
    }); 
  } 
} 

以上所述是小編給大家介紹的Android 使用ViewPager實現(xiàn)輪播圖效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Android編程入門之HelloWorld項目目錄結(jié)構(gòu)分析

    Android編程入門之HelloWorld項目目錄結(jié)構(gòu)分析

    這篇文章主要介紹了Android編程入門之HelloWorld項目目錄結(jié)構(gòu)分析,較為詳細的分析了Android項目的目錄結(jié)構(gòu)與具體作用,需要的朋友可以參考下
    2015-12-12
  • Android Activity的啟動過程源碼解析

    Android Activity的啟動過程源碼解析

    這篇文章主要介紹了Android Activity的啟動過程源碼解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • App內(nèi)切換語言詳解

    App內(nèi)切換語言詳解

    本篇文章主要介紹了App內(nèi)切換語言的方法。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • Android中的全局變量與局部變量使用小結(jié)

    Android中的全局變量與局部變量使用小結(jié)

    這篇文章主要介紹了Android中的全局變量與局部變量使用小結(jié),全局變量顧名思義就是在整個的類中或者可在多個函數(shù)中調(diào)用的變量,也稱為外部變量,局部變量則是特定過程或函數(shù)中可以訪問的變量,需要的朋友可以參考下
    2015-01-01
  • Android實現(xiàn)錄音聲波圖

    Android實現(xiàn)錄音聲波圖

    這篇文章主要為大家詳細介紹了Android實現(xiàn)錄音聲波圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Android 通過cmake的方式接入opencv的方法步驟

    Android 通過cmake的方式接入opencv的方法步驟

    這篇文章主要介紹了Android 通過cmake的方式接入opencv的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • android實現(xiàn)圖片反轉(zhuǎn)效果

    android實現(xiàn)圖片反轉(zhuǎn)效果

    這篇文章主要介紹了android實現(xiàn)圖片反轉(zhuǎn)效果的方法,需要的朋友可以參考下
    2015-09-09
  • Android開發(fā)中如何模擬輸入

    Android開發(fā)中如何模擬輸入

    這篇文章主要介紹了Android開發(fā)中如何模擬輸入,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Android使用KeyStore對數(shù)據(jù)進行加密的示例代碼

    Android使用KeyStore對數(shù)據(jù)進行加密的示例代碼

    這篇文章主要介紹了Android使用KeyStore對數(shù)據(jù)進行加密的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Android源碼中final關(guān)鍵字的用法及final,finally,finalize的區(qū)別

    Android源碼中final關(guān)鍵字的用法及final,finally,finalize的區(qū)別

    Android的源碼中很多地方對final關(guān)鍵字的用法很是“別出心裁”,之所以這么說是因為我從沒看過是這么使用final關(guān)鍵字的,通過本文給大家分享Android源碼中final關(guān)鍵字的用法及final,finally,finalize的區(qū)別,感興趣的朋友一起學習吧
    2015-12-12

最新評論