MZBannerView

repository·master·Indexed 25 days ago

https://github.com/pinguo-zhouwei/mzbannerview

An Android library providing customizable banner views, including a high-fidelity imitation of the Meizu app banner effect. It supports auto-scrolling banners, Meizu-style banners, and standard ViewPager functionality. Version v2.0.2 is available via JitPack.

Tokens
1.5K
Snippets
4
Records
6
Agent score
31%

What's inside MZBannerView

  1. Achieve different Banner display modes

    master

    You can control the visual style of the banner by combining open_mz_mode, middle_page_cover, and canLoop attributes.

    <!-- 1. Meizu Banner Effect (Middle page covers sides) -->
    app:open_mz_mode="true"
    app:canLoop="true"
    app:middle_page_cover="true"
    
    <!-- 2. Standard Banner Effect -->
    app:open_mz_mode="false"
    app:canLoop="true"
    
    <!-- 3. Meizu Style (Middle page does NOT cover sides) -->
    app:open_mz_mode="true"
    app:canLoop="true"
    app:middle_page_cover="false"
    
    <!-- 4. iQIYI Style (Pages have gaps) -->
    <!-- Note: Requires setting left/right margins in your item layout XML -->
    app:open_mz_mode="true"
    app:middle_page_cover="false"
    app:canLoop="true"
    
    <!-- 5. Meizu Style ViewPager (No auto-looping) -->
    app:open_mz_mode="true"
    app:canLoop="false"
    
    <!-- 6. Standard ViewPager -->
    app:open_mz_mode="false"
    app:canLoop="false"
  2. Install MZBannerView via JitPack

    master

    To use MZBannerView in your Android project, follow these two steps:

    1. Add the JitPack repository to your root build.gradle file:

    2. Add the dependency to your app-level build.gradle file.

    // Step 1: Root build.gradle
    allprojects {
         repositories {
              ...
              maven { url 'https://jitpack.io' }
         }
    }
    
    // Step 2: App build.gradle
    dependencies {
             compile 'com.github.pinguo-zhouwei:MZBannerView:v2.0.2'
    }
  3. Implement MZBannerView in your Activity

    master

    To use MZBannerView, define it in your XML layout and then initialize it in your Activity. You must provide a MZHolderCreator to handle data binding via a MZViewHolder implementation.

    // 1. XML Layout
    <com.zhouwei.mzbanner.MZBannerView
           android:id `@+id/banner"
           android:layout_width="match_parent"
           android:layout_height="200dp"
           app:open_mz_mode="true"
           app:canLoop="true"
           app:indicatorAlign="center"
           app:indicatorPaddingLeft="10dp"
           />
    
    // 2. Activity Implementation
    MZBannerView mMZBanner = (MZBannerView) view.findViewById(R.id.banner);
    
    // Set data
    MZBannerView.setPages(list, new MZHolderCreator<BannerViewHolder>() {
        @Override
        public BannerViewHolder createViewHolder() {
            return new BannerViewHolder();
        }
    });
    
    // 3. ViewHolder Implementation
    public static class BannerViewHolder implements MZViewHolder<Integer> {
        private ImageView mImageView;
    
        @Override
        public View createView(Context context) {
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item, null);
            mImageView = (ImageView) view.findViewById(R.id.banner_image);
            return view;
        }
    
        @Override
        public void onBind(Context context, int position, Integer data) {
            mImageView.setImageResource(data);
        }
    }
  4. Manage Banner lifecycle (start/pause)

    master

    If you are using MZBannerView as a scrolling Banner (where canLoop="true"), you must manage the auto-scrolling lifecycle in your Activity or Fragment to prevent unnecessary resource usage or crashes.

    • Call start() in onResume() to begin auto-scrolling.
    • Call pause() in onPause() to stop auto-scrolling.

    If you are using it as a standard ViewPager (canLoop="false"), these calls are not required.

    @Override
    public void onPause() {
        super.onPause();
        mMZBanner.pause(); // Stop auto-scrolling
    }
    
    @Override
    public void onResume() {
        super.onResume();
        mMZBanner.start(); // Start auto-scrolling
    }
  5. Configure MZBannerView via XML attributes

    master

    You can configure the behavior and appearance of MZBannerView directly in your layout XML using custom attributes.

    Important: All configuration items should be called or set before setPages() is invoked, otherwise they may be ineffective.