2019独角兽企业重金招聘Python工程师标准>>>
ViewPage标签:
使用android.support.v4.view.ViewPager 包为了更好兼容低版本。
将Layout布局转换成View对象
LayoutInflater if = getLayoutInflater().from(this);
if.inflate(resoure,root);
View.inflate(context,resoure,root);
需要用到的适配器:
PagerAdapter 数据源 :List
FragmentPagerAdapter 数据源 :List
FragmentStatePagerAdapter 数据源:List
贴上代码例子
布局文件 main.xml
view1.xml 依次建立 view2 view3 view4
activity写法
package com.example.viewpage;
import java.util.ArrayList;
import java.util.List;
import com.example.viewpage.adapter.MyFragmentPageAdapter;
import com.example.viewpage.adapter.MyPageAdpter;
import com.example.viewpage.fragment.MyFragment1;
import com.example.viewpage.fragment.MyFragment2;
import com.example.viewpage.fragment.MyFragment3;
import com.example.viewpage.fragment.MyFragment4;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.Toast;
import android.R.string;
import android.app.Activity;
import android.graphics.Color;
public class MainActivity extends FragmentActivity implements OnPageChangeListener{
private List viewList; //数据源
private ViewPager viewPager ;
private List titleList;
private PagerTabStrip tab;
private List fragList;//数据源
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewPager = (ViewPager) findViewById(R.id.pager);
tab = (PagerTabStrip) findViewById(R.id.tab);
viewList = new ArrayList();
titleList = new ArrayList();
fragList = new ArrayList();
//加载View
View view1 = View.inflate(this, R.layout.view1, null);
View view2 = View.inflate(this, R.layout.view2, null);
View view3 = View.inflate(this, R.layout.view3, null);
View view4 = View.inflate(this, R.layout.view4, null);
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
viewList.add(view4);
fragList.add(new MyFragment1());
fragList.add(new MyFragment2());
fragList.add(new MyFragment3());
fragList.add(new MyFragment4());
//加载title
titleList.add("第1页");
titleList.add("第2页");
titleList.add("第3页");
titleList.add("第4页");
//设置PagerTabStrip的属性
tab.setBackgroundColor(Color.YELLOW);
tab.setTextColor(Color.GREEN);
tab.setDrawFullUnderline(false);//长线不显示
tab.setTabIndicatorColor(Color.BLUE);
MyPageAdpter myPageAdpter = new MyPageAdpter(viewList,titleList);
MyFragmentPageAdapter fragmentPageAdapter = new MyFragmentPageAdapter(getSupportFragmentManager(), fragList, titleList);
viewPager.setAdapter(fragmentPageAdapter);
viewPager.setOnPageChangeListener(this);
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
/**
* 监听当前选择的页面
*/
@Override
public void onPageSelected(int arg0) {
Toast.makeText(this, "当前页数"+(arg0+1) , Toast.LENGTH_SHORT).show();
}
}
三种Adapter的写法
package com.example.viewpage.adapter;
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
public class MyPageAdpter extends PagerAdapter{
private List viewList;
private List titleList;
public MyPageAdpter(List viewList,List titleList) {
super();
this.viewList = viewList;
this.titleList = titleList;
}
/**
* 返回页卡的数量
*/
@Override
public int getCount() {
return viewList.size();
}
/**
* view是否来自于对象
*/
@Override
public boolean isViewFromObject(View view, Object arg1) {
return view == arg1;
}
/**
* 实例化一个页卡
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
}
/**
* 销毁一个页卡
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.get(position));
}
/**
* 设置vierpager页卡的标题
*/
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
package com.example.viewpage.adapter;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPageAdapter extends FragmentPagerAdapter{
private List fragList;
private List titleList;
public MyFragmentPageAdapter(FragmentManager fm,List fragList, List titleList) {
super(fm);
this.fragList =fragList;
this.titleList =titleList;
}
@Override
public Fragment getItem(int position) {
return fragList.get(position);
}
@Override
public int getCount() {
return fragList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
package com.example.viewpage.adapter;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;
public class MyFragmentStatePageAdapter extends FragmentStatePagerAdapter{
private List fragList;
private List titleList;
public MyFragmentStatePageAdapter(FragmentManager fm,List fragList, List titleList) {
super(fm);
this.fragList =fragList;
this.titleList =titleList;
}
@Override
public Fragment getItem(int position) {
return fragList.get(position);
}
@Override
public int getCount() {
return fragList.size();
}
@Override
public Object instantiateItem(ViewGroup arg0, int arg1) {
return super.instantiateItem(arg0, arg1);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
}
建立Fragment1 再依次创建3个
package com.example.viewpage.fragment;
import com.example.viewpage.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyFragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.view1, container, false);
}
}