android自定義控件用來展示廣告:VerticalBannerView

jopen 8年前發布 | 8K 次閱讀 Android開發 移動開發 VerticalBannerView

VerticalBannerView

VerticalBannerView是一個android平臺下的自定義控件,通常用來展示廣告,類似淘寶頭條,它的樣式如下:

https://github.com/Rowandjj/VerticalBannerView/blob/master/art.gif

Feature

  1. 可自由定義展示的內容
  2. 使用方式類似ListView/RecyclerView
  3. 可為當前的內容添加各種事件,比如點擊打開某個頁面等

Attention

API >= 11

Usage

可以類比ListView

  1. 添加依賴

    • Add it in your root build.gradle at the end of repositories:

          allprojects {
              repositories {
                  ...
                  maven { url "https://jitpack.io" }
              }
          }
    • Add the dependency

      dependencies {
                  compile 'com.github.Rowandjj:VerticalBannerView:1.0'
          }
  2. 定義item的布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:background="#000"
                    android:gravity="center_vertical"
                  android:layout_height="60dp">
    
        <ImageView
            android:layout_marginLeft="10dp"
            android:id="@+id/iv"
            android:src="@android:drawable/ic_dialog_email"
            android:layout_width="30dp"
            android:layout_height="30dp"
            />
    
        <TextView
            android:id="@+id/tv_02"
            android:text="hello world"
            android:layout_marginLeft="10dp"
            android:textSize="16sp"
            android:layout_toRightOf="@id/iv"
            android:paddingLeft="5dp"
            android:textColor="#fff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
        <TextView
            android:layout_below="@id/tv_02"
            android:text="I am detail message......"
            android:layout_marginLeft="10dp"
            android:textSize="14sp"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/iv"
            android:paddingLeft="5dp"
            android:textColor="#fff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </RelativeLayout>
  3. 實現adapter

    public class SampleAdapter01 extends BaseBannerAdapter<Model01> {
    
        private List<Model01> mDatas;
    
        public SampleAdapter01(List<Model01> datas) {
            super(datas);
        }
    
        @Override
        public View getView(VerticalBannerView parent) {
            return LayoutInflater.from(parent.getContext()).inflate(R.layout.item_01,null);
        }
    
        @Override
        public void setItem(final View view, final Model01 data) {
            TextView tv = (TextView) view.findViewById(R.id.tv_01);
            tv.setText(data.title);
            //你可以增加點擊事件
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //比如打開url
                    Toast.makeText(view.getContext(),data.url,Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
  4. 在布局中增加view的聲明

       <com.taobao.library.VerticalBannerView
                android:id="@+id/banner_01"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                app:animDuration="900" ---->動畫間隔900ms
                app:gap="2000"/> ----->切換時長2000ms
  5. 設置Adapter并啟動

    List<Model01> datas02 = new ArrayList<>();
    datas02.add(new Model01("Life was so beautiful","--->Life was so beautiful,"));
    datas02.add(new Model01("From morning to evening","--->From morning to evening"));
    datas02.add(new Model01("We enjoyed the road to school","--->We enjoyed the road to school,"));
    datas02.add(new Model01("Birds chirped and Peace lingered","--->Birds chirped and Peace lingered"));
    final SampleAdapter02 adapter02 = new SampleAdapter02(datas02);
    final VerticalBannerView banner02 = (VerticalBannerView) findViewById(R.id.banner_02);
    banner02.setAdapter(adapter02);
    banner02.start();
  6. 更新數據

    List<Model01> newData = new ArrayList<>();
    newData.add(new Model01("鋤禾日當午","--->鋤禾日當午"));
    newData.add(new Model01("汗滴禾下土","--->汗滴禾下土"));
    newData.add(new Model01("誰知盤中餐","--->誰知盤中餐"));
    newData.add(new Model01("粒粒皆辛苦","--->粒粒皆辛苦"));
    adapter02.setData(newData);
  7. 停止

    banner02.stop();

項目地址: https://github.com/Rowandjj/VerticalBannerView

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!