Android欢迎滑动页面
一枚-狗子 人气:0一、效果图
二、源码
1.activity_welcome_guide.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" > <android.support.v4.view.ViewPager android:id="@+id/welcome_pager" android:layout_width="match_parent" android:layout_height="match_parent" /> <Button android:id="@+id/welcome_guide_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="80dp" android:background="@mipmap/guide_button" android:paddingLeft="30dp" android:paddingRight="30dp" android:visibility="gone" /> </RelativeLayout>
2.WelcomeGuideActivity.java
/** * 滑动页面 * * @author Guan * @file com.guan.dianping.activity * @date 2015/11/3 * @Version 1.0 */ public class WelcomeGuideActivity extends FrameActivity { @ViewInject(R.id.welcome_guide_btn) private Button btn; @ViewInject(R.id.welcome_pager) private ViewPager pager; private List<View> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome_guide); ViewUtils.inject(this); init(); initViewPager(); } @OnClick(R.id.welcome_guide_btn) public void click(View view) { //页面的跳转 startActivity(new Intent(getBaseContext(), MainActivity.class)); finish(); } //初始化 public void init() { list = new ArrayList<View>(); } //初始化ViewPager的方法 public void initViewPager() { ImageView iv1 = new ImageView(this); iv1.setImageResource(R.mipmap.guide_01); ImageView iv2 = new ImageView(this); iv2.setImageResource(R.mipmap.guide_02); ImageView iv3 = new ImageView(this); iv3.setImageResource(R.mipmap.guide_03); list.add(iv1); list.add(iv2); list.add(iv3); pager.setAdapter(new MyPagerAdapter()); //监听ViewPager滑动效果 pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { //页卡被选中的方法 @Override public void onPageSelected(int arg0) { //如果是第三个页面 if (arg0 == 2) { btn.setVisibility(View.VISIBLE); } else { btn.setVisibility(View.GONE); } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } //定义ViewPager的适配器 class MyPagerAdapter extends PagerAdapter { //计算需要多少item显示 @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } //初始化item实例方法 @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); return list.get(position); } //item销毁的方法 @Override public void destroyItem(ViewGroup container, int position, Object object) { // 注销父类销毁item的方法,因为此方法并不是使用此方法 // super.destroyItem(container, position, object); container.removeView(list.get(position)); } } }
加载全部内容