1.不用导依赖
2.写布局
<?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:layout_height="match_parent"
tools:context=".MainActivity"
>
<android.support.v4.view.ViewPager
android:id="@+id/pag"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="73dp"
android:layout_weight="1"
android:text="进入" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_weight="2"/>
3.MainActivity代码
public class MainActivity extends AppCompatActivity {
private ViewPager pag;
private Button button;
private RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pag = findViewById(R.id.pag);
button = findViewById(R.id.button);
//跳转
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in=new Intent(MainActivity.this,Main2Activity.class);
startActivity(in);
}
});
rg = findViewById(R.id.rg);
//数组放图片
int arr[]={R.drawable.a1,R.drawable.a11,R.drawable.a2};
//定义list集合 for循环
final List<ImageView> list=new ArrayList<ImageView>();
for (int i=0;i<arr.length;i++){
//定义图片
ImageView imag=new ImageView(MainActivity.this);
imag.setImageResource(arr[i]);
list.add(imag);
//写小圆点
RadioButton rb=new RadioButton(MainActivity.this);
rg.addView(rb);
}
rg.check(rg.getChildAt(0).getId());
//适配器
pag.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view==o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
//设置图片
ImageView ima=list.get(position);
//添加图片布局
container.addView(ima);
return ima;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//删除布局
container.removeView((View) object);
}
});
pag.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
//判断隐藏按钮
if (i==list.size()-1) {
button.setVisibility(View.VISIBLE);
}
else {
button.setVisibility(View.GONE);
}
}
@Override
public void onPageSelected(int i) {
//选中页面
rg.check(rg.getChildAt(i).getId());
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
}