Android ViewPager 的简单应用

释放双眼,带上耳机,听听看~!

适配器

package com.example.android_viewpager;
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
public class MyViewPagerAdapter extends PagerAdapter{
private List<View>viewList;
private List<String>titleList;
public MyViewPagerAdapter(List<View>viewList,List<String>titleList)
{
this.viewList=viewList;
this.titleList=titleList;
}
//返回所有视图的数量
@Override
public int getCount() {
// TODO Auto-generated method stub
return viewList.size();
}
//判断视图是否由对象产生
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
//实例化页面
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(viewList.get(position));
return viewList.get(position);
}
//删除页面
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView(viewList.get(position));
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return titleList.get(position);
}
}

布局文件就是一个viewpager 还有 四个子view 很简单

FragmentAdrater

package com.example.android_viewpager;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
private List<Fragment>fragList;
private List<String>titleList;
public MyFragmentPagerAdapter(FragmentManager fm,List<Fragment> fragList,List<String>titleList) {
super(fm);
// TODO Auto-generated constructor stub
this.fragList=fragList;
this.titleList=titleList;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return fragList.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return fragList.size();
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return titleList.get(position);
}
}

 

 

 

人已赞赏
Android文章

Android基于TCP协议的Socket通信 实现用户登录 以及服务端的相应

2020-3-27 17:03:05

Android文章

Android AsyncTask 异步加载网络图片简单应用

2020-3-27 18:18:01

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索