liaohuqiu / android-Ultra-Pull-To-Refresh

Ultra Pull to Refresh for Android. Support all the views.
MIT License
9.59k stars 2.66k forks source link

在里面嵌套CoordinatorLayout+AppBarLayout+TabLayout+ViewPager的问题 #315

Open deezhang opened 7 years ago

deezhang commented 7 years ago

在里面嵌套CoordinatorLayout+AppBarLayout+TabLayout+ViewPage,当我下拉刷新,viewpager重新实例适配器后,无法向上滚动,请大神指导~

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/pop_like"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/me_color" >

    <com.test.widget.CusPtrClassicFrameLayout xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/rotate_header_grid_view_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f0f0f0"
        cube_ptr:ptr_duration_to_close="200"
        cube_ptr:ptr_duration_to_close_header="1000"
        cube_ptr:ptr_keep_header_when_refresh="true"
        cube_ptr:ptr_pull_to_fresh="false"
        cube_ptr:ptr_ratio_of_header_height_to_refresh="1.2"
        cube_ptr:ptr_resistance="1.7">

        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/coordinatorLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/linearLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:background="@color/white"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">

                    <com.bigkoo.convenientbanner.ConvenientBanner
                        android:id="@+id/home_banner"
                        android:layout_width="match_parent"
                        android:layout_height="160dp"
                        app:canLoop="true"/>

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="10dp"
                        android:background="@color/white">
                    </RelativeLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:src="@mipmap/rocket"/>

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="20dp"
                            android:src="@mipmap/horn"/>

                        <com.test.widget.ScrollTextView
                            android:id="@+id/home_tv"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_marginLeft="10dp"
                            android:paddingTop="9dp"
                            android:textSize="14dp"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:background="@color/home_edittext_bg"
                        android:layout_marginTop="10dp">

                        <ImageView
                            android:id="@+id/iv_taobao"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="12dp"
                            android:src="@mipmap/ic_home_taobao_select"/>

                        <ImageView
                            android:id="@+id/iv_mall"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="12dp"
                            android:src="@mipmap/ic_home_mall_no_select"/>
                    </LinearLayout>
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginTop="10dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:background="@drawable/grey_radiu">

                        <EditText
                            android:id="@+id/search_ly"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableLeft="@mipmap/search"
                            android:padding="10dp"
                            android:editable="false"
                            android:textSize="13dp"
                            android:hint="宝贝名称/关键字(如连衣裙)"
                            android:layout_toLeftOf="@+id/tv_search"
                            android:background="@drawable/grey_radiu"/>

                        <TextView
                            android:id="@+id/tv_search"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@android:color/white"
                            android:text="搜索"
                            android:paddingTop="12dp"
                            android:paddingBottom="12dp"
                            android:paddingLeft="18dp"
                            android:paddingRight="18dp"
                            android:layout_alignParentRight="true"
                            android:background="@color/home_search_bg"/>

                    </RelativeLayout>

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="185dp"
                        android:layout_marginTop="10dp"
                        android:background="#FFFFFF">

                        <android.support.v4.view.ViewPager
                            android:id="@+id/grid_viewpager"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <LinearLayout
                            android:id="@+id/ll_dot"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentBottom="true"
                            android:layout_marginBottom="10dp"
                            android:gravity="center"
                            android:orientation="horizontal" />
                    </RelativeLayout>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="15dp"
                        android:background="@color/home_color"/>
                </LinearLayout>

                <android.support.design.widget.TabLayout
                    android:id="@+id/tabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabIndicatorColor="@color/red"
                    app:tabSelectedTextColor="@color/red"
                    app:tabBackground="@android:color/white"
                    android:layout_marginBottom="15dp"
                    app:tabTextColor="@android:color/black"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed"/>
            </android.support.design.widget.AppBarLayout>

            <android.support.v4.view.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
            </android.support.v4.view.ViewPager>
        </android.support.design.widget.CoordinatorLayout>

    </com.test.widget.CusPtrClassicFrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/home_title"
            android:layout_width="match_parent"
            android:background="@color/red"
            android:layout_height="50dp">

            <LinearLayout
                android:id="@+id/area_ly"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/area_tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#FFFFFF"
                    android:textSize="14dp" />

                <ImageView
                    android:id="@+id/down_img"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:src="@mipmap/down" />
            </LinearLayout>

            <ImageView
                android:id="@+id/message_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:src="@mipmap/new_off" />

        </RelativeLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:tabIndicatorColor="@color/red"
            app:tabSelectedTextColor="@color/red"
            app:tabBackground="@android:color/white"
            app:tabTextColor="@android:color/black" />
    </LinearLayout>
</FrameLayout>

这是我刷新后重新实例viewpger的适配器:

   ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
                    String[] tabs = new String[recommendClassifications.size()];
                    for (int i = 0; i < recommendClassifications.size(); i++) {
                        String s = recommendClassifications.get(i).getCategory_name();
                        tabs[i] = s;
                        ClassifiedGoodsFragment classifiedGoodsFragment = new ClassifiedGoodsFragment();
                        classifiedGoodsFragment.setWidget(appBarLayout);
                        Bundle bundle = new Bundle();
                        bundle.putString("category_id", recommendClassifications.get(i).getId());
                        classifiedGoodsFragment.setArguments(bundle);
                        fragmentList.add(i, classifiedGoodsFragment);
                    }
                    viewPagerAdapter = new HomeGoodsViewPagerAdapter(getChildFragmentManager(), getActivity(), fragmentList, tabs);
                    viewPager.setAdapter(viewPagerAdapter);
                    tabLayout.setupWithViewPager(viewPager);
                    tabLayout2.setupWithViewPager(viewPager);
                    viewPager.setCurrentItem(0, false);
a458167371 commented 7 years ago

同问 解决没

deezhang commented 7 years ago

解决了,不过暂时不知道是什么原因造成的,加载完viewpager的适配器后,再重设AppBarLayout的宽高就可以滑动了。 @a458167371

timi-codes commented 6 years ago

Try this

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        // TODO Auto-generated method stub
        //super.onScrollStateChanged(recyclerView, newState);
        int firstPos = mLayoutManager.findFirstCompletelyVisibleItemPosition();
        if (firstPos > 0) {
            ptrFrameLayout.setEnabled(false);
        } else {
            ptrFrameLayout.setEnabled(true);
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
    }
});