【发布时间】:2020-10-09 16:02:53
【问题描述】:
我设置了一个跨度为 2 的 GridLayoutManager,理想情况下,它应该在 2 列中显示我的元素,但输出只显示一个元素。
下面是代码:
适配器
public class MemoriesAdapter extends RecyclerView.Adapter<MemoriesAdapter.MemoriesViewHolder> {
private static final String TAG = "MemoriesAdapter";
private final LayoutInflater mInflater;
private final List<List<String>> titleList;
private final List<List<StorageReference>> imageList;
public MemoriesAdapter(Context context, List<List<String>> titleList, List<List<StorageReference>> imageList) {
this.mInflater = LayoutInflater.from(context);
this.titleList = titleList;
this.imageList = imageList;
Log.d(TAG, "Constructor ");
}
@NonNull
@Override
public MemoriesAdapter.MemoriesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.memories_cards, parent, false);
MemoriesAdapter.MemoriesViewHolder viewHolder = new MemoriesAdapter.MemoriesViewHolder(view);
Log.d(TAG, "onCreateViewHolder");
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MemoriesAdapter.MemoriesViewHolder holder, int position) {
if (!(titleList.isEmpty() || imageList.isEmpty())) {
Log.d(TAG, "Lists are not empty, proceeding.");
String title = String.valueOf(titleList.get(position));
holder.title.setText(title);
// TODO: get storage ref into the imageview
}
}
@Override
public int getItemCount() {
return titleList.size();
}
public class MemoriesViewHolder extends RecyclerView.ViewHolder {
private ImageView image;
private TextView title;
private ImageView image2;
private TextView title2;
public MemoriesViewHolder(@NonNull View itemView) {
super(itemView);
Log.d(TAG, "ViewHolder");
image = itemView.findViewById(R.id.memory_image_one);
title = itemView.findViewById(R.id.memory_title);
}
}
}
片段
memoriesViewModel.getTitleList().observe(getViewLifecycleOwner(), titList -> {
titleList.add(titList);
memoriesAdapter.notifyDataSetChanged();
Log.d(TAG, "Memories Frag " + titleList);
});
GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
memoryRecyclerView.setHasFixedSize(true);
memoryRecyclerView.setLayoutManager(manager);
memoryRecyclerView.setAdapter(memoriesAdapter);
item_layout
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/memory_dashboard_card"
android:layout_width="180dp"
android:layout_height="170dp"
android:background="@drawable/memories_card_yellow"
>
<ImageView
android:id="@+id/memory_image_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="60dp"
android:src="@drawable/unicorn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView
android:id="@+id/memory_title"
android:layout_width="108dp"
android:layout_height="21dp"
android:text="My first date was epic"
android:textSize="16sp"
android:textColor="#000"
android:textStyle="bold"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/memory_image_one" />
</androidx.constraintlayout.widget.ConstraintLayout>
布局
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/memories_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:spanCount="2"
tools:listitem="@layout/memories_cards" />
如果您需要更多代码,请发表评论,如果我提供了任何不必要的细节,请进行编辑
谢谢!
编辑:我已经添加了整个适配器,我正在删除布局文件。
【问题讨论】:
-
在card_view中你设置了视图的宽度和高度吗??
-
是的,我确实设置了高度和宽度
-
显示完整的适配器和项目布局
-
我已经完成了更改。
-
尝试使用 StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
标签: android xml android-studio android-recyclerview gridlayoutmanager