在xml中写好布局
在activity中定义
private ViewPager viewpager;
viewpager = findView(R.id.viewpager);
设置监听
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 2) {
tvGuideSkip.setVisibility(View.GONE);
btnGuideEnter.setVisibility(View.VISIBLE);
} else {
tvGuideSkip.setVisibility(View.VISIBLE);
btnGuideEnter.setVisibility(View.GONE);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
ViewPager与layout.xml
新建Adapter 继承PagerAdapter
public class ViewPagerAdapter extends PagerAdapter {
private ArrayList views;
public ViewPagerAdapter(ArrayList views) {
super();
this.views = views;
}
@Override
public int getCount() {
return views.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(views.get(position));
return views.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(views.get(position));
}
}
在activity中使用
public class GuideActivity extends BaseActivity implements View.OnClickListener {
private ArrayList viewList;
private ViewPager viewpager;
private TextView tvGuideSkip;
private Button btnGuideEnter;
@Override
public void setStatusBar() {
super.setStatusBar();
StatusBarUtil.setTransparent(this);
}
@Override
public int getLayoutId() {
return R.layout.start_activity_guide;
}
@Override
public void initData() {
viewList = new ArrayList<>();
viewList.add(View.inflate(this, R.layout.start_guide_one, null));
viewList.add(View.inflate(this, R.layout.start_guide_two, null));
viewList.add(View.inflate(this, R.layout.start_guide_three, null));
}
@Override
public void initView() {
viewpager = findView(R.id.viewpager);
tvGuideSkip = findView(R.id.tv_guide_skip);
btnGuideEnter = findView(R.id.btn_guide_enter);
viewpager.setAdapter(new ViewPagerAdapter(viewList));
}
@Override
public void initListener() {
tvGuideSkip.setOnClickListener(this);
btnGuideEnter.setOnClickListener(this);
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 2) {
tvGuideSkip.setVisibility(View.GONE);
btnGuideEnter.setVisibility(View.VISIBLE);
} else {
tvGuideSkip.setVisibility(View.VISIBLE);
btnGuideEnter.setVisibility(View.GONE);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public void viewsClick(View view) {
int i = view.getId();
if (i == R.id.tv_guide_skip) {
CC.obtainBuilder("ComponentHome").setActionName("HomeActivity").build().call();
} else if (i == R.id.btn_guide_enter) {
CC.obtainBuilder("ComponentHome").setActionName("HomeActivity").build().call();
}
}
}