Viewpager

4918 ワード

ublic class MainActivity extends AppCompatActivity{//ViewPagerをグローバル変数として定義して使いやすい.private ViewPager view pager_vp;//ArrayListのセットを作成する.汎用はImageView.private ArrayList ImageView=new ArrayList();//図片int配列リソースprivate int[]imageResIds={ R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, };//画像文字列配列String[]リソース.private String[]descs={"巩俐不低俗,我就不能低俗","扑树又回来了!再唱経典老歌引万人大合唱","揭秘北京映画怎么升级","楽视网TV版大派送","热血キックアスの反杀"};
private LinearLayout ll_dot;
private TextView viewPager_tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //        
    init();
}

/**
 *  VIewPager     .
 */
private void init() {
    //          LinearLayout   ,      
    ll_dot = (LinearLayout) findViewById(R.id.ll_dot);
    //       .
    viewPager_tv = (TextView) findViewById(R.id.ViewPager_tv);

    //  VIewPager  .
    viewpager_vp = (ViewPager) findViewById(R.id.viewpager_vp);

    //    int       ,     ImageView  .       ,    ImageView,for  
    for (int x = 0; x < imageResIds.length; x++) {
        //  ImageView  
        ImageView imageView = new ImageView(this);
        //           ..setBackgroundResource  .
        imageView.setBackgroundResource(imageResIds[x]);
        //        ImageViews  ,    VIewPager     instantiateItem    .
        ImageViews.add(imageView);
        //      ,           ,        ,
        createDot(x);
    }

    //     .setAdapter
    viewpager_vp.setAdapter(new MyViewpagerAdapter());

    //  ViewPager      ,addOnPageChangeListener,set              
    viewpager_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            //  getCurrentItem           ViewPager item  .
            int currentItem = viewpager_vp.getCurrentItem();
            //       item, text         .
            changeTextandDot(currentItem);
        }
        @Override
        public void onPageSelected(int position) {

        }
        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });


}

/**
 *   :             :Ctrl+Q;
 *  ListVIew  ,    ViewPager    ,        PagerAdapter
 */
class MyViewpagerAdapter extends PagerAdapter {
    //getContent,  ViewPager     .               .
    @Override
    public int getCount() {
        return ImageViews.size();
    }

    //isViewFromObject,  ViewPager   View   InstantiateItem   Object      ,    :return view==object;
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    //Ctrl+H:               ctrl+h+l:     .
    //instantiateItem,ViewPager       .container:VIewPager   ,          ,position:           
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //        (  Position), ImageViews         ImageVIew  .
        ImageView imageView = ImageViews.get(position);
        //   ImageView  ,   VIewPager  ,   container,  addView
        container.addView(imageView);
        //  :    VIewPager    ,       , isViewFromObject      ,      ImageView,     ImageView
        return imageView;
    }

    //      .   ListView   container,    page,         instantiateItem   VIew   ViewPager   ,
    //container:  ViewPager     position: object:    View  ,       View  
    //  :      Object,     View,    99% view,      Fragment,   Object,       .
    //       ,      :container.removeView((View) object);
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}

/**
 *   ViewPager item   ,          ,         .
 * @param  position int ViewPager item,       
 */
public void changeTextandDot(int position){
    //  ViewPager item   ,       .setText(descs[position]);
    viewPager_tv.setText(descs[position]);
    //              , for  ,        ,   position  
    for(int x=0; x< imageResIds.length; x++){
        //.getChildAt(x);        .  VIew  
        View childAt = ll_dot.getChildAt(x);
        // View      ,,       .
        childAt.setBackgroundResource(x==position?R.drawable.dot_focus:R.drawable.dot_normal);
    }

}

/**
 *          ,      
 */
private void createDot(int x) {
    //    View  ;
    View view= new View(this);
    //   View      setBackgroundResource
    view.setBackgroundResource(R.drawable.dot_normal);
    // View        ,      LayoutParams(int,int),     ,        
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(8,8);
    //      LayoutParams.leftMargin=int,       padding.
    layoutParams.leftMargin=8;
    //     layoutParams    ,   View  .setLayoutParams
    view.setLayoutParams(layoutParams);
    //      .addView(VIwe);
    ll_dot.addView(view);
}