2017-06-23 114 views
-3

我有一个水平RecyclerView和位于下是RecyclerViewRecyclerView + ImageView的滚动

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和/或其下面的ImageView时,RecyclerView将相应地滚动,并且imagev视图将保留在原来的位置。 基本上我想要实现的是扩大RecyclerView滚动边界包括所有Imageview的高度

我在想设置ScrollView换行这两种意见,但我真的不知道该怎么办然后。

回答

1

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

这段代码的作用是它把你的ImageView的顶部您RecyclerView,并在底部填充用来显示在底部的图像。相应地更改RecyclerView的ImageView和Padding的高度。

+0

这是正确的答案,谢谢! – idish

+0

@idish欢迎您,很高兴我能提供帮助 –

0

您已经在我看到的问题中得到了一些回复。我想这些都是针对你的问题,这个问题实际上并不清楚,你试图达到的解决方案也不是这样做的正确方法。

从您的布局代码中,我明白了您需要一个向上箭头按钮,当点击该按钮时,它将滚动到RecyclerView的顶部,并且您希望该按钮保持在页面底部'你需要你的RecyclerView滚动,箭头按钮保持原状。如果这是你的情况,那么你需要重新考虑布局和流程设计。您需要使RecyclerView和箭头按钮完全脱节。

因此,根据我对您的问题的假设,您需要一个Activity,其中包含一个浮动操作按钮并保存FragmentFragment将使RecyclerView使浮动动作按钮(用于向上箭头)和RecyclerView完全不相交。

所以你需要有一个ActivityActivity应该是这样的布局。

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

而且ActivityonCreate功能应该是这样的。

@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发送广播,这不是一个好方法。您可能会发现在您的ActivityFragment之间非常容易沟通。祝你好运。

0

你只需把你的头,你的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(假);滚动正确信用 check this anser