标签:
ViewPager : 左右切换屏幕
PagerAdapter: ViewPager的适配器, 加载ViewPager内容显示的View
实现四个方法:
isViewFromObject();
getCount();
instantiateItem();添加视图
destroyItem()销毁视图
pagerAdapter = new PagerAdapter() { @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public int getCount() { // TODO Auto-generated method stub return views.size(); } @Override public Object instantiateItem(ViewGroup container, int position) { View view = views.get(position); container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(views.get(position)); } };
Viewpager 设置滚动的事件监听:
onPageSelected(int arg0);//滚动到了那个界面
进行相应处理
viewPager.setAdapter(pagerAdapter); viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub switch (arg0) { case 0: tv_header.setText("微信"); break; case 1: tv_header.setText("好友"); break; case 2: tv_header.setText("联系人"); break; case 3: tv_header.setText("我的"); break; default: break; } } @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 } });
视图的数据初始化, 加载视图:
inflater = LayoutInflater.from(this); views = new ArrayList<View>(); View one = inflater.inflate(R.layout.tab01_content_layout, null); View two = inflater.inflate(R.layout.tab02_content_layout, null); View three = inflater.inflate(R.layout.tab03_content_layout, null); View four = inflater.inflate(R.layout.tab04_content_layout, null); views.add(one); views.add(two); views.add(three); views.add(four);
代码如下:
package com.example.tabdemo01; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MainActivity extends Activity { private ViewPager viewPager; private PagerAdapter pagerAdapter; private LayoutInflater inflater; TextView tv_header; private List<View> views; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); viewPager.setAdapter(pagerAdapter); viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub switch (arg0) { case 0: tv_header.setText("微信"); break; case 1: tv_header.setText("好友"); break; case 2: tv_header.setText("联系人"); break; case 3: tv_header.setText("我的"); break; default: break; } } @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 } }); } public void initView() { tv_header = (TextView) findViewById(R.id.tv_headr); viewPager = (ViewPager) findViewById(R.id.viewPager); inflater = LayoutInflater.from(this); views = new ArrayList<View>(); View one = inflater.inflate(R.layout.tab01_content_layout, null); View two = inflater.inflate(R.layout.tab02_content_layout, null); View three = inflater.inflate(R.layout.tab03_content_layout, null); View four = inflater.inflate(R.layout.tab04_content_layout, null); views.add(one); views.add(two); views.add(three); views.add(four); pagerAdapter = new PagerAdapter() { @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public int getCount() { // TODO Auto-generated method stub return views.size(); } @Override public Object instantiateItem(ViewGroup container, int position) { View view = views.get(position); container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(views.get(position)); } }; } public void onweixin(View view){ viewPager.setCurrentItem(0); } public void onFriend(View view){ viewPager.setCurrentItem(1); } public void onContacts(View view){ viewPager.setCurrentItem(2); } public void onMyself(View view){ viewPager.setCurrentItem(3); } }
Tab系列 01 ViewPager+PagerAdapter(传统方式)
标签:
原文地址:http://www.cnblogs.com/chengbao/p/5651167.html