资源简介

package com.example.viewpager;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity
{
    private int[] imgResIDs = new int[]
    { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e };
    private int[] radioButtonID = new int[]
    { R.id.radio0, R.id.radio1, R.id.radio2, R.id.radio3, R.id.radio4 };
    private ViewPager pager;
    private RadioGroup mGroup;
    private ArrayList<View> items = new ArrayList<View>();
    private Runnable runnable;
    private int mCurrentItem = 0;
    private int mItem;
    private Runnable mPagerAction;
    private boolean isFrist = true;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pager = (ViewPager) findViewById(R.id.tuijian_pager);
        mGroup = (RadioGroup) findViewById(R.id.radioGroup1);
        initAllItems();
        pager.setAdapter(new PagerAdapter()
        {
            // 创建
            @Override
            public Object instantiateItem(View container, int position)
            {
                View layout = items.get(position % items.size());
                pager.addView(layout);
                return layout;
            }
            
            // 销毁
            @Override
            public void destroyItem(View container, int position, Object object)
            {
                View layout = items.get(position % items.size());
                pager.removeView(layout);
            }
            
            @Override
            public boolean isViewFromObject(View arg0, Object arg1)
            {
                return arg0 == arg1;
                
            }
            
            @Override
            public int getCount()
            {
                return imgResIDs.length;
            }
            
        });
        pager.setOnPageChangeListener(new OnPageChangeListener()
        {
            
            @Override
            public void onPageSelected(final int arg0)
            {
                mCurrentItem = arg0 % items.size();
                pager.setCurrentItem(mCurrentItem);
                mGroup.check(radioButtonID[mCurrentItem]);
                items.get(arg0).findViewById(R.id.tuijian_header_img).setOnClickListener(new OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Toast.makeText(MainActivity.this, arg0 "", 1000).show();
                    }
                });
            }
            
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {
                // TODO Auto-generated method stub
            }
            
            @Override
            public void onPageScrollStateChanged(int arg0)
            {
                // TODO Auto-generated method stub
                
            }
        });
        mPagerAction = new Runnable()
        {
            @Override
            public void run()
            {
                
                if (mItem != 0)
                {
                    if (isFrist == true)
                    {
                        mCurrentItem = 0;
                        isFrist = false;
                    }
                    else
                    {
                        if (mCurrentItem == items.size() - 1)
                        {
                            mCurrentItem = 0;
                        }
                        else
                        {
                            mCurrentItem ;
                        }
                    }
                    
                    pager.setCurrentItem(mCurrentItem);
                    mGroup.check(radioButtonID[mCurrentItem]);
                    
                }
                pager.postDelayed(mPagerAction, 2500);
            }
        };
        pager.postDelayed(mPagerAction, 100);
    }
    
    private void initAllItems()
    {
        // 初始化Viewpager的所有item
        for (int i = 0; i < imgResIDs.length; i )
        {
            items.add(initPagerItem(imgResIDs[i]));
        }
        mItem = items.size();
    }
    
    private View initPagerItem(int resID)
    {
        View layout1 = getLayoutInflater().inflate(R.layout.tuijian_header, null);
        ImageView imageView1 = (ImageView) layout1.findViewById(R.id.tuijian_header_img);
        imageView1.setImageResource(resID);
        return layout1;
    }
}


资源截图

代码片段和文件信息

/** Automatically generated file. DO NOT MODIFY */
package com.example.viewpager;

public final class BuildConfig {
    public final static boolean DEBUG = true;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        475  2014-06-12 13:52  viewpager\.classpath

     文件        845  2014-06-12 13:51  viewpager\.project

     文件        884  2014-06-12 13:52  viewpager\AndroidManifest.xml

     文件        884  2014-06-12 16:08  viewpager\bin\AndroidManifest.xml

     文件        351  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\BuildConfig.class

     文件       1794  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\MainActivity$1.class

     文件       1280  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\MainActivity$2$1.class

     文件       2209  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\MainActivity$2.class

     文件       1800  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\MainActivity$3.class

     文件       3903  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\MainActivity.class

     文件        640  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\MyViewPager.class

     文件       1566  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\R$attr.class

     文件        652  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\R$bool.class

     文件       1472  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\R$color.class

     文件       1276  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\R$dimen.class

     文件       1036  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\R$drawable.class

     文件        816  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\R$id.class

     文件        694  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\R$integer.class

     文件        450  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\R$layout.class

     文件        397  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\R$menu.class

     文件        481  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\R$string.class

     文件        671  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\R$style.class

     文件       3485  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\R$styleable.class

     文件        917  2014-06-17 11:44  viewpager\bin\classes\com\example\viewpager\R.class

     文件       1569  2014-06-17 11:44  viewpager\bin\classes\com\viewpagerindicator\R$attr.class

     文件        655  2014-06-17 11:44  viewpager\bin\classes\com\viewpagerindicator\R$bool.class

     文件       1475  2014-06-17 11:44  viewpager\bin\classes\com\viewpagerindicator\R$color.class

     文件       1181  2014-06-17 11:44  viewpager\bin\classes\com\viewpagerindicator\R$dimen.class

     文件        740  2014-06-17 11:44  viewpager\bin\classes\com\viewpagerindicator\R$drawable.class

     文件        516  2014-06-17 11:44  viewpager\bin\classes\com\viewpagerindicator\R$id.class

............此处省略91个文件信息

评论

共有 条评论