### Basic MagicIndicator Setup with CommonNavigator Source: https://context7.com/hackware1993/magicindicator/llms.txt Configure a MagicIndicator by creating a CommonNavigator, setting up its adapter to define titles and indicators, and then attaching it to the MagicIndicator. Finally, bind it to a ViewPager for synchronization. ```java // Basic setup in Activity or Fragment MagicIndicator magicIndicator = findViewById(R.id.magic_indicator); // Create and configure a navigator CommonNavigator commonNavigator = new CommonNavigator(this); commonNavigator.setAdapter(new CommonNavigatorAdapter() { @Override public int getCount() { return titleList.size(); } @Override public IPagerTitleView getTitleView(Context context, int index) { SimplePagerTitleView titleView = new SimplePagerTitleView(context); titleView.setText(titleList.get(index)); titleView.setNormalColor(Color.GRAY); titleView.setSelectedColor(Color.BLACK); return titleView; } @Override public IPagerIndicator getIndicator(Context context) { LinePagerIndicator indicator = new LinePagerIndicator(context); indicator.setColors(Color.BLUE); return indicator; } }); // Attach navigator to MagicIndicator magicIndicator.setNavigator(commonNavigator); // Bind to ViewPager for automatic synchronization ViewPagerHelper.bind(magicIndicator, viewPager); ``` -------------------------------- ### Custom Indicator Implementation (Java) Source: https://context7.com/hackware1993/magicindicator/llms.txt Implement IPagerIndicator to create custom indicator effects. Use onPositionDataProvide to get title positions and onPageScrolled to animate the indicator based on scroll progress. Remember to call invalidate() to redraw. ```java public class MyCustomIndicator extends View implements IPagerIndicator { private List positionDataList; private float currentX; private Paint paint; public MyCustomIndicator(Context context) { super(context); paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.BLUE); } @Override public void onPositionDataProvide(List dataList) { // Called once when layout is complete with position data for all titles // PositionData contains: mLeft, mTop, mRight, mBottom, // mContentLeft, mContentTop, mContentRight, mContentBottom positionDataList = dataList; } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // Called continuously during page scroll // position: current page index // positionOffset: 0.0f to 1.0f scroll progress if (positionDataList == null || positionDataList.isEmpty()) return; PositionData current = positionDataList.get(position); PositionData next = positionDataList.get(Math.min(position + 1, positionDataList.size() - 1)); // Interpolate position float currentCenter = (current.mLeft + current.mRight) / 2f; float nextCenter = (next.mLeft + next.mRight) / 2f; currentX = currentCenter + (nextCenter - currentCenter) * positionOffset; invalidate(); } @Override public void onPageSelected(int position) { // Called when page selection changes } @Override public void onPageScrollStateChanged(int state) { // Called when scroll state changes // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING } @Override protected void onDraw(Canvas canvas) { // Draw your custom indicator canvas.drawCircle(currentX, getHeight() / 2f, 10, paint); } } ``` -------------------------------- ### Configure CommonNavigator Source: https://context7.com/hackware1993/magicindicator/llms.txt Set up a CommonNavigator with various configuration options for tab behavior, indicator positioning, and animations. Requires a MagicIndicator instance and a ViewPager. ```java MagicIndicator magicIndicator = findViewById(R.id.magic_indicator); magicIndicator.setBackgroundColor(Color.WHITE); CommonNavigator navigator = new CommonNavigator(this); // Configuration options avigator.setAdjustMode(false); // false = scrollable, true = fixed width tabs avigator.setScrollPivotX(0.5f); // Scroll center point (0.0-1.0) avigator.setFollowTouch(true); // Indicator follows finger during swipe avigator.setSmoothScroll(true); // Smooth scroll animation avigator.setSkimOver(true); // Show "skim over" effect for multi-page jumps avigator.setIndicatorOnTop(false); // Indicator behind titles (false) or on top (true) avigator.setLeftPadding(50); // Left padding in pixels avigator.setRightPadding(50); // Right padding in pixels avigator.setAdapter(new CommonNavigatorAdapter() { @Override public int getCount() { return dataList.size(); } @Override public IPagerTitleView getTitleView(Context context, int index) { ColorTransitionPagerTitleView titleView = new ColorTransitionPagerTitleView(context); titleView.setText(dataList.get(index)); titleView.setNormalColor(Color.GRAY); titleView.setSelectedColor(Color.BLACK); titleView.setOnClickListener(v -> viewPager.setCurrentItem(index)); return titleView; } @Override public IPagerIndicator getIndicator(Context context) { LinePagerIndicator indicator = new LinePagerIndicator(context); indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT); indicator.setColors(Color.BLUE); return indicator; } @Override public float getTitleWeight(Context context, int index) { return 1.0f; // Used in adjustMode for weighted distribution } }); magicIndicator.setNavigator(navigator); ViewPagerHelper.bind(magicIndicator, viewPager); ``` -------------------------------- ### Configure CircleNavigator Source: https://context7.com/hackware1993/magicindicator/llms.txt Initialize and configure the CircleNavigator, a standalone component. Set the number of circles, colors, radius, spacing, stroke width, and touch behavior. Bind it to a MagicIndicator and ViewPager. ```java MagicIndicator magicIndicator = findViewById(R.id.magic_indicator); CircleNavigator circleNavigator = new CircleNavigator(this); circleNavigator.setCircleCount(pageCount); // Number of circles circleNavigator.setCircleColor(Color.parseColor("#4CAF50")); circleNavigator.setRadius(UIUtil.dip2px(this, 4)); // Circle radius circleNavigator.setCircleSpacing(UIUtil.dip2px(this, 12)); // Space between circles circleNavigator.setStrokeWidth(UIUtil.dip2px(this, 1)); // Outline stroke width circleNavigator.setFollowTouch(true); // Indicator follows swipe // Enable touch interaction circleNavigator.setTouchable(true); circleNavigator.setCircleClickListener(index -> { viewPager.setCurrentItem(index); }); // Set directly as navigator (not through CommonNavigator) magicIndicator.setNavigator(circleNavigator); ViewPagerHelper.bind(magicIndicator, viewPager); ``` -------------------------------- ### Initialize MagicIndicator with CommonNavigator Source: https://github.com/hackware1993/magicindicator/blob/main/README.md Programmatically initialize the MagicIndicator by finding it in the layout, creating a CommonNavigator, and setting up its adapter. This involves defining the number of titles, creating custom title views, and specifying the indicator type. ```java MagicIndicator magicIndicator = (MagicIndicator) findViewById(R.id.magic_indicator); CommonNavigator commonNavigator = new CommonNavigator(this); commonNavigator.setAdapter(new CommonNavigatorAdapter() { @Override public int getCount() { return mTitleDataList == null ? 0 : mTitleDataList.size(); } @Override public IPagerTitleView getTitleView(Context context, final int index) { ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context); colorTransitionPagerTitleView.setNormalColor(Color.GRAY); colorTransitionPagerTitleView.setSelectedColor(Color.BLACK); colorTransitionPagerTitleView.setText(mTitleDataList.get(index)); colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mViewPager.setCurrentItem(index); } }); return colorTransitionPagerTitleView; } @Override public IPagerIndicator getIndicator(Context context) { LinePagerIndicator indicator = new LinePagerIndicator(context); indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT); return indicator; } }); magicIndicator.setNavigator(commonNavigator); ``` -------------------------------- ### Implement IPagerTitleView for custom tabs Source: https://github.com/hackware1993/magicindicator/blob/main/README.md Create a custom view by extending View and implementing the IPagerTitleView interface to handle tab state changes. ```java public class MyPagerTitleView extends View implements IPagerTitleView { public MyPagerTitleView(Context context) { super(context); } @Override public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) { } @Override public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) { } @Override public void onSelected(int index, int totalCount) { } @Override public void onDeselected(int index, int totalCount) { } } ``` -------------------------------- ### MIT License Source: https://github.com/hackware1993/magicindicator/blob/main/README.md The software is licensed under the MIT License. ```text MIT License Copyright (c) 2016 hackware1993 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ``` -------------------------------- ### Configure SimplePagerTitleView Source: https://context7.com/hackware1993/magicindicator/llms.txt Create and customize a SimplePagerTitleView for basic text-based titles. Set text, size, normal and selected colors, and an click listener for page navigation. ```java @Override public IPagerTitleView getTitleView(Context context, int index) { SimplePagerTitleView titleView = new SimplePagerTitleView(context); titleView.setText(titles.get(index)); titleView.setTextSize(16); titleView.setNormalColor(Color.parseColor("#333333")); titleView.setSelectedColor(Color.parseColor("#e94220")); titleView.setOnClickListener(v -> viewPager.setCurrentItem(index)); return titleView; } ``` -------------------------------- ### Implement IPagerIndicator for custom indicators Source: https://github.com/hackware1993/magicindicator/blob/main/README.md Create a custom indicator by extending View and implementing the IPagerIndicator interface to handle scroll and selection events. ```java public class MyPagerIndicator extends View implements IPagerIndicator { public MyPagerIndicator(Context context) { super(context); } @Override public void onPageSelected(int position) { } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(int state) { } @Override public void onPositionDataProvide(List dataList) { } } ``` -------------------------------- ### Implement FragmentContainerHelper in an Activity Source: https://context7.com/hackware1993/magicindicator/llms.txt Integrates MagicIndicator with a Fragment container by manually handling fragment show/hide transactions and attaching the helper. ```java public class FragmentActivity extends AppCompatActivity { private List fragments = new ArrayList<>(); private FragmentContainerHelper fragmentContainerHelper = new FragmentContainerHelper(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_layout); // Initialize fragments for (String title : titles) { fragments.add(MyFragment.newInstance(title)); } // Setup MagicIndicator MagicIndicator magicIndicator = findViewById(R.id.magic_indicator); CommonNavigator navigator = new CommonNavigator(this); navigator.setAdapter(new CommonNavigatorAdapter() { @Override public int getCount() { return titles.length; } @Override public IPagerTitleView getTitleView(Context context, int index) { SimplePagerTitleView titleView = new SimplePagerTitleView(context); titleView.setText(titles[index]); titleView.setOnClickListener(v -> { fragmentContainerHelper.handlePageSelected(index); switchFragment(index); }); return titleView; } @Override public IPagerIndicator getIndicator(Context context) { LinePagerIndicator indicator = new LinePagerIndicator(context); indicator.setColors(Color.RED); return indicator; } }); magicIndicator.setNavigator(navigator); // Attach to FragmentContainerHelper instead of ViewPager fragmentContainerHelper.attachMagicIndicator(magicIndicator); fragmentContainerHelper.setDuration(200); // Animation duration in ms // Show initial fragment fragmentContainerHelper.handlePageSelected(0, false); // false = no animation switchFragment(0); } private void switchFragment(int index) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); for (int i = 0; i < fragments.size(); i++) { Fragment fragment = fragments.get(i); if (i == index) { if (fragment.isAdded()) { transaction.show(fragment); } else { transaction.add(R.id.fragment_container, fragment); } } else if (fragment.isAdded()) { transaction.hide(fragment); } } transaction.commitAllowingStateLoss(); } } ``` -------------------------------- ### Custom Indicator Implementation Source: https://context7.com/hackware1993/magicindicator/llms.txt Implement the IPagerIndicator interface to create custom indicator animations and visual effects. ```APIDOC ## IPagerIndicator Interface - Custom Indicator Implementation Implement the `IPagerIndicator` interface to create custom indicator animations and visual effects. ```java public class MyCustomIndicator extends View implements IPagerIndicator { private List positionDataList; private float currentX; private Paint paint; public MyCustomIndicator(Context context) { super(context); paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.BLUE); } @Override public void onPositionDataProvide(List dataList) { // Called once when layout is complete with position data for all titles // PositionData contains: mLeft, mTop, mRight, mBottom, // mContentLeft, mContentTop, mContentRight, mContentBottom positionDataList = dataList; } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // Called continuously during page scroll // position: current page index // positionOffset: 0.0f to 1.0f scroll progress if (positionDataList == null || positionDataList.isEmpty()) return; PositionData current = positionDataList.get(position); PositionData next = positionDataList.get(Math.min(position + 1, positionDataList.size() - 1)); // Interpolate position float currentCenter = (current.mLeft + current.mRight) / 2f; float nextCenter = (next.mLeft + next.mRight) / 2f; currentX = currentCenter + (nextCenter - currentCenter) * positionOffset; invalidate(); } @Override public void onPageSelected(int position) { // Called when page selection changes } @Override public void onPageScrollStateChanged(int state) { // Called when scroll state changes // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING } @Override protected void onDraw(Canvas canvas) { // Draw your custom indicator canvas.drawCircle(currentX, getHeight() / 2f, 10, paint); } } ``` ``` -------------------------------- ### Implement BadgePagerTitleView Source: https://context7.com/hackware1993/magicindicator/llms.txt Wraps an existing title view to add a badge, with configurable positioning rules and auto-cancellation on selection. ```java @Override public IPagerTitleView getTitleView(Context context, int index) { BadgePagerTitleView badgeTitleView = new BadgePagerTitleView(context); // Set the inner title view (any IPagerTitleView implementation) SimplePagerTitleView innerTitleView = new SimplePagerTitleView(context); innerTitleView.setText(titles.get(index)); innerTitleView.setNormalColor(Color.GRAY); innerTitleView.setSelectedColor(Color.BLACK); badgeTitleView.setInnerPagerTitleView(innerTitleView); // Create badge view (custom layout or simple view) View badgeView = LayoutInflater.from(context).inflate(R.layout.badge_layout, null); TextView countView = badgeView.findViewById(R.id.count); countView.setText(String.valueOf(unreadCounts[index])); // Or simple red dot // View redDot = new View(context); // redDot.setBackgroundResource(R.drawable.red_dot); badgeTitleView.setBadgeView(badgeView); // Position the badge relative to title content badgeTitleView.setXBadgeRule(new BadgeRule(BadgeAnchor.CONTENT_RIGHT, UIUtil.dip2px(context, -5))); badgeTitleView.setYBadgeRule(new BadgeRule(BadgeAnchor.CONTENT_TOP, UIUtil.dip2px(context, 2))); // Auto-remove badge when tab is selected badgeTitleView.setAutoCancelBadge(true); badgeTitleView.setOnClickListener(v -> viewPager.setCurrentItem(index)); return badgeTitleView; } ``` -------------------------------- ### Configure TriangularPagerIndicator Source: https://context7.com/hackware1993/magicindicator/llms.txt Set up the TriangularPagerIndicator by configuring line properties, triangle dimensions, position offset, and animation interpolators. The 'reverse' property controls the triangle's orientation. ```java TriangularPagerIndicator indicator = new TriangularPagerIndicator(context); // Line configuration indicator.setLineColor(Color.parseColor("#e94220")); indicator.setLineHeight(UIUtil.dip2px(context, 2)); // Triangle configuration indicator.setTriangleWidth(UIUtil.dip2px(context, 14)); indicator.setTriangleHeight(UIUtil.dip2px(context, 8)); // Position offset indicator.setYOffset(UIUtil.dip2px(context, 0)); // Reverse mode - triangle points downward from top indicator.setReverse(true); // Animation interpolator indicator.setStartInterpolator(new LinearInterpolator()); // Use in adapter @Override public IPagerIndicator getIndicator(Context context) { TriangularPagerIndicator triangleIndicator = new TriangularPagerIndicator(context); triangleIndicator.setLineColor(Color.BLUE); return triangleIndicator; } ``` -------------------------------- ### Work with FragmentContainerHelper Source: https://github.com/hackware1993/magicindicator/blob/main/README.md Integrate MagicIndicator with a Fragment Container by using FragmentContainerHelper. This is used when switching Fragments using hide() and show() methods. Invoke handlePageSelected() when the Fragment changes. ```java mFramentContainerHelper = new FragmentContainerHelper(magicIndicator); // ... mFragmentContainerHelper.handlePageSelected(pageIndex); // invoke when switch Fragment ``` -------------------------------- ### Custom Title View Implementation Source: https://context7.com/hackware1993/magicindicator/llms.txt Implement the IPagerTitleView interface to create custom title views with full control over selection state and scroll animations. ```APIDOC ## IPagerTitleView Interface - Custom Title Implementation Implement the `IPagerTitleView` interface to create completely custom title views with full control over selection state and scroll animations. ```java public class MyCustomTitleView extends View implements IPagerTitleView { private String text; private float scale = 1.0f; private int textColor = Color.GRAY; public MyCustomTitleView(Context context) { super(context); } @Override public void onSelected(int index, int totalCount) { // Called when this title becomes fully selected textColor = Color.RED; scale = 1.2f; invalidate(); } @Override public void onDeselected(int index, int totalCount) { // Called when this title becomes fully deselected textColor = Color.GRAY; scale = 1.0f; invalidate(); } @Override public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) { // Called continuously during page scroll when leaving this title // leavePercent: 0.0f (just started leaving) to 1.0f (completely left) // leftToRight: true if scrolling from left to right scale = 1.2f - 0.2f * leavePercent; invalidate(); } @Override public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) { // Called continuously during page scroll when entering this title // enterPercent: 0.0f (just started entering) to 1.0f (completely entered) scale = 1.0f + 0.2f * enterPercent; invalidate(); } @Override protected void onDraw(Canvas canvas) { canvas.save(); canvas.scale(scale, scale, getWidth() / 2f, getHeight() / 2f); // Draw your custom content canvas.restore(); } } ``` ``` -------------------------------- ### Implement CommonPagerTitleView Source: https://context7.com/hackware1993/magicindicator/llms.txt Uses a custom XML layout for the title view and provides callbacks to handle selection state changes and scroll animations. ```java @Override public IPagerTitleView getTitleView(Context context, int index) { CommonPagerTitleView titleView = new CommonPagerTitleView(context); // Load custom layout titleView.setContentView(R.layout.custom_tab_layout); // Or set a view directly // titleView.setContentView(customView); // Get references to child views final ImageView iconView = titleView.findViewById(R.id.icon); final TextView textView = titleView.findViewById(R.id.text); textView.setText(titles.get(index)); iconView.setImageResource(icons[index]); // Handle state changes titleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() { @Override public void onSelected(int index, int totalCount) { textView.setTextColor(Color.RED); iconView.setColorFilter(Color.RED); } @Override public void onDeselected(int index, int totalCount) { textView.setTextColor(Color.GRAY); iconView.setColorFilter(Color.GRAY); } @Override public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) { // Handle leave animation based on percentage } @Override public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) { // Handle enter animation based on percentage } }); titleView.setOnClickListener(v -> viewPager.setCurrentItem(index)); return titleView; } ``` -------------------------------- ### Configure BezierPagerIndicator Source: https://context7.com/hackware1993/magicindicator/llms.txt Customize the BezierPagerIndicator with circle sizes, multi-color support, and custom interpolators for animation timing. Use this in your CommonNavigatorAdapter. ```java BezierPagerIndicator indicator = new BezierPagerIndicator(context); // Circle size configuration indicator.setMaxCircleRadius(UIUtil.dip2px(context, 4)); // Larger circle radius indicator.setMinCircleRadius(UIUtil.dip2px(context, 2)); // Smaller circle radius indicator.setYOffset(UIUtil.dip2px(context, 2)); // Offset from bottom // Multi-color support - cycles through colors as pages change indicator.setColors( Color.parseColor("#ff4a42"), // Red Color.parseColor("#fcde64"), // Yellow Color.parseColor("#73e8f4"), // Cyan Color.parseColor("#76b0ff"), // Blue Color.parseColor("#c683fe") // Purple ); // Custom interpolators for animation timing indicator.setStartInterpolator(new AccelerateInterpolator()); indicator.setEndInterpolator(new DecelerateInterpolator()); // Use in CommonNavigatorAdapter @Override public IPagerIndicator getIndicator(Context context) { BezierPagerIndicator bezierIndicator = new BezierPagerIndicator(context); bezierIndicator.setColors(Color.RED, Color.BLUE, Color.GREEN); return bezierIndicator; } ``` -------------------------------- ### LinePagerIndicator with MODE_EXACTLY Source: https://context7.com/hackware1993/magicindicator/llms.txt Configures a LinePagerIndicator with a fixed line width, height, and rounded corners. Allows for Y and X offsets and custom animation interpolators. Requires a context. ```java LinePagerIndicator indicator3 = new LinePagerIndicator(context); indicator3.setMode(LinePagerIndicator.MODE_EXACTLY); indicator3.setLineWidth(UIUtil.dip2px(context, 30)); indicator3.setLineHeight(UIUtil.dip2px(context, 6)); indicator3.setRoundRadius(UIUtil.dip2px(context, 3)); // Rounded corners indicator3.setYOffset(UIUtil.dip2px(context, 5)); // Offset from bottom indicator3.setXOffset(UIUtil.dip2px(context, 2)); // Horizontal offset from edges indicator3.setColors(Color.parseColor("#FF5722")); // Custom animation interpolators for elastic/bounce effects indicator3.setStartInterpolator(new AccelerateInterpolator()); indicator3.setEndInterpolator(new DecelerateInterpolator(2.0f)); ``` -------------------------------- ### Configure MagicIndicator in XML Source: https://context7.com/hackware1993/magicindicator/llms.txt Add MagicIndicator components to your layout file to define tab bars or dot indicators alongside a ViewPager. ```xml ``` -------------------------------- ### Implement ColorTransitionPagerTitleView Source: https://context7.com/hackware1993/magicindicator/llms.txt Configures a title view that interpolates colors between normal and selected states based on scroll percentage. ```java @Override public IPagerTitleView getTitleView(Context context, int index) { ColorTransitionPagerTitleView titleView = new ColorTransitionPagerTitleView(context); titleView.setText(dataList.get(index)); titleView.setNormalColor(Color.GRAY); titleView.setSelectedColor(Color.WHITE); titleView.setTextSize(14); titleView.setOnClickListener(v -> viewPager.setCurrentItem(index)); return titleView; } ``` -------------------------------- ### ViewPager Integration with ViewPagerHelper.bind() Source: https://context7.com/hackware1993/magicindicator/llms.txt Integrate MagicIndicator with a ViewPager using the ViewPagerHelper.bind() static method. This simplifies the process by automatically handling page scroll events and synchronization between the indicator and the ViewPager. ```xml // In layout XML // // // ``` ```java // In Activity onCreate ViewPager viewPager = findViewById(R.id.view_pager); viewPager.setAdapter(new MyPagerAdapter(titles, fragments)); MagicIndicator magicIndicator = findViewById(R.id.magic_indicator); CommonNavigator navigator = new CommonNavigator(this); navigator.setAdapter(myNavigatorAdapter); magicIndicator.setNavigator(navigator); // Single line binding - handles all scroll synchronization ViewPagerHelper.bind(magicIndicator, viewPager); ``` -------------------------------- ### Add MagicIndicator Dependency via Gradle Source: https://context7.com/hackware1993/magicindicator/llms.txt Include the MagicIndicator library in your Android project by adding the dependency to your app's build.gradle file. Ensure the JitPack repository is also configured. ```groovy // In settings.gradle or root build.gradle repositories { maven { url "https://jitpack.io" } } // In app/build.gradle dependencies { // For AndroidX projects implementation 'com.github.hackware1993:MagicIndicator:1.7.0' // For Support Library projects implementation 'com.github.hackware1993:MagicIndicator:1.6.0' } ``` -------------------------------- ### Bind MagicIndicator with ViewPager Source: https://github.com/hackware1993/magicindicator/blob/main/README.md Connect the initialized MagicIndicator with a ViewPager to synchronize scrolling and indicator updates. This is a convenient helper method. ```java ViewPagerHelper.bind(magicIndicator, mViewPager); ``` -------------------------------- ### Add MagicIndicator Dependency Source: https://github.com/hackware1993/magicindicator/blob/main/README.md Include the MagicIndicator library in your Android project using Gradle. Choose the appropriate dependency for support libraries or AndroidX. ```groovy implementation project(':magicindicator') ``` ```groovy repositories { ... maven { url "https://jitpack.io" } } dependencies { ... implementation 'com.github.hackware1993:MagicIndicator:1.6.0' // for support lib implementation 'com.github.hackware1993:MagicIndicator:1.7.0' // for androidx } ``` -------------------------------- ### Custom Title View Implementation (Java) Source: https://context7.com/hackware1993/magicindicator/llms.txt Implement IPagerTitleView to create custom title views. Override methods like onSelected, onDeselected, onLeave, and onEnter to control visual states and animations. Ensure to call invalidate() to trigger redraws. ```java public class MyCustomTitleView extends View implements IPagerTitleView { private String text; private float scale = 1.0f; private int textColor = Color.GRAY; public MyCustomTitleView(Context context) { super(context); } @Override public void onSelected(int index, int totalCount) { // Called when this title becomes fully selected textColor = Color.RED; scale = 1.2f; invalidate(); } @Override public void onDeselected(int index, int totalCount) { // Called when this title becomes fully deselected textColor = Color.GRAY; scale = 1.0f; invalidate(); } @Override public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) { // Called continuously during page scroll when leaving this title // leavePercent: 0.0f (just started leaving) to 1.0f (completely left) // leftToRight: true if scrolling from left to right scale = 1.2f - 0.2f * leavePercent; invalidate(); } @Override public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) { // Called continuously during page scroll when entering this title // enterPercent: 0.0f (just started entering) to 1.0f (completely entered) scale = 1.0f + 0.2f * enterPercent; invalidate(); } @Override protected void onDraw(Canvas canvas) { canvas.save(); canvas.scale(scale, scale, getWidth() / 2f, getHeight() / 2f); // Draw your custom content canvas.restore(); } } ``` -------------------------------- ### Dynamic Tab Updates Source: https://context7.com/hackware1993/magicindicator/llms.txt Update tabs dynamically by modifying the data source and notifying the adapter of changes. ```APIDOC ## Dynamic Tab Updates Update tabs dynamically by modifying the data source and notifying the adapter of changes. ```java // Add new tab dataList.add("New Tab"); commonNavigator.getAdapter().notifyDataSetChanged(); viewPager.getAdapter().notifyDataSetChanged(); // Remove tab dataList.remove(position); commonNavigator.getAdapter().notifyDataSetChanged(); viewPager.getAdapter().notifyDataSetChanged(); // Or call notifyDataSetChanged on navigator directly commonNavigator.notifyDataSetChanged(); ``` ``` -------------------------------- ### LinePagerIndicator with MODE_MATCH_EDGE Source: https://context7.com/hackware1993/magicindicator/llms.txt Configures a LinePagerIndicator where the line width matches the width of the title view. Requires a context. ```java LinePagerIndicator indicator1 = new LinePagerIndicator(context); indicator1.setMode(LinePagerIndicator.MODE_MATCH_EDGE); indicator1.setColors(Color.BLUE); ``` -------------------------------- ### Layout XML for MagicIndicator and ViewPager Source: https://github.com/hackware1993/magicindicator/blob/main/README.md Define the MagicIndicator and ViewPager components in your Android layout XML file. Ensure correct dimensions and orientation. ```xml ``` -------------------------------- ### Dynamic Tab Updates (Java) Source: https://context7.com/hackware1993/magicindicator/llms.txt Dynamically update tabs by modifying the data source and calling notifyDataSetChanged on the adapter. This is applicable for both the navigator's adapter and the ViewPager's adapter. ```java // Add new tab dataList.add("New Tab"); commonNavigator.getAdapter().notifyDataSetChanged(); viewPager.getAdapter().notifyDataSetChanged(); // Remove tab dataList.remove(position); commonNavigator.getAdapter().notifyDataSetChanged(); viewPager.getAdapter().notifyDataSetChanged(); // Or call notifyDataSetChanged on navigator directly commonNavigator.notifyDataSetChanged(); ``` -------------------------------- ### LinePagerIndicator with MODE_WRAP_CONTENT Source: https://context7.com/hackware1993/magicindicator/llms.txt Configures a LinePagerIndicator where the line width matches the content width of the title text. Supports color gradients between pages. Requires a context. ```java LinePagerIndicator indicator2 = new LinePagerIndicator(context); indicator2.setMode(LinePagerIndicator.MODE_WRAP_CONTENT); indicator2.setColors(Color.RED, Color.GREEN, Color.BLUE); // Color gradient between pages indicator2.setLineHeight(UIUtil.dip2px(context, 4)); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.