gpt4 book ai didi

Android ViewPager 点指示器不动

转载 作者:行者123 更新时间:2023-11-29 02:30:56 34 4
gpt4 key购买 nike

我正在为我的应用程序构建一个介绍 slider ,底部有一个 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>

幻灯片适配器.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);

关于Android ViewPager 点指示器不动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49653496/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com