Android banner詳解用法案例
Android----banner使用詳解
昨天10.31 ,斗破蒼穹的三年之約終于出來(lái)了,自己也等了很久很久,敬師長(zhǎng),敬家人,敬朋友,敬每一個(gè)前行路上正在奮戰(zhàn)的自己,星光不問(wèn)趕路人,時(shí)間不負(fù)有心人。
效果圖:
添加依賴
implementation 'com.youth.banner:banner:2.1.0'
添加權(quán)限到你的 AndroidManifest.xml
<!-- if you want to load images from the internet --> <uses-permission android:name="android.permission.INTERNET" />
布局文件
<com.youth.banner.Banner android:id="@+id/banner" android:layout_width="0dp" android:layout_height="250dp" android:layout_margin="10dp" app:banner_radius="20dp" // 圓角 android:clickable="true" // 是否可點(diǎn)擊 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 { @Override public 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會(huì)影響性能. 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>() { @Override public 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); // 動(dòng)態(tài)更新banner數(shù)據(jù) } catch (JSONException e) { e.printStackTrace(); } } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); banner.setAdapter(new BannerImageAdapter<String>(imgList) { @Override public 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); // 設(shè)置圓形指示點(diǎn),設(shè)置循環(huán)時(shí)間,設(shè)置監(jiān)聽器 return inflate; } @Override public void OnBannerClick(Object data, int position) { // 監(jiān)聽每一個(gè)圖片的點(diǎn)擊事件 Log.i("tag", "你點(diǎn)了第"+position+"張輪播圖"); } }
常見的一些屬性設(shè)置
方法
更多方法以實(shí)際使用為準(zhǔn),下面不一定全部列出了
方法名 | 返回類型 | 描述 |
---|---|---|
getAdapter() | extends BannerAdapter | 獲取你設(shè)置的BannerAdapter |
getViewPager2() | ViewPager2 | 獲取ViewPager2 |
getIndicator() | Indicator | 獲取你設(shè)置的指示器(沒有設(shè)置直接獲取會(huì)拋異常哦) |
getIndicatorConfig() | IndicatorConfig | 獲取你設(shè)置的指示器配置信息(沒有設(shè)置直接獲取會(huì)拋異常哦) |
getRealCount() | int | 返回banner真實(shí)總數(shù) |
setUserInputEnabled(boolean) | this | 禁止手動(dòng)滑動(dòng)Banner;true 允許,false 禁止 |
setDatas(List) | this | 重新設(shè)置banner數(shù)據(jù) |
isAutoLoop(boolean) | this | 是否允許自動(dòng)輪播 |
setLoopTime(long) | this | 設(shè)置輪播間隔時(shí)間(默認(rèn)3000毫秒) |
setScrollTime(long) | this | 設(shè)置輪播滑動(dòng)的時(shí)間(默認(rèn)800毫秒) |
start() | this | 開始輪播(主要配合生命周期使用),或者你手動(dòng)暫停再次啟動(dòng) |
stop() | this | 停止輪播(主要配合生命周期使用),或者你需要手動(dòng)暫停 |
setAdapter(T extends BannerAdapter) | this | 設(shè)置banner的適配器 |
setAdapter(T extends BannerAdapter,boolean) | this | 設(shè)置banner的適配器,是否支持無(wú)限循環(huán) |
setOrientation(@Orientation) | this | 設(shè)置banner輪播方向(垂直or水平) |
setOnBannerListener(this) | this | 設(shè)置點(diǎn)擊事件,下標(biāo)是從0開始 |
addOnPageChangeListener(this) | this | 添加viewpager2的滑動(dòng)監(jiān)聽 |
setPageTransformer(PageTransformer) | this | 設(shè)置viewpager的切換效果 |
addPageTransformer(PageTransformer) | this | 添加viewpager的切換效果(可以設(shè)置多個(gè)) |
setIndicator(Indicator) | this | 設(shè)置banner輪播指示器(提供有base和接口,可以自定義) |
setIndicator(Indicator,boolean) | this | 設(shè)置指示器(傳false代表不將指示器添加到banner上,配合布局文件,可以自我發(fā)揮) |
setIndicatorSelectedColor(@ColorInt) | this | 設(shè)置指示器選中顏色 |
setIndicatorSelectedColorRes(@ColorRes) | this | 設(shè)置指示器選中顏色 |
setIndicatorNormalColor(@ColorInt) | this | 設(shè)置指示器默認(rèn)顏色 |
setIndicatorNormalColorRes(@ColorRes) | this | 設(shè)置指示器默認(rèn)顏色 |
setIndicatorGravity(@IndicatorConfig.Direction) | this | 設(shè)置指示器位置(左,中,右) |
setIndicatorSpace(int) | this | 設(shè)置指示器之間的間距 |
setIndicatorMargins(IndicatorConfig.Margins) | this | 設(shè)置指示器的Margins |
setIndicatorWidth(int,int) | this | 設(shè)置指示器選中和未選中的寬度,直接影響繪制指示器的大小 |
setIndicatorNormalWidth(int) | this | 設(shè)置指示器未選中的寬度 |
setIndicatorSelectedWidth(int) | this | 設(shè)置指示器選中的寬度 |
setIndicatorRadius(int) | this | 設(shè)置指示器圓角,不要圓角可以設(shè)置為0 |
setIndicatorHeight(int) | this | 設(shè)置指示器高度 |
setBannerRound(float) | this | 設(shè)置banner圓角(還有一種setBannerRound2方法,需要5.0以上) |
setBannerGalleryEffect(int,int,float) | this | 畫廊效果 |
setBannerGalleryMZ(int,float) | this | 魅族效果 |
setStartPosition(int) | this | 設(shè)置開始的位置 (需要在setAdapter或者setDatas之前調(diào)用才有效哦) |
setIndicatorPageChange() | this | 設(shè)置指示器改變監(jiān)聽 (一般是為了配合數(shù)據(jù)操作使用,看情況自己發(fā)揮) |
setCurrentItem() | this | 設(shè)置當(dāng)前位置,和原生使用效果一樣 |
addBannerLifecycleObserver() | this | 給banner添加生命周期觀察者,內(nèi)部自動(dòng)管理banner的生命周期 |
在banner布局文件中調(diào)用,如果你自定義了indicator請(qǐng)做好兼容處理。 下面的屬性并不是每個(gè)指示器都用得到,所以使用時(shí)要注意!
Attributes | format | describe |
---|---|---|
banner_loop_time | integer | 輪播間隔時(shí)間,默認(rèn)3000 |
banner_auto_loop | boolean | 是否自動(dòng)輪播,默認(rèn)true |
banner_infinite_loop | boolean | 是否支持無(wú)限循環(huán)(即首尾直接過(guò)渡),默認(rèn)true |
banner_orientation | enum | 輪播方向:horizontal(默認(rèn)) or vertical |
banner_radius | dimension | banner圓角半徑,默認(rèn)0(不繪制圓角) |
banner_indicator_normal_width | dimension | 指示器默認(rèn)的寬度,默認(rèn)5dp (對(duì)RoundLinesIndicator無(wú)效) |
banner_indicator_selected_width | dimension | 指示器選中的寬度,默認(rèn)7dp |
banner_indicator_normal_color | color | 指示器默認(rèn)顏色,默認(rèn)0x88ffffff |
banner_indicator_selected_color | color | 指示器選中顏色,默認(rèn)0x88000000 |
banner_indicator_space | dimension | 指示器之間的間距,默認(rèn)5dp (對(duì)RoundLinesIndicator無(wú)效) |
banner_indicator_gravity | dimension | 指示器位置,默認(rèn)center |
banner_indicator_margin | dimension | 指示器的margin,默認(rèn)5dp,不能和下面的同時(shí)使用 |
banner_indicator_marginLeft | dimension | 指示器左邊的margin |
banner_indicator_marginTop | dimension | 指示器上邊的margin |
banner_indicator_marginRight | dimension | 指示器右邊的margin |
banner_indicator_marginBottom | dimension | 指示器下邊的margin |
banner_indicator_height | dimension | 指示器高度(對(duì)CircleIndicator無(wú)效) |
banner_indicator_radius | dimension | 指示器圓角(對(duì)CircleIndicator無(wú)效) |
banner_round_top_left | boolean | 設(shè)置要繪制的banner圓角方向(如果都不設(shè)置默認(rèn)全部) |
banner_round_top_right | boolean | 設(shè)置要繪制的banner圓角方向(如果都不設(shè)置默認(rèn)全部) |
banner_round_bottom_left | boolean | 設(shè)置要繪制的banner圓角方向(如果都不設(shè)置默認(rèn)全部) |
banner_round_bottom_right | boolean | 設(shè)置要繪制的banner圓角方向(如果都不設(shè)置默認(rèn)全部) |
github官網(wǎng):
GitHub - youth5201314/banner: 🔥🔥🔥Banner 2.0 來(lái)了!Android廣告圖片輪播控件,內(nèi)部基于ViewPager2實(shí)現(xiàn),Indicator和UI都可以自定義。
到此這篇關(guān)于Python Django視圖響應(yīng)三函數(shù)詳解分析的文章就介紹到這了,更多相關(guān)Python Django內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android入門之使用SQLite內(nèi)嵌式數(shù)據(jù)庫(kù)詳解
Android內(nèi)帶SQLite內(nèi)嵌式數(shù)據(jù)庫(kù)了。這對(duì)于我們存儲(chǔ)一些更復(fù)雜的結(jié)構(gòu)化數(shù)據(jù)帶來(lái)了極大的便利。本文就來(lái)和大家聊聊具體的使用方法,希望對(duì)大家有所幫助2022-12-12Android自定義Dialog實(shí)現(xiàn)加載對(duì)話框效果
這篇文章將介紹如何定制當(dāng)今主流的對(duì)話框,通過(guò)自定義dialog實(shí)現(xiàn)加載對(duì)話框效果,具體實(shí)現(xiàn)代碼大家通過(guò)本文學(xué)習(xí)吧2018-05-05android 仿微信demo——微信消息界面實(shí)現(xiàn)(移動(dòng)端)
本系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你們提供幫助2021-06-06android 線性布局LinearLayout實(shí)例代碼
android 線性布局LinearLayout實(shí)例代碼,需要的朋友可以參考一下2013-05-05Android Bitmap的加載優(yōu)化與Cache相關(guān)介紹
這篇文章主要介紹了Android中性能優(yōu)化之Bitmap的加載優(yōu)化與Cache相關(guān)內(nèi)容介紹,文中介紹的很詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-02-02Android自定義View圖片按Path運(yùn)動(dòng)和旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Android自定義View圖片按Path運(yùn)動(dòng)和旋轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Android簡(jiǎn)單封裝一個(gè)MVP基類流程詳解
MVP是從經(jīng)典的模式MVC演變而來(lái),它們的基本思想有相通的地方:Controller/Presenter負(fù)責(zé)邏輯的處理,Model提供數(shù)據(jù),View負(fù)責(zé)顯示。下面這篇文章主要給大家介紹了關(guān)于Android從實(shí)現(xiàn)到封裝MVP的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧2023-03-03PopupWindow自定義位置顯示的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了PopupWindow自定義位置顯示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10