package com.example.zhouone;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import java.util.List;
public class GuideAdapter extends PagerAdapter {
private List<View> lists;
public GuideAdapter(List<View> lists) {
this.lists = lists;
}
@Override
public int getCount() {
return lists.size();
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(lists.get(position),0);
return lists.get(position);
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(lists.get(position));
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
}
package com.example.zhouone;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
private ViewPager vp;
private List<View> lists = new ArrayList<>();
private Button bt;
private GuideAdapter guideAdapter;
private int[] res = {
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher
};
private ImageView[] image = new ImageView[res.length];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initDatas();
initEvents();
}
private void initEvents() {
vp.setOnPageChangeListener(this);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
}
private void initDatas() {
selectedDoto(0);
guideAdapter = new GuideAdapter(lists);
vp.setAdapter(guideAdapter);
}
private void selectedDoto(int index) {
for (int j = 0; j < res.length; j++) {
if (j==index){
image[j].setSelected(true);
}else{
image[j].setSelected(false);
}
}
}
private void initViews() {
vp = findViewById(R.id.vp);
bt =findViewById(R.id.btn);
image[0] = findViewById(R.id.cri1);
image[1] = findViewById(R.id.cri2);
image[2] = findViewById(R.id.cri3);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewPager.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for (int i = 0;i<res.length;i++){
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(layoutParams);
imageView.setImageResource(res[i]);
lists.add(imageView);
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == res.length-1){
bt.setVisibility(View.VISIBLE);
}else{
bt.setVisibility(View.GONE);
}
selectedDoto(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="跳转"
android:layout_alignParentRight="true"
/>
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="24dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:src="@drawable/selector_circle"
android:id="@+id/cri1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:src="@drawable/selector_circle"
android:id="@+id/cri2"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:src="@drawable/selector_circle"
android:id="@+id/cri3"/>
</LinearLayout>
</RelativeLayout>