zoukankan      html  css  js  c++  java
  • viewPager-基本实现示例

    直接看代码

    package com.example.myviewpager;
    
    import android.app.Activity;
    import android.os.Bundle;
    import java.util.ArrayList;
    import java.util.List;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.Window;
    import android.widget.ImageView;
    import android.widget.ImageView.ScaleType;
    
    public class MainActivity extends Activity {
        private ViewPager mViewPager;
        private int[] mImgIds = new int[] { R.drawable.background_portrait,
                R.drawable.background, R.drawable.bg_vidcontrol };
        private List<ImageView> mImageViews = new ArrayList<ImageView>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
    
            initData();
    
            mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
    
            mViewPager.setAdapter(new PagerAdapter() {
                @Override
                public Object instantiateItem(ViewGroup container, int position) {
    
                    container.addView(mImageViews.get(position));
                    return mImageViews.get(position);
                }
    
                @Override
                public void destroyItem(ViewGroup container, int position,
                        Object object) {
    
                    container.removeView(mImageViews.get(position));
                }
    
                @Override
                public boolean isViewFromObject(View view, Object object) {
                    return view == object;
                }
    
                @Override
                public int getCount() {
                    return mImgIds.length;
                }
            });
            mViewPager.setPageTransformer(true, new DepthPageTransformer());
    
        }
    
        private void initData() {
            for (int imgId : mImgIds) {
                ImageView imageView = new ImageView(getApplicationContext());
                imageView.setScaleType(ScaleType.CENTER_CROP);
                imageView.setImageResource(imgId);
                mImageViews.add(imageView);
            }
        }
        
    }

    他的使用非常简单,就跟listview类似,主要使用pagerAdapter添加item就行了

  • 相关阅读:
    写给自己的话
    软件开发之技能梳理
    《创新者的窘境》读书笔记
    我的四年踩坑史以及思考
    认识问题和求解问题的一种思考框架
    《时间的秩序》读书笔记
    从JSON中自动生成对应的对象模型
    考考你:一道题引发的小思考
    哈!如果一生只如花样短暂
    使用正则表达式抽取所需文本
  • 原文地址:https://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_exam_150331102.html
Copyright © 2011-2022 走看看