Java 回收器视图不显示任何无错误的项目
您好,我是android devolpment的新手,我面临一个一周内无法解决的错误,错误是我的回收器视图中没有显示任何项目,我真的不知道为什么我阅读了有关stackoverflow和Documanation的所有问题,但没有任何帮助,我希望您能帮助我。 活动性:Java 回收器视图不显示任何无错误的项目,java,android,android-recyclerview,Java,Android,Android Recyclerview,您好,我是android devolpment的新手,我面临一个一周内无法解决的错误,错误是我的回收器视图中没有显示任何项目,我真的不知道为什么我阅读了有关stackoverflow和Documanation的所有问题,但没有任何帮助,我希望您能帮助我。 活动性: }我在代码中发现了您的错误。 代码中有两个问题 BookRecViewAdapter.java @Override public void onBindViewHolder(@NonNull ViewHolder holder, fi
}我在代码中发现了您的错误。 代码中有两个问题 BookRecViewAdapter.java
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: Working!!!!!");
......................
...................
if (books.get(position).isExpanded()) {
............................
} else {
TransitionManager.beginDelayedTransition(holder.parent);
holder.expandedRealtiveLayout.setVisibility(View.GONE); // there is problem!!!
holder.downArrow.setVisibility(View.VISIBLE);
}
}
list_item_book.xml
<CardView>
<RelativeLayout>
<RelativeLayout android:id="@+id/expandedRelativelayout"></RelativeLayout> // there is problem
<RelaitveLayout
android:layout_below="@+id/expandedRealtivelayout" // there is problem
android:visibility="gone"></RelativeLayout>
</RelativeLayout>
</CardView>
//有问题
您将expandedRelativelayout的可见性设置为“进入适配器”。
而其他相对年轻人的可见性已经消失了
因此,您的屏幕中没有可见的视图。
我想您在xml文件中写错了@id。
expandedRelativelayout应该是第二个RelativeLayout。
就这样
<CardView>
<RelativeLayout>
<RelativeLayout android:id="@+id/anyLayout"></RelativeLayout>
<RelaitveLayout
android:id="@+id/expandedRelativelayout"
android:layout_below="@+id/anylayout"
android:visibility="gone"></RelativeLayout>
</RelativeLayout>
</CardView>
请检查您自己,并正确制作。您能同时发布xml和Book类吗?然后我将使用您的代码进行测试。
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/parent"
app:cardCornerRadius="7dp"
app:cardElevation="7dp"
android:layout_margin="5dp"
android:layout_centerHorizontal="true"
>
<RelativeLayout
android:layout_width="200dp"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/expandedRealtivelayout"
>
<ImageView
android:layout_width="130dp"
android:layout_height="150dp"
android:id="@+id/imgBook"
android:visibility="visible"
android:src="@drawable/ic_launcher_background"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Book Name"
android:layout_below="@+id/imgBook"
android:textSize="16sp"
android:textStyle="bold|italic"
android:textColor="@color/black"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:id="@+id/txtBookName"
android:visibility="visible"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_arrow_down"
android:layout_alignParentEnd="true"
android:layout_marginTop="150dp"
android:id="@+id/btnArrowDown"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/expandedRealtivelayout"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Author:"
android:textSize="14sp"
android:textColor="@color/black"
android:id="@+id/author"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/author"
android:text="Name Of Author"
android:textSize="16sp"
android:textColor="@color/black"
android:id="@+id/txtAuthor"
android:textStyle="italic|bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txtAuthor"
android:text="Short Description"
android:textSize="16sp"
android:textColor="@color/black"
android:id="@+id/txtShortDesc"
android:textStyle="italic|bold"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="40dp"
android:src="@drawable/ic_arrow_up_right_one"
android:id="@+id/btnArrowUpp"/>
</RelativeLayout>
</RelativeLayout>
public class Book {
private int id;
private String name;
private String author;
private int pages;
private String imageUrl;
private String shortDesc;
private String longDesc;
private boolean isExpanded;
public Book(int id, String name, String author, int pages, String imageUrl, String shortDesc, String longDesc) {
this.id = id;
this.name = name;
this.author = author;
this.pages = pages;
this.imageUrl = imageUrl;
this.shortDesc = shortDesc;
this.longDesc = longDesc;
this.isExpanded = false;
}
public boolean isExpanded() {
return isExpanded;
}
public void setExpanded(boolean expanded) {
isExpanded = expanded;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getShortDesc() {
return shortDesc;
}
public void setShortDesc(String shortDesc) {
this.shortDesc = shortDesc;
}
public String getLongDesc() {
return longDesc;
}
public void setLongDesc(String longDesc) {
this.longDesc = longDesc;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", pages=" + pages +
", imageUrl='" + imageUrl + '\'' +
", shortDesc='" + shortDesc + '\'' +
", longDesc='" + longDesc + '\'' +
'}';
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: Working!!!!!");
......................
...................
if (books.get(position).isExpanded()) {
............................
} else {
TransitionManager.beginDelayedTransition(holder.parent);
holder.expandedRealtiveLayout.setVisibility(View.GONE); // there is problem!!!
holder.downArrow.setVisibility(View.VISIBLE);
}
}
<CardView>
<RelativeLayout>
<RelativeLayout android:id="@+id/expandedRelativelayout"></RelativeLayout> // there is problem
<RelaitveLayout
android:layout_below="@+id/expandedRealtivelayout" // there is problem
android:visibility="gone"></RelativeLayout>
</RelativeLayout>
</CardView>
<CardView>
<RelativeLayout>
<RelativeLayout android:id="@+id/anyLayout"></RelativeLayout>
<RelaitveLayout
android:id="@+id/expandedRelativelayout"
android:layout_below="@+id/anylayout"
android:visibility="gone"></RelativeLayout>
</RelativeLayout>
</CardView>