Установить RelativeLayout поверх RecyclerView
Я хочу установить RelativeLayout поверх RecyclerView. (Я пробовал с listView, и он не работает, мы не хотим Framelayout сверху.)
Я не хочу альтернатив, я хотел понять, почему он не работает логически - он работает с другим макетом, а не с переработчиком.
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);
Это не работает - означает, что это не раздувает мой относительный план по представлению переработчика.
Примечание: это не работает с ListView также. Я не хочу брать FrameLayout.
3 ответа
Вы пытаетесь раздуть RelativeLayout
внутри RecyclerView
Вот. Если вы хотите надуть его выше, вы должны надуть его внутри родителя Утилизатора, вот так:
RecyclerView recyclerView = findViewById(R.id.recycler);
View view = LayoutInflater.from(context).inflate(com.commonlib.R.layout.no_data_found, recyclerView.getParent());
Вы можете использовать следующий метод:
Во-первых, добавьте свой 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>
Затем вы делаете 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);
Для просмотра ошибок вы можете обернуть вашу относительную компоновку и 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);