1.ViewPager添加view---实现广告图片轮播
2ViewPager无限滑动---实现广告图片无限循环
2.ViewPager + FragmentStatePagerAdapter + Fragment
3.抖音 ViewPager突出的底部标题栏的实现clipChildren
4.FragmentPagerAdapter
or FragmentStatePagerAdapter
.2者的区别 .4个方法的用处和理解
5.viewPager添加标题PagerTabStrip与PagerTitleStrip
LayoutInflater layoutInflater = LayoutInflater.from(context);
for(NearBean.NearUserinfo nearUserinfo :recommendList){
View convertView = layoutInflater.inflate(R.layout.item_recommend_people, null);
TextView textViewName=convertView.findViewById(R.id.recommend_people_name);
TextView textViewAge=convertView.findViewById(R.id.recommend_people_age);
TextView textViewCountry=convertView.findViewById(R.id.recommend_people_country);
TextView textViewAttention=convertView.findViewById(R.id.recommend_people_attention);
放置里面和放置外面的区别,否则报错
影响viewpager自动翻页的动画效果,boolean smoothScroll
public void setCurrentItem(int item) {
super.setCurrentItem(item, false);
}
其实谷歌文档已经介绍的很详细了。
public void setCurrentItem (int item, boolean smoothScroll)
Set the currently selected page. // 设定当前选择的页面
参数
item int: Item index to select
smoothScroll boolean: True to smoothly scroll to the new item, false to transition immediately
// 如果smoothScroll 为true,则平滑滚动到指定页面,false 则直接跳转到指定页面
public void setCurrentItem (int item)
Set the currently selected page. If the ViewPager has already been through its first layout with its current adapter there will be a smooth animated transition between the current item and the specified item.
// 设定当前选择的页面。如果ViewPager 已经通过指定页面,那么将会在当前iten 与指定item 有一个平滑的过渡动画。
参数
item int: Item index to select
自定义动画效果
viewPager.setPageTransformer(true, new ScalePageTransformer());
FragmentPagerAdapter派生自PagerAdapter,它是用来呈现Fragment页面的,这些Fragment页面会一直保存在fragment manager中,以便用户可以随时取用。
这个适配器最好用于有限个静态fragment页面的管理。尽管不可见的视图有时会被销毁,但用户所有访问过的fragment都会被保存在内存中。因此fragment实例会保存大量的各种状态,这就造成了很大的内存开销。所以如果要处理大量的页面切换,建议使用FragmentStatePagerAdapter.
---------------------
instantiateItem(ViewGroup, int)
destroyItem(ViewGroup, int, Object)
getCount()
isViewFromObject(View, Object)
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
// TODO Auto-generated method stub
container.removeView(viewList.get(position));
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return viewList.size();
}
比较重要的方法下面2个
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(viewList.get(position));
return viewList.get(position);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return titleList.get(position);
}
首先有一个最值得注意的地方:Activity派生自FragmentActivity,其实这是有关Fragment的基础知识,只有FragmentActivity才能内嵌fragment页面,普通Activity是不行的。
问题:在MainActivity中,当写到这句:fragments.add(new Fragment1()); 向Fragment列表中添加Fragement对象实例时,会提示“无法将Fragment1()转换为fragment”
解决办法 :这是因为导入包不一致,一般的问题在于:在Fragment1中导入的是android.app.Fragment, 而在这里导入类确是:android.support.v4.app.Fragment,包不同当然无法转换,统一导入为android.support.v4.app.Fragment之后就正常了.参考文章《android之cannot convert from Fragment1 to Fragment》
---------------------
1.viewPager如果循环滚动要最少3个,循环滚动往右滑会白屏
parent.removeAllViews();
public class NearRecommedLoopAdapter extends PagerAdapter {
private List views;
public NearRecommedLoopAdapter(List views) {
this.views = views;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position %= views.size();
if (position<0){
position = views.size()+position;
}
View view = views.get(position);
ViewPager parent = (ViewPager) view.getParent();
if (parent != null) {
parent.removeView(view);
container.addView(view);
}else{
container.addView(view);
}
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// container.removeView(views.get(position % views.size()));
}
}
2.