项目需要的框架是上面是可滑动隐藏的头布局,下面是滑动切换的viewpager
之前填充viewpager 用的adapter是
package com.example.panghaha12138.testapp;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
public class MyPagerAdapter extends PagerAdapter {
private final ArrayList views;
public MyPagerAdapter(ArrayList views) {
this.views = views;
}
@Override
public int getCount() {
return views.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView(views.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(views.get(position));
return views.get(position);
}
}
不过这是我在自己demo 里写的 没有考虑到解耦的问题
因为是通过inflate 吹起来的view,操作代码都需要在一个fragment里写
view1 = View.inflate(getActivity(), R.layout.view3, null);
recyclerView1 = (RecyclerView) view1.findViewById(R.id.dongtailist);
swipeRefreshLayout1 = (SwipeRefreshLayout) view1.findViewById(R.id.refsh);
view2 = View.inflate(getActivity(), R.layout.view3, null);
recyclerView2 = (RecyclerView) view2.findViewById(R.id.dongtailist);
swipeRefreshLayout2 = (SwipeRefreshLayout) view2.findViewById(R.id.refsh);
这样不利于扩展和维护
所以采取用fragmentstatepageradapter
package com.example.panghaha12138.testapp;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.List;
/**
* ━━━━ Code is far away from ━━━━━━
* () ()
* ( ) ( )
* ( ) ( )
* ┏┛┻━━━┛┻┓
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┻ ┃
* ┗━┓ ┏━┛
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
* ━━━━ bug with the more protecting ━━━
*
* @ClassName: Myadapter
* @Description:
* @Author: panghaha12138
* @Create On 2017/8/8 00:01
*/
public class Myadapter extends FragmentStatePagerAdapter {
public Myadapter(FragmentManager fm, List list) {
super(fm);
this.list = list;
}
public List list;
public Myadapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
package com.example.panghaha12138.testapp;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;
/**
* ━━━━ Code is far away from ━━━━━━
* () ()
* ( ) ( )
* ( ) ( )
* ┏┛┻━━━┛┻┓
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┻ ┃
* ┗━┓ ┏━┛
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
* ━━━━ bug with the more protecting ━━━
*
* @ClassName: Test2Adapter
* @PackageName: com.example.panghaha12138.testapp
* @Create On 2017/8/3 00:48
* @Author: PangHaHa12138
* @CSDN: http://blog.csdn.net/panghaha12138
* @GitHub: https://github.com/PangHaHa12138
* @jianshu: http://www.jianshu.com/u/4e577623e3f8
* @Copyrights 2017/8/3 PangHaHa12138 All rights reserved.
*/
public class Test2Adapter extends FragmentPagerAdapter {
private FragmentManager manager;
public Test2Adapter(FragmentManager fm) {
super(fm);
this.manager = fm;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
if (0 == position) {
fragment = new Fraglab1();
}
if (1 == position) {
fragment = new Fraglab2();
}
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "动态";
case 1:
return "头条";
}
return null;
}
}
最最重要的一点!
在activity 里面我们经常是
getSupportFragmentManager()
来获取 fragmentmanger
但是在fragment里 需要
test2Adapter = new Test2Adapter(getChildFragmentManager());
因为现在是在fragment里面嵌套fragment 需要找到子fragment 的manger 如果
这样写
getActivity().getSupportFragmentManager()
获取到的是activity里面的 manger
工作时出现的bug 这里记录一下