代码之家  ›  专栏  ›  技术社区  ›  Munir

在RecyclerView上设置RelativeLayout

  •  0
  • Munir  · 技术社区  · 7 年前

    我想在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文件(RelativeLayout)

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

    3 回复  |  直到 7 年前
        1
  •  1
  •   tahsinRupam    7 年前

    您可以遵循以下方法:

    首先,添加您的 RelativeLayout 在相同的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" />
    
    <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>
    

    然后,你 相对布局 ' 看得见的 当没有数据时。在活动类中:

    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);
    
        2
  •  0
  •   Vlad    7 年前

    您正试图为 RelativeLayout 内部 RecyclerView 在这里如果要在其上方充气,则必须在回收者的父级内部充气,如下所示:

    RecyclerView recyclerView = findViewById(R.id.recycler);
    View view = LayoutInflater.from(context).inflate(com.commonlib.R.layout.no_data_found,  recyclerView.getParent());
    
        3
  •  0
  •   ADM    7 年前

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

    <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\u错误。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);