溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用VerticalBannerView怎么實(shí)現(xiàn)垂直輪播廣告

發(fā)布時(shí)間:2021-06-03 16:02:45 來源:億速云 閱讀:188 作者:Leah 欄目:移動(dòng)開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)使用VerticalBannerView怎么實(shí)現(xiàn)垂直輪播廣告,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

一、項(xiàng)目使用

(1).添加項(xiàng)目依賴。

dependencies {
  compile 'com.github.Rowandjj:VerticalBannerView:1.0'
}

(2).添加布局。

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center_vertical"
  android:orientation="horizontal">
 
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"
    android:text="淘寶頭條"
    android:textStyle="bold"/>
 
  <View
    android:layout_width="1dp"
    android:layout_height="40dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="#cccccc"/>
 
  <com.taobao.library.VerticalBannerView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/banner"
    android:layout_width="wrap_content"
    android:layout_height="36dp"
    app:animDuration="900"
    app:gap="2000"/>
</LinearLayout>

(3).實(shí)現(xiàn)Adapter。

public class SampleAdapter extends BaseBannerAdapter<Model> {
  private List<Model> mDatas;
 
  public SampleAdapter01(List<Model> datas) {
    super(datas);
  }
 
  @Override
  public View getView(VerticalBannerView parent) {
    return LayoutInflater.from(parent.getContext()).inflate(R.layout.your_item,null);
  }
 
  @Override
  public void setItem(final View view, final Model data) {
    TextView textView = (TextView) view.findViewById(R.id.text);
    textView.setText(data.title);
    // 你可以增加點(diǎn)擊事件
    view.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        // TODO handle click event
      }
    });
  }
}

(4).為VerticalBannerView設(shè)置Adapter,并啟動(dòng)動(dòng)畫。

List<Model> datas = new ArrayList<>();
datas.add(new Model01("Note7發(fā)布了"));
datas.add(new Model01("Note7被召回了"));
SampleAdapter adapter = new SampleAdapter(datas);
VerticalBannerView banner = (VerticalBannerView) findViewById(R.id.banner);
banner.setAdapter(adapter);
banner.start();

二、源碼分析

實(shí)現(xiàn)原理:

VerticalBannerView本質(zhì)上是一個(gè)垂直的LinearLayout。定義一個(gè)Adapter類,向LinearLayout提供子View。初始狀態(tài)下往LinearLayout中添加兩個(gè)子View,子View的高度同LinearLayout的高度一致,這樣一來只有第1個(gè)子View顯示出來,第2個(gè)子View在底部不顯示。然后使用屬性動(dòng)畫ObjectAnimator同時(shí)修改兩個(gè)子View的translationY屬性,動(dòng)畫執(zhí)行過程中translationY從默認(rèn)值0漸變到負(fù)的LinearLayout的高度,顯示出來的效果就是第1個(gè)子View逐漸向上退出,第2個(gè)子View從底部向上逐漸顯示。動(dòng)畫執(zhí)行完畢后,移除第1個(gè)子View,這樣第2個(gè)子View的索引變成0,并完全顯示出來占據(jù)LinearLayout的高度。再將已經(jīng)移除的第1個(gè)子View,添加到索引為1的位置,此時(shí)該子View超出父視圖之外完全不顯示。一輪動(dòng)畫執(zhí)行完畢,再調(diào)用postDelay()方法重復(fù)上述動(dòng)畫,一直循環(huán)下去。

下面進(jìn)入代碼部分,主要是兩個(gè)類BaseBannerAdapter和VerticalBannerView。

(1).BaseBannerAdapter類

BaseBannerAdapter類負(fù)責(zé)為廣告欄提供數(shù)據(jù)。我們在使用時(shí),需要寫一個(gè)Adapter類繼承BaseBannerAdapter,實(shí)現(xiàn)getView()和setItem()方法。在getView()方法中,我們需要把要添加到廣告欄中的item view創(chuàng)建出來并返回,setItem()方法則負(fù)責(zé)為創(chuàng)建的item view綁定數(shù)據(jù)。

public abstract class BaseBannerAdapter<T> {
  private List<T> mDatas;
  private OnDataChangedListener mOnDataChangedListener;
 
  public BaseBannerAdapter(List<T> datas) {
    mDatas = datas;
    if (datas == null || datas.isEmpty()) {
      throw new RuntimeException("nothing to show");
    }
  }
 
  public BaseBannerAdapter(T[] datas) {
    mDatas = new ArrayList<>(Arrays.asList(datas));
  }
 
  // 設(shè)置banner填充的數(shù)據(jù)
  public void setData(List<T> datas) {
    this.mDatas = datas;
    notifyDataChanged();
  }
 
  void setOnDataChangedListener(OnDataChangedListener listener) {
    mOnDataChangedListener = listener;
  }
 
  // 獲取banner總數(shù)
  public int getCount() {
    return mDatas == null ? 0 : mDatas.size();
  }
 
  // 通知數(shù)據(jù)改變
  void notifyDataChanged() {
    mOnDataChangedListener.onChanged();
  }
 
  // 獲取數(shù)據(jù)
  public T getItem(int position) {
    return mDatas.get(position);
  }
 
  // 設(shè)置banner的ItemView
  public abstract View getView(VerticalBannerView parent);
 
  // 設(shè)置banner的數(shù)據(jù)
  public abstract void setItem(View view, T data);
 
  // 數(shù)據(jù)變化的監(jiān)聽
  interface OnDataChangedListener {
    void onChanged();
  }
}

(2).VerticalBannerView類

VerticalBannerView類繼承自LinearLayout,并在構(gòu)造方法中設(shè)定方向?yàn)榇怪?。同時(shí)VerticalBannerView類實(shí)現(xiàn)了OnDataChangedListener接口,實(shí)現(xiàn)onChanged()方法,這樣當(dāng)改變數(shù)據(jù)后調(diào)用BaseBannerAdapter的notifyDataChanged()時(shí),VerticalBannerView的onChanged()方法被回調(diào),執(zhí)行setupAdapter()重新初始化數(shù)據(jù)。

public class VerticalBannerView extends LinearLayout implements BaseBannerAdapter.OnDataChangedListener {
  public VerticalBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
  }
 
  private void init(Context context, AttributeSet attrs, int defStyleAttr) {
    setOrientation(VERTICAL);
 ......
  }
 
  ......
 
  @Override
  public void onChanged() {
    setupAdapter();
  }
 
  ......
}

為VerticalBannerView添加item數(shù)據(jù),需要調(diào)用setAdapter()方法,關(guān)鍵在于其中執(zhí)行的setupAdapter()方法。

public void setAdapter(BaseBannerAdapter adapter) {
  if (adapter == null) {
    throw new RuntimeException("adapter must not be null");
  }
  if (mAdapter != null) {
    throw new RuntimeException("you have already set an Adapter");
  }
  this.mAdapter = adapter;
  mAdapter.setOnDataChangedListener(this);
  setupAdapter();
}

在setupAdapter()方法中,先移除所有的子View,然后調(diào)用Adapter的getView()方法創(chuàng)建兩個(gè)子View,分別賦值給成員變量mFirstView和mSecondView,并為這兩個(gè)子View綁定數(shù)據(jù),最后再調(diào)用addView()添加進(jìn)來。

// 初始化Child View
private void setupAdapter() {
  // 先移除所有的子View
  removeAllViews();
 
  if (mAdapter.getCount() == 1) {
    mFirstView = mAdapter.getView(this);
    mAdapter.setItem(mFirstView, mAdapter.getItem(0));
    addView(mFirstView);
  } else {
    // 調(diào)用Adapter的getView()方法,創(chuàng)建兩個(gè)子View,分別賦值給mFirstView和mSecondView
    mFirstView = mAdapter.getView(this);
    mSecondView = mAdapter.getView(this);
    // 使用索引0和1的數(shù)據(jù),為mFirstView和mSecondView設(shè)置數(shù)據(jù)
    mAdapter.setItem(mFirstView, mAdapter.getItem(0));
    mAdapter.setItem(mSecondView, mAdapter.getItem(1));
    // 將mFirstView和mSecondView添加到當(dāng)前View
    addView(mFirstView);
    addView(mSecondView);
 
    mPosition = 1;
    isStarted = false;
  }
  setBackgroundDrawable(mFirstView.getBackground());
}

為了實(shí)現(xiàn)子View之間的切換,需要把上面添加進(jìn)來的子View的高度修改為與VerticalBannerView高度一致。這個(gè)操作在onMeasure()方法中完成。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  // 成員變量mBannerHeight有一個(gè)默認(rèn)值
  if (LayoutParams.WRAP_CONTENT == getLayoutParams().height) {
    // 如果當(dāng)前View的高度設(shè)置為wrap_content,使用默認(rèn)值
    getLayoutParams().height = (int) mBannerHeight;
  } else {
    // 如果當(dāng)前View設(shè)定了固定高度,則使用設(shè)定的高度
    mBannerHeight = getHeight();
  }
  // 修改mFirstView和mSecondView的高度為其父視圖的高度
  if (mFirstView != null) {
    mFirstView.getLayoutParams().height = (int) mBannerHeight;
  }
  if (mSecondView != null) {
    mSecondView.getLayoutParams().height = (int) mBannerHeight;
  }
}

上面的準(zhǔn)備工作完成后,就可以進(jìn)入動(dòng)畫的執(zhí)行了。VerticalBannerView通過調(diào)用start()方法啟動(dòng)切換動(dòng)畫。在start()方法中,調(diào)用postDelayed()執(zhí)行AnimRunnable任務(wù),在AnimRunnable的run()方法中,先調(diào)用performSwitch(),然后再次調(diào)用postDelayed()使AnimRunnable任務(wù)一直循環(huán)執(zhí)行下去。兩個(gè)子View之間的切換工作由performSwitch()負(fù)責(zé)執(zhí)行。

public void start() {
  if (mAdapter == null) {
    throw new RuntimeException("you must call setAdapter() before start");
  }
 
  if (!isStarted && mAdapter.getCount() > 1) {
    isStarted = true;
    postDelayed(mRunnable, mGap);
  }
}
 
private AnimRunnable mRunnable = new AnimRunnable();
 
private class AnimRunnable implements Runnable {
  @Override
  public void run() {
    performSwitch();
    // 調(diào)用postDelayed()延時(shí)再執(zhí)行,一直循環(huán)下去
    postDelayed(this, mGap);
  }
}

下面再進(jìn)入performSwitch()方法,該方法是Item View之間產(chǎn)生切換效果的核心。

// 執(zhí)行切換
private void performSwitch() {
  // 動(dòng)畫在執(zhí)行過程中,View的translationY屬性從0一直減小到-mBannerHeight
  // 因?yàn)閠ranslationY屬性一直是負(fù)值,所以View向上移動(dòng)
  ObjectAnimator animator1 = ObjectAnimator.ofFloat(mFirstView, "translationY", -mBannerHeight);
  ObjectAnimator animator2 = ObjectAnimator.ofFloat(mSecondView, "translationY", -mBannerHeight);
  AnimatorSet set = new AnimatorSet();
  set.playTogether(animator1, animator2);
  set.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
      // 動(dòng)畫執(zhí)行完成,把mFirstView和mSecondView的translationY恢復(fù)到默認(rèn)狀態(tài)
      mFirstView.setTranslationY(0);
      mSecondView.setTranslationY(0);
      // 使用下一條數(shù)據(jù),設(shè)置到第一個(gè)子View
      View removedView = getChildAt(0);
      mPosition++;
      mAdapter.setItem(removedView, mAdapter.getItem(mPosition % mAdapter.getCount()));
      // 移除第一個(gè)子View,此時(shí)當(dāng)前LinearLayout的childCount==1
      removeView(removedView);
      // 移除的View,再添加到第2個(gè)位置
      addView(removedView, 1);
    }
  });
  set.setDuration(mAnimDuration);
  set.start();
}

關(guān)于使用VerticalBannerView怎么實(shí)現(xiàn)垂直輪播廣告就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI