最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android首頁無限輪播功能的示例代碼

 更新時間:2018年06月28日 09:28:07   作者:楊子翎  
這篇文章主要介紹了Android首頁無限輪播功能的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

最近工作不是很忙,也跟大神學(xué)習(xí)下總結(jié)一些小的技術(shù)點(diǎn):

對于一個App幾乎都有Banner廣告功能,也就是我們常見的輪播圖,當(dāng)然目前市場第三方框架已經(jīng)非常成熟了,尤其是youth5201314/banner這里有g(shù)ithub地址也可以學(xué)習(xí)下:https://github.com/youth5201314/banner.git

那么下面給大家介紹我的一些總結(jié):

首先分析下輪播圖的設(shè)計

  1. 多張輪播圖定時效果
  2. 指示點(diǎn)以及每張圖片的文字說明
  3. 實現(xiàn)無限輪播,可滑動,圖片點(diǎn)擊事件

開始布局:

<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"
  tools:context="com.yangziling.carousel.MainActivity">
<!--輪播圖-->
  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="200dp">
    <android.support.v4.view.ViewPager
      android:id="@+id/vp"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>
      
    <!--指示點(diǎn)和圖片標(biāo)題-->
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="35dip"
      android:layout_gravity="bottom"
      android:background="#33000000"
      android:gravity="center"
      android:orientation="vertical">
      <!--圖片配文-->
      <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圖片標(biāo)題"
        android:textColor="@android:color/white" />
      <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dip"
        android:orientation="horizontal" >
      <!--指示點(diǎn)-->
        <View
          android:id="@+id/dot_0"
          style="@style/view_attr"
          android:background="@drawable/dot_focused"/>
        <View
          android:id="@+id/dot_1"
          style="@style/view_attr"
          android:background="@drawable/dot_normal"/>
        <View
          android:id="@+id/dot_2"
          style="@style/view_attr"
          android:background="@drawable/dot_normal"/>
        <View
          android:id="@+id/dot_3"
          style="@style/view_attr"
          android:background="@drawable/dot_normal"/>
        <View
          android:id="@+id/dot_4"
          style="@style/view_attr"
          android:background="@drawable/dot_normal"/>
      </LinearLayout>
    </LinearLayout>
  </FrameLayout>
</RelativeLayout>

布局中抽取的view屬性:

<style name="view_attr">
  <item name="android:layout_width">5dp</item>
  <item name="android:layout_height">5dp</item>
  <item name="android:layout_marginLeft">5dp</item>
  <item name="android:layout_marginRight">5dp</item>
</style>

這里我展示的一些圖片是在本地的,通過網(wǎng)絡(luò)框架加載圖片原理也是一樣的。

這里直接給大家貼出來核心代碼:

public class MainActivity extends AppCompatActivity {
  private ViewPager mMyViewPaper;
  private List<ImageView> images;
  private List<View> dots;
  private int currentItem;
  private TextView title;
  private MyAdapter adapter;
  //當(dāng)前顯示圖片的位置
  private int localPosition = 0;
  //圖片的id
  private int[] imageIds =
    new int[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e};
  //圖片的標(biāo)題
  private String[] titles =
    new String[]{"藍(lán)天白云", "青山綠水", "枯藤老樹", "人間仙境", "島嶼大樹"};
  private TimerTask mTimerTask;
  //創(chuàng)建一個定時器
  private final Timer timer = new Timer();
  private ImageView mImageView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mMyViewPaper = (ViewPager) findViewById(R.id.vp);

    //顯示的圖片
    images = new ArrayList<>();
    for (int i = 0; i < imageIds.length; i++) {
      mImageView = new ImageView(this);
      mImageView.setBackgroundResource(imageIds[i]);
      images.add(mImageView);
    }
    //指示點(diǎn)
    dots = new ArrayList<>();
    dots.add(findViewById(R.id.dot_0));
    dots.add(findViewById(R.id.dot_1));
    dots.add(findViewById(R.id.dot_2));
    dots.add(findViewById(R.id.dot_3));
    dots.add(findViewById(R.id.dot_4));

    title = (TextView) findViewById(R.id.title);
    title.setText(titles[0]);

    adapter = new MyAdapter(MainActivity.this, images);
    mMyViewPaper.setAdapter(adapter);
    mMyViewPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

    @Override
    public void onPageSelected(int position) {
      position = position % images.size();
      title.setText(titles[position]);
      dots.get(position).setBackgroundResource(R.drawable.dot_focused);
      dots.get(localPosition).setBackgroundResource(R.drawable.dot_normal);

      localPosition = position;
      currentItem = position;
    }
    /**
     * 頁面滑動時回調(diào)
     */
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }
    /**
     * 當(dāng)ViewPager狀態(tài)改變時,回調(diào)
     */
    @Override
    public void onPageScrollStateChanged(int state) {
      }
    });
  }
    /**
     * 輪播任務(wù)
     */
    @Override
    protected void onStart() {
      super.onStart();
      mTimerTask = new TimerTask() {

    @Override
    public void run() {
      currentItem = (currentItem + 1) % imageIds.length;
      mHandler.sendEmptyMessage(0);
    }
  };
  timer.schedule(mTimerTask, 2000, 2000);
}

/**
 * 接收子線程傳遞的數(shù)據(jù)
 */
private Handler mHandler = new Handler() {
  public void handleMessage(android.os.Message msg) {
    //輪播到最后一張圖片時,直接跳轉(zhuǎn)至第一頁,并且取消滑動效果
    if (currentItem % images.size() == 0) {
      mMyViewPaper.setCurrentItem(currentItem, false);
    }
    //非最后一張展示圖片的滑動效果
    mMyViewPaper.setCurrentItem(currentItem, true);
  }
};

  @Override
  protected void onStop() {
    super.onStop();
    timer.cancel();
    }
}

自定義了一個Adapter適配器:

public class MyAdapter extends PagerAdapter {
  private List<ImageView> images;
  private Context mContext;

  public MyAdapter(Context context,List<ImageView> images) {
    this.mContext =context;
    this.images = images;
  }
  //返回Viewpager中的view個數(shù)
  @Override
  public int getCount() {
    return Integer.MAX_VALUE;
  }

  //判斷instantiateItem中的函數(shù)返回的key與一個頁面示圖是不是代表同一個
  //通常直接相等就OK啦
  @Override
  public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == arg1;
  }
  //移除一個固定位置的頁面
  @Override
  public void destroyItem(ViewGroup view, int position, Object object) {
    view.removeView((View) object);
  }

  //將固定位置的View添加到Viewgroup中,并創(chuàng)建顯示出來
  @Override
  public Object instantiateItem(ViewGroup view, final int position) {
    ImageView imageView = images.get(position % images.size());
    ViewGroup parent = (ViewGroup) imageView.getParent();
    //這里是動態(tài)添加示圖,一個子類只能有一個父類
    //判斷下如果parent存在一定要記得移除
    if (parent != null) {
      parent.removeView(imageView);
    }
    view.addView(imageView);
    //給圖片添加點(diǎn)擊事件
    imageView.setOnClickListener(new View.OnClickListener(){
      @Override
      public void onClick(View v) {
        Toast.makeText(mContext,"點(diǎn)擊了第"+(position % images.size()+1)+"圖片",Toast.LENGTH_SHORT).show();
      }
    });
    return images.get(position % images.size());
  }

  @Override
  public int getItemPosition(Object object) {
    return POSITION_NONE;
  }
}

在這里面也遇到一些問題,做了一些“妥協(xié)”,比如說無限輪播的時候發(fā)現(xiàn)播放到最后一張時會返回到第一張再次輪播,但是會有那種卡頓的感覺。所以我做了個“妥協(xié)的處理”,就是在播放到最后一張時,取消自帶的動畫效果,直接跳轉(zhuǎn)到第一張,然后進(jìn)行輪播。

private Handler mHandler = new Handler() {
    public void handleMessage(android.os.Message msg) {
      //輪播到最后一張圖片時,直接跳轉(zhuǎn)至第一頁,并且取消滑動效果
      if (currentItem % images.size() == 0) {
      mMyViewPaper.setCurrentItem(currentItem, false);
    }
    //非最后一張展示圖片的滑動效果
    mMyViewPaper.setCurrentItem(currentItem, true);
  }
};

項目的github地址:https://github.com/yangziling/Carousel.git

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

相關(guān)文章

  • kotlin Standard中的內(nèi)聯(lián)函數(shù)示例詳解

    kotlin Standard中的內(nèi)聯(lián)函數(shù)示例詳解

    這篇文章主要給大家介紹了關(guān)于kotlin Standard中內(nèi)聯(lián)函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Android?中TextureView和SurfaceView的屬性方法及示例說明

    Android?中TextureView和SurfaceView的屬性方法及示例說明

    這篇文章主要介紹了Android?中TextureView和SurfaceView的屬性方法及示例說明,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-06-06
  • Kotlin中的高階函數(shù)深入講解

    Kotlin中的高階函數(shù)深入講解

    這篇文章主要給大家介紹了關(guān)于Kotlin中高階函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Android開發(fā)之線程通信詳解

    Android開發(fā)之線程通信詳解

    這篇文章主要為大家詳細(xì)介紹了Android開發(fā)中線程間通信的相關(guān)資料,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Android有一定的幫助,?需要的可以了解一下
    2022-11-11
  • Android  調(diào)用系統(tǒng)應(yīng)用的方法總結(jié)

    Android 調(diào)用系統(tǒng)應(yīng)用的方法總結(jié)

    這篇文章主要介紹了Android 調(diào)用系統(tǒng)應(yīng)用的方法總結(jié)的相關(guān)資料,這里提供調(diào)用錄像,錄音,拍照等功能,需要的朋友可以參考下
    2017-08-08
  • Android應(yīng)用實現(xiàn)點(diǎn)擊按鈕震動

    Android應(yīng)用實現(xiàn)點(diǎn)擊按鈕震動

    這篇文章主要為大家詳細(xì)介紹了Android應(yīng)用實現(xiàn)點(diǎn)擊按鈕震動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放

    android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放

    本篇文章實現(xiàn)了Android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • 淺析Kotlin使用infix函數(shù)構(gòu)建可讀語法流程講解

    淺析Kotlin使用infix函數(shù)構(gòu)建可讀語法流程講解

    這篇文章主要介紹了淺析Kotlin使用infix函數(shù)構(gòu)建可讀語法,我們在Kotlin中就多次使用A to B這樣的語法結(jié)構(gòu)構(gòu)建鍵值對,包括Kotlin自帶的mapOf()函數(shù),這種語法結(jié)構(gòu)的優(yōu)點(diǎn)是可讀性強(qiáng)
    2023-01-01
  • Android 中build.prop 文件與 getprop 命令

    Android 中build.prop 文件與 getprop 命令

    這篇文章主要介紹了Android 中build.prop 文件與 getprop 命令的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • android搜索框上下滑動變色效果

    android搜索框上下滑動變色效果

    這篇文章主要為大家詳細(xì)介紹了android實現(xiàn)搜索框上下滑動透明度改變的效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11

最新評論

延庆县| 罗山县| 如东县| 抚顺县| 温州市| 梨树县| 南安市| 高州市| 桂东县| 中牟县| 沙洋县| 永泰县| 五台县| 深水埗区| 贺兰县| 麻城市| 新巴尔虎右旗| 贺兰县| 余江县| 鄢陵县| 德阳市| 泌阳县| 铜川市| 仙游县| 裕民县| 桓仁| 县级市| 德州市| 岗巴县| 凤城市| 公安县| 水富县| 左云县| 山西省| 辉南县| 布尔津县| 石台县| 竹山县| 红桥区| 临朐县| 靖远县|