Android使用ViewPager完成app引导页

本文实例为大家分享了Android使用ViewPager完成app引导页的具体代码,供大家参考,具体内容如下

Android使用ViewPager完成app引导页_第1张图片

public class MainActivity extends AppCompatActivity {


//  int[] resourceId = {
//      R.layout.first,
//      R.layout.second,
//      R.layout.third
//  };
  List mListView;
  ViewPager viewPager;
  ViewGroup dotGroup;
  List dotImageView = new ArrayList<>();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewPager = findViewById(R.id.main_vp);
    dotGroup = findViewById(R.id.dot_layout);

    mListView = new ArrayList<>();
    for (int index = 0; index < 3; index++) {
      ImageView imageView = new ImageView(this);
      imageView.setImageResource(R.mipmap.ic_launcher_round);
      mListView.add(imageView);

      ImageView dot = new ImageView(this);
      dot.setImageResource(R.mipmap.cc_dot_o);
      dot.setMaxHeight(100);
      dot.setMaxWidth(100);

      LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(60, 60);
      layoutParams.leftMargin = 85;
      dot.setLayoutParams(layoutParams);
      dot.setEnabled(false);
      dotGroup.addView(dot);
      dotImageView.add(dot);
    }

    myAdapter adapter = new myAdapter();
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(1);
    setDotWindow(1);

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

      }

      @Override
      public void onPageSelected(int position) {
        //回调这个方法
        setDotWindow(position);
      }

      @Override
      public void onPageScrollStateChanged(int state) {

      }
    });

  }

  private void setDotWindow(int position) {
    for (int index = 0; index < dotImageView.size(); index++) {
      dotImageView.get(index).setImageResource(index == position ? R.mipmap.cc_dot : R.mipmap.cc_dot_o);
    }
  }

  class myAdapter extends PagerAdapter {

    @Override
    public int getCount() {
      return mListView.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
      return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
      View child = mListView.get(position);
      container.addView(child);
      return child;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
      container.removeView(mListView.get(position));
    }
  }

}

xml




  

  

  

Android使用ViewPager完成app引导页_第2张图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Android使用ViewPager完成app引导页)