/**
* 1.在布局中加载viewpager控件android.support.v4.view.ViewPager
* 2.初始化viewpager
* 3.数据源的初始化
* 4.创建适配器 --->继承PagerAdapter 重写4个方法
* 5.绑定适配器
* 6.设置监听事件*/
1.在布局中加载viewpager控件android.support.v4.view.ViewPager
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > 添加Viewpager布局 <android.support.v4.view.ViewPager android:id="@+id/vpager" android:background="@drawable/guide_1" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> <Button android:id="@+id/enter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp" android:onClick="onEnter" android:visibility="gone" android:text="enter" /> </RelativeLayout>
4.创建适配器 --->继承PagerAdapter 重写4个方法
主要实现四个方法
public int getCount() //获取视图中加载的view的个数//判断是否决定一个页面视图与特定的重点对象返回的instantiateitem相关(ViewGroup,int)。该方法是一个PagerAdapter正常需要。
public boolean isViewFromObject(View arg0, Object arg1) //添加视图
public Object instantiateItem(ViewGroup container, int position) //销毁视图
public void destroyItem(ViewGroup container, int position, Object object)
public class MyPagerAdapter extends PagerAdapter { private List<View> list; public MyPagerAdapter(List<View> list) { this.list = list; } @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } /** * 添加视图 */ public Object instantiateItem(ViewGroup container, int position) { View v = list.get(position); //将View对象添加到ViewGroup中 container.addView(v); return v; } /** * 移除视图 */ public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(list.get(position)); } }
2.初始化viewpager
* 3.数据源的初始化
* 5.绑定适配器
* 6.设置监听事件
public class MainActivity extends Activity implements OnPageChangeListener { private ViewPager viewPager; //数据源 private List<View> list; //适配器 private MyPagerAdapter adapter; Button btn3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn3 = (Button) findViewById(R.id.enter); viewPager = (ViewPager) findViewById(R.id.vpager); list = new ArrayList<View>(); ininData(); adapter = new MyPagerAdapter(list); //绑定适配器 viewPager.setAdapter(adapter); //设置监听 viewPager.setOnPageChangeListener(this); /*new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(MainActivity.this, One.class); startActivity(intent); finish(); } }, 3000);*/ } public void ininData(){ ImageView iv1 = new ImageView(this); iv1.setBackgroundResource(R.drawable.guide_1); list.add(iv1); ImageView iv2 = new ImageView(this); iv2.setBackgroundResource(R.drawable.guide_2); list.add(iv2); ImageView iv3 = new ImageView(this); iv3.setBackgroundResource(R.drawable.guide_3); list.add(iv3); ImageView iv4 = new ImageView(this); iv4.setBackgroundResource(R.drawable.guide_4); list.add(iv4); } public void onEnter(View v){ new Handler().post(new Runnable() { @Override public void run() { Intent intent = new Intent(MainActivity.this, One.class); startActivity(intent); finish(); } }); } /** * 有三种状态(0,1,2)。 * arg0 ==1的时辰默示正在滑动, * arg0==2的时辰默示滑动完毕了, * arg0==0的时辰默示什么都没做。 */ public void onPageScrollStateChanged(int arg0) { Log.i("tag", "onPageScrollStateChanged"+arg0); } /** * arg0 :当前页面,及你点击滑动的页面 arg1:当前页面偏移的百分比 arg2:当前页面偏移的像素位置 */ public void onPageScrolled(int arg0, float arg1, int arg2) { Log.i("tag", "onPageScrolled"+arg0+"-->"+arg1+"--->"+arg2); } /** * 此方法是页面跳转完后得到调用, * arg0是你当前选中的页面的Position(位置编号)。 */ public void onPageSelected(int arg0) { Log.i("tag", "onPageSelected"+arg0); if(arg0 == 3){ btn3.setVisibility(View.VISIBLE); } } }
使用ViewPager 显示上部的广告活动,添加一个异步任务,
使用setCurrentItem(index)设置当前页面
class MyAsyncTask extends AsyncTask<Void, Void, Void>{ protected Void doInBackground(Void... params) { while(true){ //判断当前的异步任务是否取消 if(isCancelled()){ break; } try { Thread.sleep(2000); //publishProgress(); } catch (Exception e) { e.printStackTrace(); } } return null; } protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); viewPager.setCurrentItem(++index%4); if(index==4){ Intent intent = new Intent(MainActivity.this, One.class); startActivity(intent); finish(); index=0; //viewPager.removeAllViews(); //viewPager.setCurrentItem(0); //add(); } } }
时间: 2024-10-22 22:18:50