【问题标题】:Placing Recylerview beneath transparent actionbar?将 Recylerview 放在透明操作栏下方?
【发布时间】:2018-08-16 15:50:12
【问题描述】:

我目前有一个带有recylerViewactionBar 的透明片段。我正在尝试在我的recylerView 顶部创建一个空间,以便在查看 recylerView 顶部时我的actionBar 不会立即与我的recylerView 重叠。这是我试图创建的效果(取自默认图库应用):

https://imgur.com/nBuGCnG

这是我目前在我自己的应用程序中拥有的:

如何在我的 recylerView 之前获得顶部的那个小空白,以便我的透明 actionBar 不会立即与最顶部的 recylerView 行重叠?

这是相关代码:

ActivityMain.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment);


    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragment_container);
    if (fragment == null) {
        fragment = FragmentAlbumGallery.newInstance();
        fm.beginTransaction().add(R.id.fragment_container, fragment, Constants.FRAGMENT_ALBUMS_GALLERY).commit();


    }
}

FragmentAlbumGallery.java:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_recycler_gallery, container, false);
    mAlbumRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);

    mAlbumRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    //onResume() gets called here. No need to set up adapter since we do it there
   // setupAdapter();
    return v;
}

activity_fragment.xml:

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

fragment_recyler_gallery.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ActivityGallery"/>

【问题讨论】:

  • 您是否尝试过在您的回收站视图上添加等于操作栏高度的填充顶部?
  • 您的活动 XML 是什么样的?
  • 是的,我确实有!当我将填充添加到我的 recylerview 顶部时,操作栏不再透明!
  • 看看this教程。
  • 你的问题没有完整的代码,它没有告诉你如何在你的活动中设置工具栏。

标签: android android-layout android-recyclerview android-actionbar


【解决方案1】:

在其下添加ViewRecyclerView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#c4c4c4" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom" />

</LinearLayout>

输出:

【讨论】:

  • 感谢您的回复!这是我得到的结果:gph.is/2MNtGxw。如何将灰色区域固定到顶部以使其不滚动?
  • 这是另一个问题,你应该打开一个新的线程问题,但我回答希望我能提供帮助:那是Toolbar 所以,你需要CoordinatorLayout + ToolbarToolbar 的这一行:app:layout_collapseMode="pin" 示例:stackoverflow.com/a/32853390/4409113 等等。
  • 已解决!!!我需要将 RecyclerView 放在 NestedScrollView 中。原来我只是在 RecyclerView 中滚动,而不是在整个 FrameLayout 上滚动。希望这可以节省很多时间!
  • 哦,所以你试图实现滚动行为。嗯,是的,你也可以这样做,这与这个线程无关。更多问题请使用提问
【解决方案2】:

原来我需要将我的 RecyclerView 放在 NestedScrollView 中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <View
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@null" />

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

我遇到的问题是我只在 RecyclerView 中滚动,而不是整个片段。

【讨论】:

  • 然而; 问题标题:Recylerview 置于透明操作栏下方?现在,在我的 RecyclerView 而不是整个片段中滚动。恐怕与此线程无关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多