一,在布局中引入控件ViewPager和一个按钮
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp_show"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="600dp"
android:text="完成"
android:visibility="invisible" />先将按钮设置为隐藏
二,在Activity中添加布局
final ArrayList views =new ArrayList<>();
View view = LayoutInflater.from(this).inflate(R.layout.item_pop,null);
for (int i =1; i <4; i++) {
views.add(view);
}
三,适配器(继承PagerAdapter)
private Contextcontext;
private ArrayListviews;
public VpAdapter(Context context, ArrayList views) {
this.context = context;
this.views = views;
}
@Override
public int getCount() {
return views.size();
}
@Override
public boolean isViewFromObject(@NonNull View view,@NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container,int position) {
View view = LayoutInflater.from(context).inflate(R.layout.item_pop,null);
TextView tv_num = view.findViewById(R.id.tv_num);
tv_num.setText((position +1) +"/" +views.size());
container.addView(view);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container,int position,@NonNull Object object) {
container.removeView((View) object);
}
}
四,绑定适配器
VpAdapter adapter =new VpAdapter(this, views);
vp_show.setAdapter(adapter);
五,设置监听方法判断是否到了最后一个页面
vp_show.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (views.size() -1 == position) {
btn_next.setVisibility(View.VISIBLE);//显示
}else {
btn_next.setVisibility(View.GONE);//隐藏
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
六,给按钮设置监听跳转到指定页面
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_next:
Intent intent =new Intent(ShowActivity.this, DataActivity.class);
startActivity(intent);
break;
}
}