日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android----banner使用详解

發布時間:2024/3/13 Android 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android----banner使用详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android----banner使用詳解

昨天10.31 ,斗破蒼穹的三年之約終于出來了,自己也等了很久很久,敬師長,敬家人,敬朋友,敬每一個前行路上正在奮戰的自己,星光不問趕路人,時間不負有心人。

效果圖:

  • 添加依賴

    implementation 'com.youth.banner:banner:2.1.0'
  • 添加權限到你的 AndroidManifest.xml

    <!-- if you want to load images from the internet --> <uses-permission android:name="android.permission.INTERNET" />
  • 布局文件

    <com.youth.banner.Bannerandroid:id="@+id/banner"android:layout_width="0dp"android:layout_height="250dp"android:layout_margin="10dp"app:banner_radius="20dp" // 圓角android:clickable="true" // 是否可點擊app:banner_indicator_selected_color="#95F2EC"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.157"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" />
  • 邏輯代碼

    package com.hnucm.xiaotang;import android.os.Bundle;import androidx.fragment.app.Fragment;import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;import com.bumptech.glide.Glide; import com.bumptech.glide.load.resource.bitmap.RoundedCorners; import com.bumptech.glide.request.RequestOptions; import com.youth.banner.Banner; import com.youth.banner.adapter.BannerImageAdapter; import com.youth.banner.holder.BannerImageHolder; import com.youth.banner.indicator.CircleIndicator; import com.youth.banner.listener.OnBannerListener;import org.json.JSONArray; import org.json.JSONException; import org.xutils.common.Callback; import org.xutils.http.RequestParams; import org.xutils.x;import java.util.ArrayList; import java.util.List;public class ShouYeFragment extends Fragment implements OnBannerListener {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View inflate = inflater.inflate(R.layout.fragment_shou_ye, container, false);Banner banner =inflate.findViewById(R.id.banner);List<String> imgList = new ArrayList<>();x.Ext.init(getActivity().getApplication());x.Ext.setDebug(BuildConfig.DEBUG); // 是否輸出debug日志, 開啟debug會影響性能.x.view().inject(getActivity()); //沒有用到view注解可以先不用imgList.add("https://cdn.jsdelivr.net/gh/Yqifei/Blog-Image@master/20211026/image.6719h9mvs700.png");imgList.add("https://cdn.jsdelivr.net/gh/Yqifei/Blog-Image@master/20211031/800-(11).2txrpbqztva0.jpg");imgList.add("https://cdn.jsdelivr.net/gh/Yqifei/Blog-Image@master/20211031/800-(5).5s6zwxy19v40.jpg");imgList.add("https://cdn.jsdelivr.net/gh/Yqifei/Blog-Image@master/20211031/800-(10).24p8puxcmqbk.jpg");imgList.add("https://cdn.jsdelivr.net/gh/Yqifei/Blog-Image@master/20211031/800-(14).pizaxijh534.jpg");RequestParams params = new RequestParams("https://www.fastmock.site/mock/08392ee207964eb010bf22b157103494/androidJavaEE/banner");x.http().get(params, new Callback.CommonCallback<String>() {@Overridepublic void onSuccess(String result) {try {JSONArray jsonArray = new JSONArray(result);for (int i=0;i<jsonArray.length();i++){imgList.add((String)jsonArray.get(i));}banner.setDatas(imgList); // 動態更新banner數據} catch (JSONException e) {e.printStackTrace();}}@Overridepublic void onError(Throwable ex, boolean isOnCallback) {}@Overridepublic void onCancelled(CancelledException cex) {}@Overridepublic void onFinished() {}});banner.setAdapter(new BannerImageAdapter<String>(imgList) {@Overridepublic void onBindView(BannerImageHolder holder, String data, int position, int size) {System.out.println("hello TEST");Glide.with(holder.itemView).load(data).apply(RequestOptions.bitmapTransform(new RoundedCorners(30))).into(holder.imageView);}}).setIndicator(new CircleIndicator(getContext())).setLoopTime(1000).setOnBannerListener(this);// 設置圓形指示點,設置循環時間,設置監聽器return inflate;}@Overridepublic void OnBannerClick(Object data, int position) { // 監聽每一個圖片的點擊事件Log.i("tag", "你點了第"+position+"張輪播圖");} }
  • 常見的一些屬性設置

    方法

    更多方法以實際使用為準,下面不一定全部列出了

    方法名返回類型描述
    getAdapter()extends BannerAdapter獲取你設置的BannerAdapter
    getViewPager2()ViewPager2獲取ViewPager2
    getIndicator()Indicator獲取你設置的指示器(沒有設置直接獲取會拋異常哦)
    getIndicatorConfig()IndicatorConfig獲取你設置的指示器配置信息(沒有設置直接獲取會拋異常哦)
    getRealCount()int返回banner真實總數
    setUserInputEnabled(boolean)this禁止手動滑動Banner;true 允許,false 禁止
    setDatas(List)this重新設置banner數據
    isAutoLoop(boolean)this是否允許自動輪播
    setLoopTime(long)this設置輪播間隔時間(默認3000毫秒)
    setScrollTime(long)this設置輪播滑動的時間(默認800毫秒)
    start()this開始輪播(主要配合生命周期使用),或者你手動暫停再次啟動
    stop()this停止輪播(主要配合生命周期使用),或者你需要手動暫停
    setAdapter(T extends BannerAdapter)this設置banner的適配器
    setAdapter(T extends BannerAdapter,boolean)this設置banner的適配器,是否支持無限循環
    setOrientation(@Orientation)this設置banner輪播方向(垂直or水平)
    setOnBannerListener(this)this設置點擊事件,下標是從0開始
    addOnPageChangeListener(this)this添加viewpager2的滑動監聽
    setPageTransformer(PageTransformer)this設置viewpager的切換效果
    addPageTransformer(PageTransformer)this添加viewpager的切換效果(可以設置多個)
    setIndicator(Indicator)this設置banner輪播指示器(提供有base和接口,可以自定義)
    setIndicator(Indicator,boolean)this設置指示器(傳false代表不將指示器添加到banner上,配合布局文件,可以自我發揮)
    setIndicatorSelectedColor(@ColorInt)this設置指示器選中顏色
    setIndicatorSelectedColorRes(@ColorRes)this設置指示器選中顏色
    setIndicatorNormalColor(@ColorInt)this設置指示器默認顏色
    setIndicatorNormalColorRes(@ColorRes)this設置指示器默認顏色
    setIndicatorGravity(@IndicatorConfig.Direction)this設置指示器位置(左,中,右)
    setIndicatorSpace(int)this設置指示器之間的間距
    setIndicatorMargins(IndicatorConfig.Margins)this設置指示器的Margins
    setIndicatorWidth(int,int)this設置指示器選中和未選中的寬度,直接影響繪制指示器的大小
    setIndicatorNormalWidth(int)this設置指示器未選中的寬度
    setIndicatorSelectedWidth(int)this設置指示器選中的寬度
    setIndicatorRadius(int)this設置指示器圓角,不要圓角可以設置為0
    setIndicatorHeight(int)this設置指示器高度
    setBannerRound(float)this設置banner圓角(還有一種setBannerRound2方法,需要5.0以上)
    setBannerGalleryEffect(int,int,float)this畫廊效果
    setBannerGalleryMZ(int,float)this魅族效果
    setStartPosition(int)this設置開始的位置 (需要在setAdapter或者setDatas之前調用才有效哦)
    setIndicatorPageChange()this設置指示器改變監聽 (一般是為了配合數據操作使用,看情況自己發揮)
    setCurrentItem()this設置當前位置,和原生使用效果一樣
    addBannerLifecycleObserver()this給banner添加生命周期觀察者,內部自動管理banner的生命周期

    Attributes屬性

    在banner布局文件中調用,如果你自定義了indicator請做好兼容處理。 下面的屬性并不是每個指示器都用得到,所以使用時要注意!

    Attributesformatdescribe
    banner_loop_timeinteger輪播間隔時間,默認3000
    banner_auto_loopboolean是否自動輪播,默認true
    banner_infinite_loopboolean是否支持無限循環(即首尾直接過渡),默認true
    banner_orientationenum輪播方向:horizontal(默認) or vertical
    banner_radiusdimensionbanner圓角半徑,默認0(不繪制圓角)
    banner_indicator_normal_widthdimension指示器默認的寬度,默認5dp (對RoundLinesIndicator無效)
    banner_indicator_selected_widthdimension指示器選中的寬度,默認7dp
    banner_indicator_normal_colorcolor指示器默認顏色,默認0x88ffffff
    banner_indicator_selected_colorcolor指示器選中顏色,默認0x88000000
    banner_indicator_spacedimension指示器之間的間距,默認5dp (對RoundLinesIndicator無效)
    banner_indicator_gravitydimension指示器位置,默認center
    banner_indicator_margindimension指示器的margin,默認5dp,不能和下面的同時使用
    banner_indicator_marginLeftdimension指示器左邊的margin
    banner_indicator_marginTopdimension指示器上邊的margin
    banner_indicator_marginRightdimension指示器右邊的margin
    banner_indicator_marginBottomdimension指示器下邊的margin
    banner_indicator_heightdimension指示器高度(對CircleIndicator無效)
    banner_indicator_radiusdimension指示器圓角(對CircleIndicator無效)
    banner_round_top_leftboolean設置要繪制的banner圓角方向(如果都不設置默認全部)
    banner_round_top_rightboolean設置要繪制的banner圓角方向(如果都不設置默認全部)
    banner_round_bottom_leftboolean設置要繪制的banner圓角方向(如果都不設置默認全部)
    banner_round_bottom_rightboolean設置要繪制的banner圓角方向(如果都不設置默認全部)
  • github官網: GitHub - youth5201314/banner: 🔥🔥🔥Banner 2.0 來了!Android廣告圖片輪播控件,內部基于ViewPager2實現,Indicator和UI都可以自定義。

  • 總結

    以上是生活随笔為你收集整理的Android----banner使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。