亲宝软件园·资讯

展开

Android使用ViewPager完成app引导页

codeTcy 人气:0

public class MainActivity extends AppCompatActivity {


//  int[] resourceId = {
//      R.layout.first,
//      R.layout.second,
//      R.layout.third
//  };
  List<View> mListView;
  ViewPager viewPager;
  ViewGroup dotGroup;
  List<ImageView> 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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <androidx.viewpager.widget.ViewPager
    android:id="@+id/main_vp"
    android:layout_width="match_parent"
    android:layout_height="600dp" />

  <LinearLayout
    android:id="@+id/dot_layout"
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:layout_centerInParent="true"
    android:layout_marginBottom="30dp"
    android:orientation="horizontal">

  </LinearLayout>

</RelativeLayout>

加载全部内容

相关教程
猜你喜欢
用户评论