首次启动viewpager

xiaoxiao2021-03-01  14

public class MainActivity extends AppCompatActivity { //把图片存放到这个集合里边 private List<Integer> list=new ArrayList<>(); Button lj; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找空间 ViewPager mViewPager=findViewById(R.id.mViewPager); lj = findViewById(R.id.lj); //放里边 list.add(R.drawable.splash01); list.add(R.drawable.splash02); list.add(R.drawable.splash04); //适配器 MyAdpater myAdpater=new MyAdpater(); // SharedPreferences存储 SharedPreferences infor = getSharedPreferences("infor", MODE_PRIVATE); //存储一个布尔类型的值,进行判断 boolean b = infor.getBoolean("islogin", true); //如果为true,就实现那个点击按钮和适配器 if(b){ mViewPager.setAdapter(myAdpater); lj.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(MainActivity.this,Two.class)); finish(); } }); //最后提交一下 infor.edit().putBoolean("islogin",false).commit(); } else{ //否则直接跳转 startActivity(new Intent(MainActivity.this,Two.class)); finish(); } } //适配器 class MyAdpater extends PagerAdapter{ @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(MainActivity.this); imageView.setImageResource(list.get(position)); if(position==list.size()-1){ lj.setVisibility(View.VISIBLE); } else{ lj.setVisibility(View.GONE); } container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }
转载请注明原文地址: https://www.6miu.com/read-4050021.html

最新回复(0)