Я установил GridLayoutManager с диапазоном 2, и в идеале он должен отображать мои элементы в 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>
layout
<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" />
Прокомментируйте, если вам нужен дополнительный код, или отредактируйте, если я дал ненужные детали
Спасибо!
EDIT: Я добавил весь адаптер и удаляю файлы макета.