ViewpagerIndicator 的使用

本文介绍如何使用ViewPagerIndicator实现卡片切换效果,包括布局配置和Activity调用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、之前在项目中一直使用自己手动的写ViewPager引导页卡片切换,但是看到网易新闻等一些做的比较好的APK的卡片切换,使用到了ViewpagerIndicator 进行相关设置。在这里不得不多啰嗦几句那就是,之前在一些教学视频中有的讲解师就提到了,要避免重复创造代码,如果你觉得你的代码重构和一些相关逻辑处理的很好的话,我就不多讲了!所以说:不要重复创造代码,但这并不代表的是一味地拿来用,要更加深入的了解它的一些框架,切入重点。

在这个ViewpagerIndicator 当,我们需要导入 相关的library库。所以我们的下载这个Library 当中的 sample 当中包含了各种卡片样式的切换效果!具体的使用我们可以参照 sample 例子中的设置。那么Github地址:https://github.com/JakeWharton/ViewPagerIndicator

1、创建相关的 layout 文件 main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.wangly.project.helloworld.MainActivity">

    <TextView
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:text="ViewPagerIndicator(指示器)"
        android:layout_width="wrap_content"
        android:textSize="17sp"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView"
        android:layout_above="@+id/indicator">

    </android.support.v4.view.ViewPager>

    <com.wangly.project.helloworld.CirclePageIndicator
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:id="@+id/indicator" />

</RelativeLayout>

2、Activity调用的时候,即可使用如下:

public class MainActivity extends FragmentActivity {
    private  TestAdapter adapter;
    private  ViewPager viewPager;

    private  CirclePageIndicator mIndicator;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        adapter = new TestAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(viewPager);

    }

以上几步操作即可完成相关的操作!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值