使其可见后,RecyclerView进入屏幕顶部

时间:2018-08-06 08:23:31

标签: android android-recyclerview android-spinner

我有一个片段,其布局如下:

<?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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/grey_background"
    android:layoutDirection="rtl">

    <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="170dp"
        android:background="@color/orange"
        android:paddingRight="20dp"
        app:collapsedTitleGravity="right|center_vertical"
        app:collapsedTitleTextAppearance="@style/CollapsedAppBar"
        app:contentScrim="@color/orange"
        app:expandedTitleGravity="bottom|center_horizontal"
        app:expandedTitleTextAppearance="@style/CollapsedAppBar"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="30dp"
            android:layout_marginLeft="10dp"
            android:src="@drawable/ic_lock_profile_prime"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layoutDirection="ltr"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/costs_nested_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="168dp">

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

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:background="@drawable/spinner_background">

                    <Spinner
                        android:id="@+id/wallet_spinner"
                        style="@style/Widget.AppCompat.DropDownItem.Spinner"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="left"
                        android:layoutDirection="rtl" />
                </RelativeLayout>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/cost_list"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:divider="@null"
                    android:dividerHeight="0dp"
                    android:scrollbars="none" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/payment_list"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:visibility="gone"
                    android:layout_weight="1"
                    android:divider="@null"
                    android:dividerHeight="0dp"
                    android:scrollbars="none" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/gift_list"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:visibility="gone"
                    android:divider="@null"
                    android:dividerHeight="0dp"
                    android:scrollbars="none" />

                <TextView
                    android:id="@+id/empty_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="@string/no_records"
                    android:visibility="gone" />
            </LinearLayout>
        </RelativeLayout>
    </android.support.v4.widget.NestedScrollView>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginBottom="@dimen/bottom_navigation_height"
        android:layout_gravity="bottom"
        android:background="@color/blue_2"
        android:padding="10dp">

        <TextView
            android:id="@+id/rent_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:paddingBottom="5dp"
            android:drawablePadding="10dp"
            android:drawableRight="@drawable/ic_plus"
            android:textColor="@color/colorPrimary"
            android:textSize="15sp" />

        </RelativeLayout>

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

我希望在选择 Spinner 的特定项目并隐藏其他 RecyclerView 时,使每个 RecyclerView 可见。第一次选择每个项目都是可以的,但是从选择项目后的第二次开始, RecyclerView 会转到屏幕顶部,微调器会在工具栏后面。我的微调器代码实现是:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

            switch (position) {
                case 0:
                    if (costs != null) {

                        paymentRecyclerView.setVisibility(View.GONE);
                        costRecyclerView.setVisibility(View.VISIBLE);
                    } else {

                        getCostData();
                    }
                    break;
                case 1:
                    if (payments != null) {

                        costRecyclerView.setVisibility(View.GONE);
                        paymentRecyclerView.setVisibility(View.VISIBLE);
                    } else {

                        getPaymentData();
                    }
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });

我该如何解决我的问题?

1 个答案:

答案 0 :(得分:2)

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