将 Recylerview 置于透明操作栏下方?
Placing Recylerview beneath transparent actionbar?
我目前有一个 recylerView
和 actionBar
透明的片段。我正在尝试在 recylerView
的顶部创建一个 space,以便在查看 recylerView 的顶部时,我的 actionBar
不会立即与我的 recylerView
重叠。这是我要创建的效果(取自默认图库应用程序):
这是我目前在自己的应用程序中拥有的内容:
如何在我的 recylerView 之前让顶部的白色小 space 使我的透明 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"/>
在其下添加一个View
和RecyclerView
:
<?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>
输出:
原来我需要像这样将 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
和 actionBar
透明的片段。我正在尝试在 recylerView
的顶部创建一个 space,以便在查看 recylerView 的顶部时,我的 actionBar
不会立即与我的 recylerView
重叠。这是我要创建的效果(取自默认图库应用程序):
这是我目前在自己的应用程序中拥有的内容:
如何在我的 recylerView 之前让顶部的白色小 space 使我的透明 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"/>
在其下添加一个View
和RecyclerView
:
<?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>
输出:
原来我需要像这样将 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 而不是整个片段中滚动。