我需要为Recycler视图的每个元素设置一个标题。我该怎么办呢?
这是我的java代码:
public class HorizontalAdapterViewTopDieci extends RecyclerView.Adapter<HorizontalAdapterViewTopDieci.HorizontalViewHolder>
{
private int[] items;
public HorizontalAdapterViewTopDieci(int[] items) {
this.items = items;
}
@Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_layout_recycler_view_top_10, parent, false);
return new HorizontalViewHolder(view);
}
@Override
public void onBindViewHolder(HorizontalViewHolder holder, int position) {
holder.image.setImageResource(items[position]);
holder.titleText.setText("Recipe");
}
@Override
public int getItemCount() {
return items.length;
}
public class HorizontalViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView titleText;
public HorizontalViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image_view_ricette_categorie_primi_top_10);
titleText = itemView.findViewById(R.id.titleText);
}
} }
我还为项目创建了xml布局(在本例中为images):
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical"
>
<ImageView
android:id="@+id/image_view_ricette_categorie_primi_top_10"
android:layout_width="125dp"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:src="@drawable/test_pizza"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:layout_gravity="center_horizontal"
android:textColor="@color/colorRicette"
/>
</LinearLayout>
我尝试在这里添加文本视图,对齐并居中但是在设置此文本后我该怎么办?
更新:这是你的解决方案,但出了什么问题?
答案 0 :(得分:1)
将单个项目xml创建为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical"
>
<ImageView
android:id="@+id/image_view_ricette_categorie_primi_top_10"
android:layout_width="125dp"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher_background"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
现在将适配器修改为:
public class HorizontalAdapterImageViewTopDieci extends RecyclerView.Adapter<HorizontalAdapterImageViewTopDieci.HorizontalViewHolder>
{
private int[] items;
public HorizontalAdapterImageViewTopDieci(int[] items) {
this.items = items;
}
@Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_layout_recycler_view_immagini_top_10, parent, false);
return new HorizontalViewHolder(view);
}
@Override
public void onBindViewHolder(HorizontalViewHolder holder, int position) {
holder.image.setImageResource(items[position]);
holder.titleText.setText("Image"+position);
}
@Override
public int getItemCount() {
return items.length;
}
public class HorizontalViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView titleText;
public HorizontalViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image_view_ricette_categorie_primi_top_10);
titleText = itemView.findViewById(R.id.titleText);
}
}
}