Mobile Development 7 min read

SmartViewPager2Adapter: A Versatile ViewPager2 Adapter for Android

This article introduces the SmartViewPager2Adapter library, detailing its XML‑free setup, key features such as seamless data loading, gallery mode, infinite looping and auto‑scroll, and provides step‑by‑step Kotlin and Java code examples for integrating the adapter into Android projects.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
SmartViewPager2Adapter: A Versatile ViewPager2 Adapter for Android

The SmartViewPager2Adapter is an Android library that enables developers to create powerful ViewPager2 experiences without writing XML layouts, offering a concise API for common UI patterns.

Features

• Complete XML‑free usage • Two‑line code to achieve TikTok‑style list effects • Smooth, invisible data loading from top or bottom • Pre‑load listeners triggered at configurable limits • Gallery mode without needing clipChildren in XML • Infinite loop and auto‑scroll added in version 2.0

Demo & Real‑World Usage

Demo APK can be downloaded via QR code; screenshots and GIFs illustrate multi‑type fragment handling and gallery effects.

Dependency Setup

Add the JitPack repository and the library dependency:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    implementation 'com.github.lihangleo2:SmartViewPager2Adapter:1.0.2'
}

Basic Usage

1. Initialize the adapter

private val mAdapter by lazy {
    SmartViewPager2Adapter(this, mBinding.viewPager2)
        .cancleOverScrollMode()
        .setOffscreenPageLimit(5)
        .setPreLoadLimit(3)
        .addFragment(1, ImageFragment::class.java)
        .addFragment(2, TextFragment::class.java)
        .addData(list)
}

2. Attach to ViewPager2

mBinding.viewPager2.adapter = mAdapter

3. Provide data beans

Data beans must implement SmartFragmentTypeExEntity to supply a fragment type.

public class SourceBean implements SmartFragmentTypeExEntity {
    int type;
    @Override
    public int getFragmentType() {
        return type;
    }
}

4. Implement fragment classes

Fragments must implement SmartFragmentImpl to receive the bean.

public class ImageFragment extends Fragment implements SmartFragmentImpl {
    // ...
    @Override
    public void initSmartFragmentData(SmartFragmentTypeExEntity bean) {
        this.mSourceBean = (SourceBean) bean;
    }
}

Gallery Effect

Enable gallery mode with a single call, no XML changes required.

// Horizontal gallery
.asGallery(leftMargin, rightMargin)
// Apply transformer for visual effect
.setPagerTransformer(SmartTransformer.TRANSFORMER_ALPHA_SCALE)

Key Methods Overview

Name

Format

Description

.addData(List list)

void

Append data for downward loading

.addFrontData(List list)

void

Append data for upward loading

.addFragment(type, Fragment.class)

void

Register fragment class for a type

.setOnRefreshListener(...)

void

Header refresh callback

.setLoadMoreListener(...)

void

Footer load‑more callback

.finishRefreshWithNoMoreData()

void

Stop further header refreshes

.finishLoadMoreWithNoMoreData()

void

Stop further footer loads

.setPagerTransformer(SmartTransformer enum)

void

Apply 3D or alpha‑scale page transition

GitHub Repository

Find the source code and documentation at the SmartViewPager2Adapter GitHub page.

Other Works

Reference to additional utilities such as a universal shadow layout.

JavaandroidKotlinMobile UIViewPager2SmartViewPager2Adapter
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.