【问题标题】:Recyclerview with CoordinatorLayout and AppBarLayout带有 CoordinatorLayout 和 AppBarLayout 的 Recyclerview
【发布时间】:2017-04-11 16:07:17
【问题描述】:

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

<?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>

【问题讨论】:

  • 根据我的建议,在点击按钮时初始化具有所需布局的新适配器对象。
  • @RahulSharma 但是我怎么还有header?
  • 像现在一样将所需的标头添加到新适配器

标签: android android-recyclerview recyclerview-layout


【解决方案1】:

已编辑:

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

当您点击标签按钮时,只需使用隐藏和显示视图并为两者放置单独的布局管理器。

 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;

【讨论】:

  • 嗯,我无法访问这些列表,因为它们位于不同的活页夹中。根据您的解决方案,我需要在第一个活页夹中隐藏/显示第二个活页夹中的项目。
  • @Esteban 我已经做过 instagram 设计。您可以简单地在单个活动中使用 4 或 5 个按钮。只需根据此隐藏和显示视图即可。您需要像 this 这样的用户协调器布局.永远不要使用嵌套滚动视图。它在recyclerview中做的不好。
  • @Esteban 检查我编辑的帖子。运行该示例时,您可以获得一些清晰的标识。
  • 谢谢,这很有帮助,尽管这里的滚动也不流畅。你不能一蹴而就。 :(
  • @Esteban 我认为在那个样本中他们也只是看起来像那样。
【解决方案2】:

我认为您执行错误。不需要在 RecyclerView 项中添加 TabLayout。

你的代码应该是这样的

<android.support.design.widget.CoordinatorLayout>
     <android.support.design.widget.AppBarLayout>
          <android.support.design.widget.CollapsingToolbarLayout>

              <YourHeaderLayout />
              <android.support.design.widget.TabLayout />              

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

    <RecyclerView /> 
    <!-- OR -->
    <ViewPager />


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

这里如果你显示RecyclerView,那么你需要在Tab键上改变LayoutManager。或者你可以在ViewPager中放置两个RecyclerView和List和Grid。

【讨论】:

  • 有没有办法在没有CollapsingToolbarLayout? 的情况下做到这一点我需要内容可以滚动,比如 Instagram 用户个人资料页面。
  • 您可以在没有 CollapsingToolbarLayout 的情况下使用 AppBarLayout。对于滚动,您可以使用 android.support.v4.widget.NestedScrollView。
  • @Esteban 没有收到。关于标题滚动你想说什么?
  • 当我滚动到底部然后我跳到顶部时,我只能到达recyclerview 的顶部,而不是整个布局。我找到了一些解决方法,但它并没有真正解决问题。你有什么建议吗?
  • @Esteban 你是说什么时候滚动到顶部你的标题布局是可见的。对吗?
猜你喜欢
  • 1970-01-01
  • 2015-08-13
  • 2017-05-31
  • 2016-09-09
  • 1970-01-01
  • 2016-03-12
  • 1970-01-01
相关资源
最近更新 更多