在 RecyclerView 上设置 RelativeLayout
Set RelativeLayout over RecyclerView
我想在 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。
对于错误视图,您可以将相对布局和错误视图包装在相对布局中。
<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);
您可以按照以下方法:
首先,将您的 RelativeLayout
添加到具有 RecyclerView
的同一 xml 文件中,并将其 visibility 设置为 走了.
<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
'可见。在 activity class:
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
内的 RelativeLayout
充气。如果你想在它上面膨胀,你必须在 Recycler 的父级内部膨胀它,像这样:
RecyclerView recyclerView = findViewById(R.id.recycler);
View view = LayoutInflater.from(context).inflate(com.commonlib.R.layout.no_data_found, recyclerView.getParent());
我想在 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。
对于错误视图,您可以将相对布局和错误视图包装在相对布局中。
<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);
您可以按照以下方法:
首先,将您的 RelativeLayout
添加到具有 RecyclerView
的同一 xml 文件中,并将其 visibility 设置为 走了.
<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
'可见。在 activity class:
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
内的 RelativeLayout
充气。如果你想在它上面膨胀,你必须在 Recycler 的父级内部膨胀它,像这样:
RecyclerView recyclerView = findViewById(R.id.recycler);
View view = LayoutInflater.from(context).inflate(com.commonlib.R.layout.no_data_found, recyclerView.getParent());