### Initialize and Configure Banner Programmatically Source: https://github.com/youth5201314/banner/blob/master/README.md This Java snippet demonstrates how to programmatically initialize and configure the Banner. It covers adding a lifecycle observer, setting a custom adapter, and applying an indicator. A simplified example for image-only carousels using `BannerImageAdapter` with Glide for image loading is also provided. ```java public class BannerActivity extends AppCompatActivity { public void useBanner() { //--------------------------简单使用------------------------------- banner.addBannerLifecycleObserver(this)//添加生命周期观察者 .setAdapter(new BannerExampleAdapter(DataBean.getTestData())) .setIndicator(new CircleIndicator(this)); //—————————————————————————如果你想偷懒,而又只是图片轮播———————————————————————— banner.setAdapter(new BannerImageAdapter(DataBean.getTestData3()) { @Override public void onBindView(BannerImageHolder holder, DataBean data, int position, int size) { //图片加载自己实现 Glide.with(holder.itemView) .load(data.imageUrl) .apply(RequestOptions.bitmapTransform(new RoundedCorners(30))) .into(holder.imageView); } }) .addBannerLifecycleObserver(this)//添加生命周期观察者 .setIndicator(new CircleIndicator(this)); //更多使用方法仔细阅读文档,或者查看demo } } ``` -------------------------------- ### Integrate Banner View in XML Layout Source: https://github.com/youth5201314/banner/blob/master/README.md An example of embedding the `com.youth.banner.Banner` view into an Android XML layout. It includes the necessary `xmlns:app` namespace and basic layout attributes. This step is optional as the Banner can also be instantiated programmatically. ```xml ``` -------------------------------- ### Manage Banner Lifecycle in Android Activity Source: https://github.com/youth5201314/banner/blob/master/README.md This Java code demonstrates two approaches to manage the lifecycle of a `banner` instance within an Android `Activity`. The first method involves manually calling `start()`, `stop()`, and `destroy()` in the Activity's respective lifecycle methods (`onStart()`, `onStop()`, `onDestroy()`). The second, more automated method, involves registering the Activity as a lifecycle observer for the banner using `banner.addBannerLifecycleObserver(this)` in `onCreate()`, allowing the banner to manage its own lifecycle. ```java public class BannerActivity { //方法一:自己控制banner的生命周期 @Override protected void onStart() { super.onStart(); //开始轮播 banner.start(); } @Override protected void onStop() { super.onStop(); //停止轮播 banner.stop(); } @Override protected void onDestroy() { super.onDestroy(); //销毁 banner.destroy(); } //方法二:调用banner的addBannerLifecycleObserver()方法,让banner自己控制 protected void onCreate(Bundle savedInstanceState) { //添加生命周期观察者 banner.addBannerLifecycleObserver(this); } } ``` -------------------------------- ### Add Banner Library Gradle Dependency Source: https://github.com/youth5201314/banner/blob/master/README.md Instructions for adding the Banner library to your Android project's `build.gradle` file. It specifies the Maven Central repository and the `implementation` dependency for version 2.2.3, noting the change from JCenter. ```groovy repositories { maven { url "https://s01.oss.sonatype.org/content/groups/public" } } dependencies{ //2.1.0以前jcenter的依赖 //implementation 'com.youth.banner:banner:2.1.0' //现在Maven Central implementation 'io.github.youth5201314:banner:2.2.3' } ``` -------------------------------- ### Implement Custom BannerAdapter for Data Handling Source: https://github.com/youth5201314/banner/blob/master/README.md This Java code defines `ImageAdapter`, a custom implementation of `BannerAdapter`. It illustrates how to create a `BannerViewHolder` and bind data to an `ImageView`, emphasizing the requirement for `MATCH_PARENT` layout parameters for views within the adapter. This customization is recommended for complex layouts beyond simple image carousels. ```java /** * 自定义布局,下面是常见的图片样式,更多实现可以看demo,可以自己随意发挥 */ public class ImageAdapter extends BannerAdapter { public ImageAdapter(List mDatas) { //设置数据,也可以调用banner提供的方法,或者自己在adapter中实现 super(mDatas); } //创建ViewHolder,可以用viewType这个字段来区分不同的ViewHolder @Override public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) { ImageView imageView = new ImageView(parent.getContext()); //注意,必须设置为match_parent,这个是viewpager2强制要求的 imageView.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); return new BannerViewHolder(imageView); } @Override public void onBindView(BannerViewHolder holder, DataBean data, int position, int size) { holder.imageView.setImageResource(data.imageRes); } class BannerViewHolder extends RecyclerView.ViewHolder { ImageView imageView; public BannerViewHolder(@NonNull ImageView view) { super(view); this.imageView = view; } } } ``` -------------------------------- ### Banner Custom Attributes Reference Source: https://github.com/youth5201314/banner/blob/master/README.md A detailed list of customizable attributes for the Banner view, typically used within XML layout files. It's important to note that not all attributes are applicable to every indicator type, and compatibility should be verified when using custom indicators. ```APIDOC Banner Attributes: banner_loop_time: integer description: Carousel interval time, default 3000ms banner_auto_loop: boolean description: Whether to auto-loop, default true banner_infinite_loop: boolean description: Whether to support infinite looping (direct transition between start and end), default true banner_orientation: enum (horizontal, vertical) description: Carousel direction, default horizontal banner_radius: dimension description: Banner corner radius, default 0 (no rounded corners) banner_indicator_normal_width: dimension description: Indicator default width, default 5dp (ineffective for RoundLinesIndicator) banner_indicator_selected_width: dimension description: Indicator selected width, default 7dp banner_indicator_normal_color: color description: Indicator default color, default 0x88ffffff banner_indicator_selected_color: color description: Indicator selected color, default 0x88000000 banner_indicator_space: dimension description: Space between indicators, default 5dp (ineffective for RoundLinesIndicator) banner_indicator_gravity: dimension description: Indicator position, default center banner_indicator_margin: dimension description: Indicator margin, default 5dp, cannot be used simultaneously with individual margins below banner_indicator_marginLeft: dimension description: Indicator left margin banner_indicator_marginTop: dimension description: Indicator top margin banner_indicator_marginRight: dimension description: Indicator right margin banner_indicator_marginBottom: dimension description: Indicator bottom margin banner_indicator_height: dimension description: Indicator height (ineffective for CircleIndicator) banner_indicator_radius: dimension description: Indicator corner radius (ineffective for CircleIndicator) banner_round_top_left: boolean description: Set the banner rounded corner direction to draw (if none are set, all are drawn by default) banner_round_top_right: boolean description: Set the banner rounded corner direction to draw (if none are set, all are drawn by default) banner_round_bottom_left: boolean description: Set the banner rounded corner direction to draw (if none are set, all are drawn by default) banner_round_bottom_right: boolean description: Set the banner rounded corner direction to draw (if none are set, all are drawn by default) ``` -------------------------------- ### Declare Internet Permission in AndroidManifest Source: https://github.com/youth5201314/banner/blob/master/README.md This snippet shows how to declare the `android.permission.INTERNET` permission in your `AndroidManifest.xml` file, which is necessary for the Banner library to load images from network sources. ```xml ``` -------------------------------- ### Configure Android Layout to Prevent Banner Focus Source: https://github.com/youth5201314/banner/blob/master/README.md This XML snippet illustrates how to set `android:focusable` and `android:focusableInTouchMode` attributes on a parent view. Applying these attributes prevents the banner from automatically gaining focus and expanding when the parent view is scrolled, ensuring a smoother user experience. ```xml android:focusable="true" android:focusableInTouchMode="true" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.