CircleIndicator在ViewPager中显示页码

时间:2016-01-28 07:05:19

标签: android android-fragments android-studio android-viewpager viewpagerindicator

我正在创建一个Activity,它使用ViewPager以幻灯片形式显示一组片段。要指出我正在使用的页面:https://github.com/ongakuer/CircleIndicator

这是活动的XML:

<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">

<android.support.v4.view.ViewPager
    android:id="@+id/introduction_view_pager"
    android:layout_weight="8"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>

<me.relex.circleindicator.CircleIndicator
    android:id="@+id/circle_indicator"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"/>

<Button
    android:layout_weight="1"
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="@string/skip_to_login"
    android:onClick="skipToLogin"/>

</LinearLayout>

这是活动的.java文件:

public class IntroductionActivity extends FragmentActivity {

static final int NO_OF_SLIDES = 4;
ViewPager mViewPager;
private PagerAdapter mPagerAdapter;
Button mButton;
CircleIndicator mCircleIndicator;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_introduction);

    mButton = (Button)findViewById(R.id.login);
    mCircleIndicator = (CircleIndicator)findViewById(R.id.circle_indicator);
    mViewPager = (ViewPager)findViewById(R.id.introduction_view_pager);
    mPagerAdapter = new SlideScreenPagerAdapter(getSupportFragmentManager());
    mViewPager.setAdapter(mPagerAdapter);
    mCircleIndicator.setViewPager(mViewPager);
    mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
}

@Override
public void onBackPressed() {
    if (mViewPager.getCurrentItem() == 0) {
        super.onBackPressed();
    } else {
        mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
    }
}
public class SlideScreenPagerAdapter extends FragmentPagerAdapter {


    public SlideScreenPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0 : return new IntroductionViewPagerFragmentOne();
            case 1 : return new IntroductionViewPagerFragmentTwo();
            case 2 : return new IntroductionViewPagerFragmentThree();
            case 3 : return new IntroductionViewPagerFragmentFour();
        }
        return null;
    }

    @Override
    public int getCount() {
        return NO_OF_SLIDES;
    }
}
}

片段完美无缺,但CircleIndicator没有显示。我如何获得CircleIndicator?

3 个答案:

答案 0 :(得分:1)

你可以使用https://github.com/JakeWharton/ViewPagerIndicator更强大的功能^^试试吧!

答案 1 :(得分:0)

使用此gradle依赖

'com.viewpagerindicator:parent:2.4.1'

答案 2 :(得分:0)

将下面的代码段粘贴到您的文件中(我使用了相同的圆圈指示符,它就像魅力一样)。

mViewPager.setOnPageChangeListener(this);

@Override
    public void onPageSelected(int position) {
        circleIndicator.onPageSelected(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <android.support.v4.view.ViewPager
        android:id="@+id/introduction_view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.8"
        android:layout_alignParentTop="true" />

    <me.relex.circleindicator.CircleIndicator
        android:id="@+id/circle_indicator"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1"
        android:background="@android:color/darker_gray" />

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.1"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:onClick="skipToLogin"
        android:text="login" />

</LinearLayout>