Android ViewPager 点指示器没有移动

Android ViewPager's dots indictor is not moving

我正在为我的应用程序构建一个介绍滑块,底部有一个 viewpager 和点指示器,带有两个用于登录和登录的按钮,首先我的点数有误;每次我滑动时它们都会成倍增加,然后当我滑动时点不会移动。

activity_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_intro"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<LinearLayout
    android:id="@+id/layoutDots"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="80dp"
    android:orientation="horizontal"></LinearLayout>

<Button
    android:id="@+id/btn_login"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_toEndOf="@+id/btn_join"
    android:layout_toRightOf="@+id/btn_join"
    android:background="#C1403D"
    android:onClick="btnLoginClick"
    android:text="Se connecter"
    android:textColor="@android:color/white" />

<Button
    android:id="@+id/btn_join"
    android:layout_width="193dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="#303952"
    android:onClick="btnJoinClick"
    android:text="S'enregistrer"
    android:textColor="@android:color/white" />

</RelativeLayout>

slide_layout.xml :

简单的幻灯片代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_screen">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/slide_heading"
        android:text="header"
        android:textAlignment="center"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="@dimen/slide_title"
        android:textStyle="bold" />

    <TextView
        android:text="Description"
        android:id="@+id/slide_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:paddingLeft="@dimen/desc_padding"
        android:paddingRight="@dimen/desc_padding"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="17dp" />

</LinearLayout>
</RelativeLayout>

SlidesAdapter.java :

public class SlidesAdapter extends PagerAdapter {

    Context context;

    public SlidesAdapter(Context context) {
        this.context = context;
    }

    public SlidesAdapter() {
    }

    public String [] slide_headings= {
            "Connectez-vous",
            "Gérez vos tâches rapidement",
            "Suivez vos progrés facilement",
            "Accéder aux services de votre université"
    };

    public String [] slide_descriptions = {
            "à \"app name\"",
            "Organisez votre travail et vos études et controlez les\n" +
                    "grâce à un agenda facile à utiliser",
            "Enregistrer les notes de matières et suivez vos progrés",
            "Découvrez les clubs, bibliothéques et les services de votre université"
    };

    @Override
    public int getCount() {
        return slide_headings.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object obj) {
        return view == obj;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

        View view = layoutInflater.inflate(R.layout.slide_layout, container, false);

        TextView slideHeading = (TextView) view.findViewById(R.id.slide_heading);
        TextView slideDescription = (TextView) view.findViewById(R.id.slide_description);

        slideHeading.setText(slide_headings[position]);
        slideDescription.setText(slide_descriptions[position]);

        ViewPager vp = (ViewPager) container;
        container.addView(view,0);

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ViewPager vp = (ViewPager) container;
        View view = (View) object;
        vp.removeView(view);
    }
}

MainActivity.java:

public class MainActivity extends Activity {

    private ViewPager viewPager;
    private SlidesAdapter slidesAdapter;
    private LinearLayout dotsLayout;
    private TextView[] dots;
    private int[] layouts;
    private Button btnLogin, btnJoin;

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

        viewPager =  findViewById(R.id.view_pager);
        dotsLayout = findViewById(R.id.layoutDots);
        btnLogin = findViewById(R.id.btn_login);
        btnJoin = findViewById(R.id.btn_join);

        slidesAdapter = new SlidesAdapter(this);
        viewPager.setAdapter(slidesAdapter);

        addBottomDots(0);

        viewPager.addOnPageChangeListener(viewListener);

    }

    private void addBottomDots(int position) {

        dots = new TextView[4];

        for (int i = 0; i < dots.length; i++) {
            dots[i] = new TextView(this);
            dots[i].setText(Html.fromHtml("&#8226;"));
            dots[i].setTextSize(35);
            dots[i].setTextColor(getResources().getColor(R.color.dot_inactive));
            dotsLayout.addView(dots[i]);
        }

        if(dots.length>0){
          dots[position].setTextColor(getResources().getColor(R.color.dot_active));
        }
    }

    ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    };

}

One way could be that you use circle pager indicator instead of manually adding dots.

在你的 gradle 添加:

implementation 'fr.avianey.com.viewpagerindicator:library:2.4.1@aar'

activity_main.xml :

   <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/btn_login"
        >
    </android.support.v4.view.ViewPager>

    <com.viewpagerindicator.CirclePageIndicator
        android:id="@+id/circle_page_indicator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/_8sdp"
        android:layout_above="@+id/btn_login"
        app:radius="4dp"/>

然后在你的MainActivity.java

slidesAdapter = new SlidesAdapter(this);
viewPager.setAdapter(slidesAdapter);
mPageIndicator = (CirclePageIndicator) findViewById(R.id.circle_page_indicator);
mPageIndicator.setViewPager(mVwPager);

从 onPageSelected() 调用此方法

 private void activateDot(int position) {
        for (int i = 0; i < dots.length; i++) {
            if(position == i) {
                dots[i].setTextColor(getResources().getColor(R.color.dot_active));
            }else {
                dots[i].setTextColor(getResources().getColor(R.color.dot_inactive));
            }
        }
    }

https://github.com/tizisdeepan/dots

在您的应用模块 gradle 文件中

dependencies {
    implementation 'com.github.tizisdeepan:dots:1.0.1'
}

在你的项目级别gradle文件

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

在你身上使用DotsIndicatorlayout.xml

<com.make.dots.dotsindicator.DotsIndicator
    android:id="@+id/dotsIndicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:background="@drawable/dots_background"
    android:padding="6dp"
    app:dot_drawable="@drawable/ic_dot_darkgrey"
    app:dot_drawable_unselected="@drawable/ic_dot_lightgrey"
    app:dot_height="8dp"
    app:dot_margin="6dp"
    app:dot_width="8dp" />

Link 在您的 Java / Kotlin 代码中使用点指示器查看寻呼机

dotsIndicator.setViewPager(imageViewPager)
imageViewPager.adapter?.registerDataSetObserver(dotsIndicator.dataSetObserver)

文档

XML

dot_drawable: 用于指示当前选中页面的drawable dot_drawable_unselected: 用于指示未选中页面的Drawable dot_height:Dot 指示器的高度(以 dp 为单位) dot_width:dp 中点指示器的宽度 dot_margin: 点之间的边缘空间 科特林/Java

// 这将 DotsIndicator 附加到 ViewPager

DotsIndicator.setViewPager(ViewPager)

瞧!您现在已经为您的 View Pager 实现了一个简单的点指示器!