Android使用ViewPager完成app引导页

时间:2021-05-20

本文实例为大家分享了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>

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章