android - 带有 CoordinatorLayout 和 AppBarLayout 的 Recyclerview

标签 android android-recyclerview recyclerview-layout

根据下面的评论,我更改了我的布局。它正在工作,但是当我转到 recyclerview 中的项目底部并尝试向上滚动时,它只会向上滚动到 recyclerview 的开头。我必须再次滚动才能到达标题

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_scrollFlags="scroll">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/iv_profile"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="15dp"
                android:gravity="center_vertical"
                android:src="@drawable/ap_placeholder"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

            <TextView
                android:id="@+id/tv_profile_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="25dp"
                android:text="@string/profile_name"
                app:layout_constraintLeft_toRightOf="@+id/iv_profile"
                app:layout_constraintTop_toTopOf="parent"/>

            <TextView
                android:id="@+id/tv_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:text="@string/username"
                android:textStyle="bold"
                app:layout_constraintLeft_toRightOf="@+id/iv_profile"
                app:layout_constraintTop_toBottomOf="@+id/tv_profile_name"/>

            <ImageView
                android:id="@+id/iv_settings"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="33dp"
                android:contentDescription="@string/settings_content_description"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ap_settings"/>

            <TextView
                android:id="@+id/tv_profile_description"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:ellipsize="end"
                android:maxLines="2"
                android:text="@string/profile_description"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/iv_profile"/>

            <View
                android:id="@+id/view"
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_marginTop="20dp"
                android:background="@color/light_grey"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tv_profile_description"/>

            <com.roughike.bottombar.BottomBar
                android:id="@+id/bottom_bar"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_marginTop="20dp"
                android:background="@color/light_grey"
                app:bb_activeTabColor="@color/ap_yellow"
                app:bb_inActiveTabColor="@color/ap_black"
                app:bb_tabXmlResource="@xml/profile_bar_tabs"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tv_followers"/>

            <TextView
                android:id="@+id/tv_history"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:layout_marginStart="50dp"
                android:text="@string/tv_history"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/number_history"/>

            <TextView
                android:id="@+id/tv_followers"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tv_followers"
                app:layout_constraintLeft_toLeftOf="@+id/tv_history"
                app:layout_constraintRight_toRightOf="@+id/tv_following"
                app:layout_constraintTop_toBottomOf="@+id/number_followers"/>

            <TextView
                android:id="@+id/tv_following"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="40dp"
                android:layout_marginRight="40dp"
                android:text="@string/tv_following"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/number_following"/>

            <TextView
                android:id="@+id/number_history"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="@string/test_history"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="@id/tv_history"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintStart_toStartOf="@id/tv_history"
                app:layout_constraintTop_toBottomOf="@+id/view"/>

            <TextView
                android:id="@+id/number_followers"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="@string/test_followers"
                android:textStyle="bold"
                app:layout_constraintLeft_toLeftOf="@+id/number_history"
                app:layout_constraintRight_toRightOf="@+id/number_following"
                app:layout_constraintTop_toBottomOf="@+id/view"/>

            <TextView
                android:id="@+id/number_following"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="@string/test_following"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="@id/tv_following"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintStart_toStartOf="@id/tv_following"
                app:layout_constraintTop_toBottomOf="@+id/view"/>


        </android.support.constraint.ConstraintLayout>

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:visibility="visible"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

最佳答案

已编辑:

如果您需要精确的 Instagram 设计,请查看此 Sample :

当您单击选项卡按钮时,只需使用隐藏和显示 View ,并为两者放置单独的布局管理器。

 private boolean gridClick = false;


   case R.id.list_click:                       

                if(listClick) {                 // Check whether the tab is already clicked or not

                    Log.e("AlreadyClicked", "Test");

                } else {

                    Log.e("ClickedFirstTime", "Test");

                    loadPost();

                    ivGrid.setVisibility(View.GONE);      // Hiding grid recyclerview
                    ivList.setVisibility(View.VISIBLE);   // showing list recyclerview

                    rvGrid.setVisibility(View.GONE);      // Make grid recyclerview as gone
                    recyclerList.setVisibility(View.VISIBLE); // make listview as visible



                    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);       // Use separate layoutmanager for list recyclerview

                    recyclerList.setLayoutManager(mLayoutManager);
                    recyclerList.setItemAnimator(new DefaultItemAnimator());
                    recyclerList.setAdapter(friendsProfileUserPostitemsAdapter);

                    listClick = true;


                }

                break;

关于android - 带有 CoordinatorLayout 和 AppBarLayout 的 Recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43318022/

相关文章:

android - 向上滚动时用动画展开 Recyclerview 项目

android - 文本到语音应用程序无法在 android studio 的模拟器中运行,但可以在 android 设备中运行

android - 我如何在 Android 中将数据设置到适配器中

android - 将 View 模型与recyclerviews一起使用

android - 删除数据时 FirebaseRecyclerAdapter 不更新项目索引

java - 滑动删除 Android recyclerview 不起作用

android - Unity Ads 4.0 - 奖励广告的多个 OnUnityAdsShowComplete 回调

android - 在 fragment 内使用 viewpager 和 fragment 时没有 Activity 异常

java - HttpAsyncTask 返回 null 作为数据

android - 当回收站 View 中的第一项不可见但在第一项可见时隐藏时,如何在滚动上显示我的工厂?