【问题标题】:Set RelativeLayout over RecyclerView在 RecyclerView 上设置 RelativeLayout
【发布时间】:2017-12-17 22:20:10
【问题描述】:

我想在 RecyclerView 上设置 RelativeLayout。 (我尝试过 listView 也无法正常工作,我们不希望 Framelayout 在顶部。)

我不想要替代品,我想了解为什么它不符合逻辑 - 它与其他布局一起使用,为什么不与 recyclerview 一起使用。

1) XML 文件 (RecyclerVIew)

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

2) XML 文件(相对布局)

<?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="match_parent"
    android:orientation="vertical">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/viewcenter"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:id="@+id/imgNoDataFound"
        android:src="@drawable/no_data_found" />


</RelativeLayout>

3) Java 代码

RecyclerView recyclerView = findViewById(R.id.recycler);
     View view = LayoutInflater.from(context).inflate(com.commonlib.R.layout.no_data_found,  recyclerView);

它不起作用 - 意味着它没有在 recyclerview 上夸大我的相对布局。

注意:它也不适用于 ListView。 我不想使用 FrameLayout。

【问题讨论】:

  • 不工作是什么意思?能详细点吗?
  • 你到底想达到什么目的?正确解释。
  • @tahsinRupam 它不会在 recyclerview 上夸大我的相对布局。
  • @ADM 我想在 recyclerview 上设置 nodata found 视图。我不想为它使用任何单独的布局,只是直接在 recyclerview 上膨胀
  • @Munir 首先,即使您想要直接在 RecyclerView 内扩展视图,您也应该明白,这不是您应该如何实现所需的 UI .其次,是什么让您说您对inflate() 的呼叫不起作用?仅仅因为一个视图有一个孩子并不意味着它需要绘制它。

标签: android android-layout android-recyclerview android-relativelayout


【解决方案1】:

您可以按照以下方法:

首先,将RelativeLayout 添加到具有RecyclerView 的同一个xml 文件中,并将其可见性 设置为gone

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:visibility="gone" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/viewcenter"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:id="@+id/imgNoDataFound"
        android:src="@drawable/no_data_found" />

</RelativeLayout>

然后,在没有数据的情况下让RelativeLayout'可见。在活动课上:

if(noData){   //Or list.isEmpty() or something
    relativeLayout.setVisibility(View.VISIBLE);
}

您仍然可以替换您的recyclerView,如下所示;

View recyclerView = findViewById(R.id.recyclerView);
ViewGroup parent = (ViewGroup) recyclerView.getParent();
int index = parent.indexOfChild(recyclerView);
parent.removeView(recyclerView);
View myInflatedView = getLayoutInflater().inflate(R.layout.no_data_found, parent, false);
parent.addView(myInflatedView, index);

【讨论】:

  • 感谢您的回复,但我想知道它在逻辑上不膨胀而不是替代品的原因,我感谢您的回复。
  • 如果您想替换recyclerView,您可以在我更新的答案中尝试上述方法。但我更喜欢第一种方法,因为它更容易。选择是你的 :)
  • 兄弟,你能检查一下你编辑的代码吗,第一行写着 recyclerview.getparent();如果你在第一行之后写第二行,你怎么能得到recyclerview。
  • 第二个 - recyclerView = getLayoutInflater() 在这个方法中我们需要 recyclerview 但你正在传递视图。 - 我必须使用 (RecyclerView)
  • after - 第 5 行应用程序将崩溃,请运行​​代码。 recyclerView = (RecyclerView) getLayoutInflater().inflate(R.layout.no_data_found, parent, false);
【解决方案2】:

您正试图在此处为RecyclerView 中的RelativeLayout 充气。如果你想在它上面充气,你必须在 Recycler 的父级内充气,如下所示:

RecyclerView recyclerView = findViewById(R.id.recycler);
View view = LayoutInflater.from(context).inflate(com.commonlib.R.layout.no_data_found,  recyclerView.getParent());

【讨论】:

    【解决方案3】:

    对于错误视图,您可以将相对 Layout 和 errorView 包装在相对布局中。

    <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">
    
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_fragmentCardStack"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:dividerHeight="0dp"
        android:drawSelectorOnTop="false" />
    
    <include
        android:id="@+id/error_view"
        layout="@layout/view_error"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:visibility="gone" />
    
    </RelativeLayout>
    

    view_error.xml 可以是 .

    <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">
    
    <ImageView
        android:id="@+id/img_errorImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/txt_errorMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:visibility="gone" />
    <Button
        android:id="@+id/btn_Error_Retry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Retry"
        android:textAllCaps="false" />
    </RelativeLayout>
    

    在没有数据或任何连接错误的情况下,通过正确的消息显示错误视图。

    要在列表视图中添加空视图,您可以使用下面的代码。

      View view = LayoutInflater.from(context).inflate(R.layout.yourlayoutId,null);
        listView.setEmptyView(view);
    

    【讨论】:

    • 感谢您的回复,但我想知道它在逻辑上不膨胀而不是替代品的原因,我感谢您的回复。
    • 好的,你在哪里添加了这个视图?我没有看到任何代码。你只是膨胀视图。
    • 我想使用视图组“膨胀”,检查我上面提到的 java 代码。
    • 只有两行java代码。我问你是如何添加这个视图的?
    • 仅供参考,回收站视图没有任何 setEmptyView 功能。所以你必须按照上面的方式来做。 ListView 确实有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多