如何防止Recyclerview在CoordinatorLayout中扩展

时间:2017-02-20 07:06:44

标签: android android-recyclerview parallax android-coordinatorlayout android-appbarlayout

我想用coordinatorlayout制作视差标题。我将recyclerview放入coordinatorlayout。有用。但recyclerview加载所有数据,但我将数据列表分成页面。每个滚动的最后一项,我想加载下一页。但它在开始时加载所有数据。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <FrameLayout
                    android:id="@+id/bannerFrame"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true"
                    app:layout_collapseMode="parallax" />

            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:id="@+id/nestedScrollView"
        android:layout_height="wrap_content">
        <FrameLayout
            android:id="@+id/productListFrame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.v4.widget.NestedScrollView>




</android.support.design.widget.CoordinatorLayout>

MainActivity.java

 AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appbar);
        appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if(state == State.COLLAPSED){
                    Log.i("ScrollView", "COLLAPSED");
                }
                else {

                    Log.i("ScrollView", "UNCOLLAPSED");
                }

            }
        });


        BannerFragment bannerFragment = BannerFragment.newInstance();
        ProductListFragment productListFragment = ProductListFragment.newInstance();

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.addToBackStack("Banner");
        ft.add(R.id.bannerFrame, bannerFragment, "Banner");
        ft.commit();

        ft = getSupportFragmentManager().beginTransaction();

        ft.addToBackStack("ProductList");
        ft.add(R.id.productListFrame, productListFragment, "ProductList");
        ft.commit();

ProductListFragment.java

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);

        mAdapter = new MoviesAdapter(movieList);
        GridLayoutManager mLayoutManager = new GridLayoutManager(getContext(), 1);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setHasFixedSize(true);

        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener((GridLayoutManager)recyclerView.getLayoutManager()) {
            @Override
            public void onLoadMore(int current_page) {
                if(current_page == 2){
                    prepareMovieData2();
                }
            }
        });
        recyclerView.setAdapter(mAdapter);
        prepareMovieData();
    }

1 个答案:

答案 0 :(得分:0)

如果将Match_Parent attr设置为RecyclerView,则应该为Recyclerview设置固定高度,或者在NestedScrollView中设置FrameLayout