ViewPager的简单使用
来源:互联网 发布:端口号80 编辑:程序博客网 时间:2024/04/19 16:04
极客学院的总结,用于引导页
Guide 的activity代码
public class Guide extends AppCompatActivity implements ViewPager.OnPageChangeListener { private ViewPager viewPager; private ViewPagerAdapter viewPagerAdapter; private List<View> views; private Button btn_enter; private ImageView[] dots; private int[] ids = {R.id.iv_a, R.id.iv_b, R.id.iv_c}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_guide); initView(); initDots(); } private void initView() { LayoutInflater inflater = LayoutInflater.from(this); views = new ArrayList<View>(); views.add(inflater.inflate(R.layout.one, null)); views.add(inflater.inflate(R.layout.two, null)); views.add(inflater.inflate(R.layout.three, null)); viewPager = (ViewPager) findViewById(R.id.vp_guide); viewPagerAdapter = new ViewPagerAdapter(views, this); viewPager.setAdapter(viewPagerAdapter); viewPager.setOnPageChangeListener(this); btn_enter = (Button)views.get(2).findViewById(R.id.enter); btn_enter.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Guide.this, MainActivity.class); startActivity(intent); } }); } private void initDots() { dots = new ImageView[views.size()]; for(int i =0;i<views.size();i++) { dots[i] = (ImageView) findViewById(ids[i]); } } @Override public void onPageScrolled(int i, float v, int i1) { } @Override public void onPageSelected(int pos) { for(int i=0;i<views.size();i++){ if (pos==i){ dots[i].setImageResource(R.mipmap.login_point_selected); }else { dots[i].setImageResource(R.mipmap.login_point); } } } @Override public void onPageScrollStateChanged(int i) { }}
它的xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/vp_guide" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00000000"></android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/ll_points" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center_horizontal" android:orientation="horizontal"> <ImageView android:id="@+id/iv_a" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/login_point_selected" /> <ImageView android:id="@+id/iv_b" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/login_point" /> <ImageView android:id="@+id/iv_c" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/login_point" /> </LinearLayout></RelativeLayout>
welcome的activity代码
public class welcome extends AppCompatActivity { private static final int time = 2000; private static final int GoHome = 0; private static final int GoGuide = 1; private boolean isFirstIn = false; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case GoHome: goHome(); break; case GoGuide: goGuide(); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); init(); } private void init() { SharedPreferences sp = getSharedPreferences("beibei", MODE_PRIVATE); isFirstIn = sp.getBoolean("isFirstIn", true); if (isFirstIn) { mHandler.sendEmptyMessageDelayed(GoGuide, time); sp.edit().putBoolean("isFirstIn", false).commit(); } else { mHandler.sendEmptyMessageDelayed(GoHome, time); } } private void goGuide() { Intent intent = new Intent(welcome.this, Guide.class); startActivity(intent); finish(); } private void goHome() { Intent intent = new Intent(welcome.this, MainActivity.class); startActivity(intent); finish(); }}
它的xml布局,就一张图片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:id="@+id/iv_welcome" android:background="@mipmap/welcome_android" android:layout_height="match_parent" /></LinearLayout>
MainActivity的代码无,显示个helloworld即可
viewPagerAdapter的代码 ,用来存所有View
public class ViewPagerAdapter extends PagerAdapter { private List<View> views;//想法:用于承载所有的view private Context context; public ViewPagerAdapter(List<View> views, Context context) { this.views = views; this.context = context; } @Override public int getCount() { return views.size(); } @Override public boolean isViewFromObject(View view, Object o) { return view==o; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(views.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(views.get(position)); return views.get(position); }}在此仅列一个view的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/iv_one" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/guide_1" /></LinearLayout>第三个布局加一按钮
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/iv_three" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/guide_3" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center_horizontal"> <Button android:id="@+id/enter" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="进入" android:textSize="14sp" /> </LinearLayout></RelativeLayout>
0 0
- ViewPager的简单使用
- viewpager的简单使用
- viewpager的简单使用
- ViewPager的简单使用
- ViewPager的简单使用
- viewpager的简单使用
- viewPager的简单使用
- ViewPager的简单使用
- ViewPager的简单使用
- Viewpager的简单使用
- ViewPager的简单使用
- 简单的ViewPager使用
- ViewPager的简单使用
- ViewPager的简单使用
- ViewPager的简单使用
- ViewPager的简单使用
- ViewPager的简单使用
- ViewPager的简单使用
- 打造属于自己的稀缺性
- 当Linux开启SeLinux后的常见权限问题
- 1025. PAT Ranking (25)
- Ant中的java
- 【数据结构与算法】(二) c 语言链表的简单操作
- ViewPager的简单使用
- 记2016年3月的面试
- java的(PO,VO,TO,BO,DAO,POJO)解释
- final关键字的使用
- 分类问题之K-近邻算法
- 一个岔路口分别通向诚实国和说谎国。来了两个人,已知一个是诚实国的,另一个是说谎国的,诚实国永远说实话,说谎国永远说谎话。现在你要去说谎国,但不知道应该走哪条路,需要问这两个人。请问应该怎么问?
- Assigning retained object to weak property object will be released after assignment
- php 读取 csv 文件的两种方法
- interview sum