将Recylerview放在透明的操作栏下方?

时间:2018-08-16 15:50:13

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

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

https://imgur.com/nBuGCnG

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

enter image description here

如何在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"/>

2 个答案:

答案 0 :(得分:0)

在其下添加一个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>

输出:

enter image description here

答案 1 :(得分:0)

结果是我需要将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中滚动而不是滚动整个片段。