RecyclerView条目不显示问题排查求助
Android RecyclerView条目不显示问题求助
在Android开发中遇到RecyclerView条目不显示的问题,页面完全空白,已做以下排查:
- folderList的长度为2
- Adapter的
getItemCount()返回值为2 - 尝试重新创建Adapter,问题依然存在
相关代码如下:
FolderListAdapter 代码
public class FolderListAdapter extends RecyclerView.Adapter<FolderListAdapter.myViewHolder> { private final Context context; private final ArrayList<Folder> folderList; public FolderListAdapter(Context context, ArrayList<Folder> folderList) { this.context = context; this.folderList = folderList; } @NonNull @Override public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.folder_view, parent, false); return new myViewHolder(view); } @Override public void onBindViewHolder(@NonNull myViewHolder holder, int position) { Folder folder = folderList.get(position); Glide.with(context).load(folder.getFolder()).into(holder.imageView); holder.folder_name.setText(folder.getFolder().getName()); holder.folder_item_count.setText(folder.getFilesList().size() + " Items"); } @Override public int getItemCount() { return folderList.size(); } public class myViewHolder extends RecyclerView.ViewHolder { private final ShapeableImageView imageView; private final TextView folder_name; private final TextView folder_item_count; public myViewHolder(@NonNull View itemView) { super(itemView); imageView = itemView.findViewById(R.id.folder_image_view); folder_name = itemView.findViewById(R.id.folder_name_text_view); folder_item_count = itemView.findViewById(R.id.folder_items_count_text_view); } } }
RecyclerView 初始化代码
FolderListAdapter adapter = new FolderListAdapter(this, folderList); RecyclerView recyclerView = binding.folderRecyclerView; recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
RecyclerView 布局XML(预览配置)
<androidx.recyclerview.widget.RecyclerView android:id="@+id/folder_recyclerView" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" app:spanCount="2" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:listitem="@layout/folder_view" />
条目布局 folder_view.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.imageview.ShapeableImageView android:id="@+id/folder_image_view" android:layout_width="150dp" android:layout_height="150dp" android:layout_marginTop="10dp" android:scaleType="centerCrop" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:background="?android:attr/selectableItemBackground" app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.CornerSize50Percent" tools:srcCompat="@tools:sample/backgrounds/scenic" /> <TextView android:id="@+id/folder_name_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:text="Camera" android:textColor="@android:color/primary_text_light" android:textSize="16sp" app:layout_constraintEnd_toEndOf="@+id/folder_image_view" app:layout_constraintStart_toStartOf="@+id/folder_image_view" app:layout_constraintTop_toBottomOf="@+id/folder_image_view" /> <TextView android:id="@+id/folder_items_count_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="600 Items" android:textSize="12sp" app:layout_constraintStart_toStartOf="@+id/folder_name_text_view" app:layout_constraintTop_toBottomOf="@+id/folder_name_text_view" /> </androidx.constraintlayout.widget.ConstraintLayout>
页面布局 activity_folder.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ui.addFiles.FoldersActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/folder_recyclerView" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
运行效果

求帮忙排查为何页面没有显示任何内容。
内容的提问来源于stack exchange,提问作者Hammad Zafar Bawara
相关产品推荐
相关产品推荐

