Android中ViewPager嵌套ViewPager

    • 先看代码:
    • 这一步处理最重要的是MyPagerAdapter构造方法中不能传入getActivity().getSupportFragmentManager()
    • 原因如下:

最近UI重新设计了一个店铺首页的效果,要用到ViewPager嵌套ViewPager才能实现效果,可是在子ViewPager中出现了加载不出来Fragment,由于里面用了多层的Fragment。

老规矩,先看效果图,没图说个j8。

先看代码:

首页代码:

第一层布局代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_marginTop="-4dp"    android:background="@drawable/ic_shop_bg2"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="28dp"        android:layout_marginLeft="@dimen/text_layout_15_dp"        android:layout_marginRight="@dimen/text_layout_15_dp"        android:layout_marginTop="25dp"        android:gravity="center_vertical"        android:orientation="horizontal">        <TextView            android:id="@+id/tv_shop_name"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="ShopName"            android:textColor="@color/white"            android:textSize="@dimen/text_size_16sp"            android:textStyle="bold" />        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_gravity="right"            android:gravity="center_vertical"            android:orientation="horizontal">            <Button                android:id="@+id/bt_shop_is_coll"                android:layout_width="61dp"                android:layout_height="28dp"                android:layout_marginRight="@dimen/text_layout_12_dp"                android:background="@drawable/ic_shop_text_bg"                android:text="已关注"                android:textColor="@color/white"                android:textSize="@dimen/text_size_13_sp" />            <LinearLayout                android:layout_width="75dp"                android:layout_height="28dp"                android:background="@drawable/ic_shop_text_bg"                android:gravity="center"                android:orientation="horizontal">                <TextView                    android:id="@+id/tv_shop_other"                    android:layout_width="wrap_content"                    android:layout_height="match_parent"                    android:gravity="center_vertical"                    android:paddingLeft="@dimen/text_layout_5_dp"                    android:paddingRight="@dimen/text_layout_10_dp"                    android:text="···"                    android:textColor="@color/white"                    android:textSize="@dimen/text_size_20_sp" />                <View                    android:layout_width="1dp"                    android:layout_height="match_parent"                    android:layout_marginBottom="@dimen/text_layout_5_dp"                    android:layout_marginTop="@dimen/text_layout_5_dp"                    android:background="@color/white"                    android:gravity="center_vertical" />                <ImageView                    android:id="@+id/iv_shop_exit"                    android:layout_width="wrap_content"                    android:layout_height="match_parent"                    android:gravity="center_vertical"                    android:paddingLeft="@dimen/text_layout_10_dp"                    android:paddingRight="@dimen/text_layout_5_dp"                    android:src="@drawable/ic_shop_exit"                    android:textSize="@dimen/text_size_15_sp" />            </LinearLayout>        </LinearLayout>    </LinearLayout>    <com.jkgl.view.NoSlideViewPager        android:id="@+id/viewPager"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:layout_marginTop="@dimen/text_layout_5_dp"        />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="@dimen/text_layout_50_dp"        android:orientation="horizontal"        android:gravity="center_vertical"        android:background="@color/white"        android:paddingLeft="@dimen/text_layout_15_dp"        android:paddingRight="@dimen/text_layout_15_dp"        >        <RelativeLayout            android:id="@+id/rl_shop_home"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:gravity="center"            >            <ImageView                android:id="@+id/iv_shop_logo"                android:layout_width="41dp"                android:layout_height="41dp"                android:src="@drawable/logo1"                android:visibility="visible"                />            <TextView                android:id="@+id/tv_shop_home"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="首页"                android:textColor="@color/tv333"                android:textSize="@dimen/text_size_10_sp"                android:drawableTop="@drawable/ic_shop_home"                android:drawablePadding="@dimen/text_layout_5_dp"                android:layout_centerInParent="true"                android:gravity="center"                android:visibility="gone"                />        </RelativeLayout>        <LinearLayout            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:gravity="center"            android:orientation="vertical"            >            <TextView                android:id="@+id/tv_all_goods"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="全部宝贝"                android:textColor="@color/tv333"                android:textSize="@dimen/text_size_10_sp"                android:drawableTop="@drawable/ic_shop_all"                android:drawablePadding="@dimen/text_layout_5_dp"                />        </LinearLayout>        <LinearLayout            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:gravity="center"            android:orientation="vertical"            >            <TextView                android:id="@+id/tv_shop_goods_classify"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="宝贝分类"                android:textColor="@color/tv333"                android:textSize="@dimen/text_size_10_sp"                android:drawableTop="@drawable/ic_shop_classify"                android:drawablePadding="@dimen/text_layout_5_dp"                />        </LinearLayout>        <LinearLayout            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:gravity="center"            android:orientation="vertical"            >            <TextView                android:id="@+id/tv_shop_service"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="联系客服"                android:textColor="@color/tv333"                android:textSize="@dimen/text_size_10_sp"                android:drawableTop="@drawable/ic_shop_service"                android:drawablePadding="@dimen/text_layout_5_dp"                />        </LinearLayout>    </LinearLayout></LinearLayout>

第一层逻辑代码

/** * 店铺首页 */public class ShopActivity extends FastBaseActivity {    @InjectView(R.id.tv_shop_name)    TextView tv_shop_name;    @InjectView(R.id.tv_shop_other)    TextView tv_shop_other;//三个点    @InjectView(R.id.bt_shop_is_coll)    Button bt_shop_is_coll;//是否关注    @InjectView(R.id.iv_shop_exit)    ImageView iv_shop_exit;//退出    @InjectView(R.id.viewPager)    NoSlideViewPager viewPager;    @InjectView(R.id.rl_shop_home)    RelativeLayout rl_shop_home;    @InjectView(R.id.iv_shop_logo)    ImageView iv_shop_logo;    @InjectView(R.id.tv_shop_home)    TextView tv_shop_home;    @InjectView(R.id.tv_all_goods)    TextView tv_all_goods;    @InjectView(R.id.tv_shop_goods_classify)    TextView tv_shop_goods_classify;    @InjectView(R.id.tv_shop_service)    TextView tv_shop_service;    private List<Fragment> fragmentList;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_shop);        ButterKnife.inject(this);        initView();    }    private void initView() {        fragmentList = new ArrayList<>();        fragmentList.add(ShopHomeFragment.getInstance());        fragmentList.add(AllGoodsFragment.getInstance());        fragmentList.add(GoodsClassifyFragment.getInstance());        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());        viewPager.setAdapter(adapter);        viewPager.setOffscreenPageLimit(3);    }    @OnClick({R.id.iv_shop_exit,R.id.rl_shop_home,R.id.tv_all_goods,R.id.tv_shop_goods_classify,R.id.tv_shop_service})    public void onClick(View view) {        Drawable drawableTopAllNormal = getResources().getDrawable(R.drawable.ic_shop_all);        Drawable drawableTopAllSelect = getResources().getDrawable(R.drawable.ic_shop_all_select);        Drawable drawableTopClassifyNormal = getResources().getDrawable(R.drawable.ic_shop_classify);        Drawable drawableTopClassifySelect = getResources().getDrawable(R.drawable.ic_shop_classify_select);        switch (view.getId()) {            case R.id.iv_shop_exit:                finish();                break;            case R.id.rl_shop_home:                iv_shop_logo.setVisibility(View.VISIBLE);                tv_shop_home.setVisibility(View.GONE);                tv_all_goods.setTextColor(getResources().getColor(R.color.tv333));                tv_shop_goods_classify.setTextColor(getResources().getColor(R.color.tv333));                tv_all_goods.setCompoundDrawablesWithIntrinsicBounds(null,drawableTopAllNormal,null,null);                tv_shop_goods_classify.setCompoundDrawablesWithIntrinsicBounds(null,drawableTopClassifyNormal,null,null);                viewPager.setCurrentItem(0,false);                break;            case R.id.tv_all_goods:                tv_all_goods.setCompoundDrawablesWithIntrinsicBounds(null,drawableTopAllSelect,null,null);                tv_shop_goods_classify.setCompoundDrawablesWithIntrinsicBounds(null,drawableTopClassifyNormal,null,null);                tv_all_goods.setTextColor(getResources().getColor(R.color.zhutihuang));                tv_shop_goods_classify.setTextColor(getResources().getColor(R.color.tv333));                iv_shop_logo.setVisibility(View.GONE);                tv_shop_home.setVisibility(View.VISIBLE);                viewPager.setCurrentItem(1,false);                break;            case R.id.tv_shop_goods_classify:                tv_all_goods.setCompoundDrawablesWithIntrinsicBounds(null,drawableTopAllNormal,null,null);                tv_shop_goods_classify.setCompoundDrawablesWithIntrinsicBounds(null,drawableTopClassifySelect,null,null);                tv_all_goods.setTextColor(getResources().getColor(R.color.tv333));                tv_shop_goods_classify.setTextColor(getResources().getColor(R.color.zhutihuang));                iv_shop_logo.setVisibility(View.GONE);                tv_shop_home.setVisibility(View.VISIBLE);                viewPager.setCurrentItem(2,false);                break;            case R.id.tv_shop_service:                String kefu2 = "KEFU154277063782440";                /**                 * 启动客户服聊天界面。                 *                 * @param context           应用上下文。                 * @param customerServiceId 要与之聊天的客服 Id。                 * @param title             聊天的标题,如果传入空值,则默认显示与之聊天的客服名称。                 * @param customServiceInfo 当前使用客服者的用户信息。{@link CSCustomServiceInfo}                 */                RongIM.getInstance().startCustomerServiceChat(this, kefu2, "在线客服",null);                break;        }    }    private class MyPagerAdapter extends FragmentPagerAdapter{        public MyPagerAdapter(FragmentManager fm) {            super(fm);        }        @Override        public Fragment getItem(int position) {            return fragmentList.get(position);        }        @Override        public int getCount() {            return fragmentList.size();        }    }}

我第一层的viewpager为了防止子View中的滑动冲突,做了一个处理,禁止滑动ViewPager

/** * 禁止左右滑动viewpager * Created by Administrator on 2018/12/10. */public class NoSlideViewPager extends ViewPager {    //是否可以进行滑动    private boolean isSlide = false;    public void setSlide(boolean slide) {        isSlide = slide;    }    public NoSlideViewPager(Context context) {        super(context);    }    public NoSlideViewPager(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        return isSlide;    }    @Override    public boolean onTouchEvent(MotionEvent ev) {        return false;    }}

第二层布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:orientation="vertical"    >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:gravity="center_vertical"            android:layout_gravity="bottom"            android:layout_marginLeft="@dimen/text_layout_15_dp"            android:layout_marginRight="@dimen/text_layout_15_dp"            android:layout_marginTop="@dimen/text_layout_20_dp"            >            <LinearLayout                android:id="@+id/ll_shop_search"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@drawable/ic_shop_search_bg"                android:orientation="horizontal"                android:gravity="center"                >                <ImageView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:src="@drawable/ic_shop_search"                    />                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="搜索"                    android:textSize="@dimen/text_size_14_sp"                    android:textColor="@color/white"                    />            </LinearLayout>            <android.support.design.widget.TabLayout                android:id="@+id/tab_shop"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                app:tabIndicatorColor="@color/white"                app:tabSelectedTextColor="@color/white"                app:tabMode="fixed"                app:tabTextColor="@color/white"                style="@style/tabSize"                android:layout_marginLeft="24dp"                android:paddingBottom="@dimen/text_layout_5_dp"                />        </LinearLayout>    <android.support.v4.view.ViewPager        android:id="@+id/viewPager"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_marginTop="@dimen/text_layout_5_dp"        >    </android.support.v4.view.ViewPager></LinearLayout>

代码

/** * 店铺首页 * Created by Administrator on 2018/12/13. */public class ShopHomeFragment extends Fragment {    LinearLayout ll_shop_search;//搜索    TabLayout tab_shop;    ViewPager viewPager;    private String[] title = {"首页","宝贝","新品"};    private List<Fragment> fragmentList;    static ShopHomeFragment fragment;    public static ShopHomeFragment getInstance(){        if(fragment == null){            fragment = new ShopHomeFragment();        }        return fragment;    }    @Nullable    @Override    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_shop_home,null);        initView(view);        initListener();        return view;    }    private void initListener() {        ll_shop_search.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                ToastUtil.showToast("搜索");            }        });    }    private void initView(View view) {        ll_shop_search = view.findViewById(R.id.ll_shop_search);        tab_shop = view.findViewById(R.id.tab_shop);        viewPager = view.findViewById(R.id.viewPager);        fragmentList = new ArrayList<>();        fragmentList.add(new ShopHomeAllFragment());        fragmentList.add(new ShopHomeGoodsFragment());        fragmentList.add(new ShopHomeNewGoodsFragment());        MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());        viewPager.setAdapter(adapter);        tab_shop.setupWithViewPager(viewPager);        viewPager.setOffscreenPageLimit(3);    }    private class MyPagerAdapter extends FragmentPagerAdapter {        public MyPagerAdapter(FragmentManager fm) {            super(fm);        }        @Override        public Fragment getItem(int position) {            return fragmentList.get(position);        }        @Override        public int getCount() {            return fragmentList.size();        }        @Nullable        @Override        public CharSequence getPageTitle(int position) {            return title[position];        }    }}

这一步处理最重要的是MyPagerAdapter构造方法中不能传入getActivity().getSupportFragmentManager()

MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());

原因如下:

Activity的Fragment要使用FragmentManager(),
而在Fragment中动态的添加Fragment要使用getChildFragmetManager()来管理。

更多相关文章

  1. android实现推特Twitter分享
  2. Android(安卓)init源代码分析(1)概要分析
  3. 【安卓】Content Provider 基础
  4. android:沉浸式状态栏(状态栏一体化)
  5. Android(安卓)- 引用计数(sp、wp、Refbase)
  6. Android接入免费的短信验证SMSSDK的应用
  7. 从一篇Blog看两个并发编程错误
  8. Android(安卓)Studio查看Android(安卓)5.x源码的步骤详解
  9. Android(安卓)高手进阶教程(十四)之----Android(安卓)Location的

随机推荐

  1. 第一个android程序HelloWorld
  2. android intent action 介绍大全
  3. Android客户端应用享用传统Web服务
  4. android的TabActivity
  5. android moudle 资源文件重命名
  6. Android(安卓)实现图片圆角
  7. Android 开源项目源码分析第一期正式发布
  8. 【Android(安卓)开发教程】链接Activitie
  9. Flex学习小心得
  10. Android中的onActivityResult和setResult