<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
/>
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
package lll.com.yk_moni02;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ViewPager pager;
private RadioGroup radioGroup;
private Button button;
private ArrayList<ImageView> list;
int array[] = {R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = findViewById(R.id.pager);
radioGroup = findViewById(R.id.radioGroup);
button = findViewById(R.id.button1);
//设置点击监听
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,TwoActivity.class));
}
});
list = new ArrayList<>();
for (int i=0;i<array.length;i++){
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(array[i]);
list.add(imageView);
}
//适配器
pager.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 view = list.get(position);
container.addView(view);
//判断是不是最后一个
if (position == list.size()-1){
button.setVisibility(View.VISIBLE);
}else {
button.setVisibility(View.GONE);
}
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
});
//小圆点
//距离
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//设置距离
params.leftMargin = 15;
for (int i=0; i<list.size(); i++){
RadioButton radioButton = new RadioButton(MainActivity.this);
if (i ==0){
radioGroup.addView(radioButton);
}else {
radioGroup.addView(radioButton,params);
}
}
//第一个选中
radioGroup.check(radioGroup.getChildAt(0).getId());
//联动
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
radioGroup.check(radioGroup.getChildAt(i).getId());
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
//点击切换
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
pager.setCurrentItem(checkedId -1);
}
});
}
}