您的位置:首页技术文章
文章详情页

Android使用ViewPager完成app引导页

【字号: 日期:2022-09-21 15:30:31浏览:2作者:猪猪

本文实例为大家分享了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: android:layout_width='match_parent' android:layout_height='600dp' /> <LinearLayout android: 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>

Android使用ViewPager完成app引导页

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

标签: Android
相关文章: