RecyclerView + ImageView 滚动
RecyclerView + ImageView scrolling
我有一个水平 RecyclerView
和一个位于 RecyclerView
下方的 ImageView
这是相关的 xml 代码:
<android.support.v7.widget.RecyclerView
android:id="@+id/my_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/upArrow">
</android.support.v7.widget.RecyclerView>
<!-- TODO: This view should be part of the recyclerview scroll, we should find a solution for this -->
<ImageView
android:id="@+id/upArrow"
android:layout_marginTop="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_my_up_triangle_vector"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
我想实现如下滚动效果:
当尝试滚动其下方的 RecyclerView
and/or 和 ImageView
时,RecyclerView
将相应地滚动,并且 imageview 将保持其原始状态地方。
基本上我想要实现的是扩展 RecyclerView
滚动边界以包括所有 Imageview
的高度
我正在考虑设置一个包含这两个视图的 ScrollView
,但我不确定接下来该怎么做。
在你的 xml 文件中
<RelativeLayout
android:layout_width="match_parent"
android:layout_width="match_parent">
<!-- TODO: This view should be part of the recyclerview scroll, we should find a solution for this -->
<ImageView
android:id="@+id/upArrow"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:src="@drawable/ic_my_up_triangle_vector"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<android.support.v7.widget.RecyclerView
android:id="@+id/my_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:paddingBottom="40dp">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
这段代码的作用是将 RecyclerView 放在 ImageView 的顶部,底部的填充用于显示底部的图像。相应地更改 ImageView 的高度和 RecyclerView 的填充。
我看到你的问题已经有一些反对票。我想这些是针对你的问题,实际上并不清楚,你试图实现的解决方案也不是正确的方法。
从您的布局代码中,我了解到您需要一个向上箭头按钮,单击它会滚动到 RecyclerView
的顶部,并且您希望该按钮留在您所在页面的底部。您希望 RecyclerView
按原样滚动,而箭头按钮保持原样。如果是这种情况,那么您需要重新考虑布局和 Activity
流程设计。您需要使 RecyclerView
和箭头按钮完全不相交。
因此,根据我对您问题的假设,您需要一个 Activity
,其中包含一个浮动操作按钮并包含一个 Fragment
。 Fragment
将有 RecyclerView
使浮动操作按钮(向上箭头)和 RecyclerView
完全不相交。
所以你需要一个 Activity
并且 Activity
的布局应该是这样的。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/activity_horizontal_margin"
android:src="@drawable/ic_up_arrow"
app:backgroundTint="@color/colorPrimary"
app:borderWidth="0dp"
app:elevation="4dp"
app:fabSize="normal" />
</RelativeLayout>
而 Activity
的 onCreate
函数应该是这样的。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mFabUpArrow = (FloatingActionButton) findViewById(R.id.fab_up);
mFabUpArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
jumpToTopOfTheRecyclerView();
}
});
// Now launch the Fragment from here.
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new RecyclerViewFragment()).commit();
}
现在 RecyclerViewFragment
应该有下面的布局来容纳 RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/your_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
而且我认为您已经知道如何填充 RecyclerView
并在 Fragment
中执行该操作。
您可能会问如何在Activity
中实现jumpToTopOfTheRecyclerView()
功能。它很简单,您有多种选择。一种方法可能是在您的 Fragment
中使用 BroadcastReceiver
并从您的 Activity
发送广播,这不是很好的方法。您可能会发现您的 Activity
和 Fragment
之间的交流非常容易。祝你好运。
您可以将 header 和 RecyclerView 放在 NestedScrollView 中:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/upArrow">
</android.support.v7.widget.RecyclerView>
<ImageView
android:id="@+id/upArrow"
android:layout_marginTop="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_my_up_triangle_vector"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
并在 Java 代码中使 mRecyclerView.setNestedScrollingEnabled(false);正确滚动学分
check this anser
我有一个水平 RecyclerView
和一个位于 RecyclerView
ImageView
这是相关的 xml 代码:
<android.support.v7.widget.RecyclerView
android:id="@+id/my_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/upArrow">
</android.support.v7.widget.RecyclerView>
<!-- TODO: This view should be part of the recyclerview scroll, we should find a solution for this -->
<ImageView
android:id="@+id/upArrow"
android:layout_marginTop="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_my_up_triangle_vector"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
我想实现如下滚动效果:
当尝试滚动其下方的 RecyclerView
and/or 和 ImageView
时,RecyclerView
将相应地滚动,并且 imageview 将保持其原始状态地方。
基本上我想要实现的是扩展 RecyclerView
滚动边界以包括所有 Imageview
的高度
我正在考虑设置一个包含这两个视图的 ScrollView
,但我不确定接下来该怎么做。
在你的 xml 文件中
<RelativeLayout
android:layout_width="match_parent"
android:layout_width="match_parent">
<!-- TODO: This view should be part of the recyclerview scroll, we should find a solution for this -->
<ImageView
android:id="@+id/upArrow"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:src="@drawable/ic_my_up_triangle_vector"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<android.support.v7.widget.RecyclerView
android:id="@+id/my_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:paddingBottom="40dp">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
这段代码的作用是将 RecyclerView 放在 ImageView 的顶部,底部的填充用于显示底部的图像。相应地更改 ImageView 的高度和 RecyclerView 的填充。
我看到你的问题已经有一些反对票。我想这些是针对你的问题,实际上并不清楚,你试图实现的解决方案也不是正确的方法。
从您的布局代码中,我了解到您需要一个向上箭头按钮,单击它会滚动到 RecyclerView
的顶部,并且您希望该按钮留在您所在页面的底部。您希望 RecyclerView
按原样滚动,而箭头按钮保持原样。如果是这种情况,那么您需要重新考虑布局和 Activity
流程设计。您需要使 RecyclerView
和箭头按钮完全不相交。
因此,根据我对您问题的假设,您需要一个 Activity
,其中包含一个浮动操作按钮并包含一个 Fragment
。 Fragment
将有 RecyclerView
使浮动操作按钮(向上箭头)和 RecyclerView
完全不相交。
所以你需要一个 Activity
并且 Activity
的布局应该是这样的。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/activity_horizontal_margin"
android:src="@drawable/ic_up_arrow"
app:backgroundTint="@color/colorPrimary"
app:borderWidth="0dp"
app:elevation="4dp"
app:fabSize="normal" />
</RelativeLayout>
而 Activity
的 onCreate
函数应该是这样的。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mFabUpArrow = (FloatingActionButton) findViewById(R.id.fab_up);
mFabUpArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
jumpToTopOfTheRecyclerView();
}
});
// Now launch the Fragment from here.
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new RecyclerViewFragment()).commit();
}
现在 RecyclerViewFragment
应该有下面的布局来容纳 RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/your_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
而且我认为您已经知道如何填充 RecyclerView
并在 Fragment
中执行该操作。
您可能会问如何在Activity
中实现jumpToTopOfTheRecyclerView()
功能。它很简单,您有多种选择。一种方法可能是在您的 Fragment
中使用 BroadcastReceiver
并从您的 Activity
发送广播,这不是很好的方法。您可能会发现您的 Activity
和 Fragment
之间的交流非常容易。祝你好运。
您可以将 header 和 RecyclerView 放在 NestedScrollView 中:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/upArrow">
</android.support.v7.widget.RecyclerView>
<ImageView
android:id="@+id/upArrow"
android:layout_marginTop="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_my_up_triangle_vector"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
并在 Java 代码中使 mRecyclerView.setNestedScrollingEnabled(false);正确滚动学分 check this anser