Если ответ не помогает, я хотел бы добавить простой код, который может дать вам некоторые подсказки.
Xml Основной файл, в котором находится мой recyclerView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/sample_recyclerview"/>
</RelativeLayout>
Xml макет, который я сделал для загрузки данных в:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="165dp"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
app:cardCornerRadius="5dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="120dp"
tools:srcCompat="@drawable/download"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@color/pinkish"
android:gravity="center"
android:shadowColor="#FFFFFF"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
Вот мой класс модели для загрузки данных:
public class RecipeModel {
private int pic;
private String text;
public RecipeModel(int pic, String text) {
this.pic = pic;
this.text = text;
}
public int getPic() {
return pic;
}
public void setPic(int pic) {
this.pic = pic;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
А вот класс адаптера показывает, как загрузить данные из класса модели и загрузить в созданный макет внутри метода адаптера Oncreate:
public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.viewHolder> {
ArrayList<RecipeModel> List;
Context context;
public RecipeAdapter(ArrayList<RecipeModel> list, Context context) {
List = list;
this.context = context;
}
@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.sample_recyclerview, parent, false);
return new viewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull viewHolder holder, int position) {
RecipeModel model = List.get(position);
holder.imageView.setImageResource(model.getPic());
holder.textView.setText(model.getText());
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Item is clicked", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return List.size();
}
public class viewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public viewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
}
}
}
и здесь вы передаете значения из действия, в котором вы создали тэг recyclerview: , и я прокомментировал другой стиль recyclerView, который можно использовать, чтобы увидеть разницу.
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycleView);
ArrayList<RecipeModel> list = new ArrayList<>();
list.add(new RecipeModel(R.drawable.cc, "A Picture"));
list.add(new RecipeModel(R.drawable.ccccc, "A Picture"));
list.add(new RecipeModel(R.drawable.ddd, "A Picture"));
list.add(new RecipeModel(R.drawable.download, "A Picture"));
list.add(new RecipeModel(R.drawable.flower2, "A Picture"));
list.add(new RecipeModel(R.drawable.images, "A Picture"));
list.add(new RecipeModel(R.drawable.littlegirl, "A Picture"));
list.add(new RecipeModel(R.drawable.pler4, "A Picture"));
list.add(new RecipeModel(R.drawable.plw, "A Picture"));
list.add(new RecipeModel(R.drawable.qwe, "A Picture"));
list.add(new RecipeModel(R.drawable.port, "A Picture"));
list.add(new RecipeModel(R.drawable.rg5fs, "A Picture"));
RecipeAdapter adapter = new RecipeAdapter(list, this);
recyclerView.setAdapter(adapter);
// LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
// recyclerView.setLayoutManager(linearLayoutManager);
// LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);
// recyclerView.setLayoutManager(linearLayoutManager);
// GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
// recyclerView.setLayoutManager(layoutManager);
StaggeredGridLayoutManager staggered = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(staggered);
}
}