Android使用ViewPager完成app引导页
作者:codeTcy
这篇文章主要为大家详细介绍了Android使用ViewPager完成app引导页,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Android使用ViewPager完成app引导页的具体代码,供大家参考,具体内容如下
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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。