Запомни этот код : Для создания системы оценки в карточках товаров вам нужно сделать следующее:

1. Внедрить в ваш проект библиотеку com.google.android.material:material:1.2.1 в файл build.gradle.
2. Создать класс Product для хранения информации о товаре, включающий в себя поля для хранения имени, описания, фотографии и рейтинга.
3. Создать класс ProductAdapter, который будет работать со списком товаров и представленных в карточках, и реализовать методы для отображения элементов списка.
4. Создать диалоговое окно RatingDialog для оценки товара.
5. Наконец, создать FirstFragment, который будет отображать карточки товаров и обработчик событий для взаимодействия пользователей с системой оценки.

Примерный код на языке Java:

1. Класс Product:

public class Product {
    private String name;
    private String description;
    private int imageResource;
    private float rating;

    public Product(String name, String description, int imageResource, float rating) {
        this.name = name;
        this.description = description;
        this.imageResource = imageResource;
        this.rating = rating;
    }

    public String getName() { return name; }

    public String getDescription() { return description; }

    public int getImageResource() { return imageResource; }

    public float getRating() { return rating; }

    public void setRating(float rating) { this.rating = rating; }
}


2. Класс ProductAdapter:

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
    private List<Product> products;
    private Context context;

    public ProductAdapter(Context context, List<Product> products) {
        this.context = context;
        this.products = products;
    }

    @Override
    public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_product, parent, false);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ProductViewHolder holder, int position) {
        Product product = products.get(position);
        holder.productImage.setImageResource(product.getImageResource());
        holder.productName.setText(product.getName());
        holder.productDescription.setText(product.getDescription());
        holder.productRating.setText(String.format(“%.1f”, product.getRating()));
    }

    @Override
    public int getItemCount() {
        return products.size();
    }

    public class ProductViewHolder extends RecyclerView.ViewHolder {
        ImageView productImage;
        TextView productName;
        TextView productDescription;
        TextView productRating;

        public ProductViewHolder(View view) {
            super(view);
            productImage = view.findViewById(R.id.product_image);
            productName = view.findViewById(R.id.product_name);
            productDescription = view.findViewById(R.id.product_description);
            productRating = view.findViewById(R.id.product_rating);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showRatingDialog(getAdapterPosition());
                }
            });
        }
    }

    private void showRatingDialog(final int position) {
        final RatingDialog ratingDialog = new RatingDialog(context);
        ratingDialog.setRating(products.get(position).getRating());
        ratingDialog.setOnRatingChangedListener(new RatingDialog.OnRatingChangedListener() {
            @Override
            public void onRatingChanged(float newRating) {
                products.get(position).setRating(newRating);
                notifyDataSetChanged();
            }
        });
        ratingDialog.show();
    }
}


3. Класс RatingDialog:

public class RatingDialog extends AlertDialog implements RatingBar.OnRatingBarChangeListener {
    private RatingBar ratingBar;
    private TextView ratingMessage;
    private float rating;
    private OnRatingChangedListener listener;

    public RatingDialog(Context context) {
        super(context);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_rating, null);
        ratingBar = view.findViewById(R.id.rating_bar);
        ratingMessage = view.findViewById(R.id.rating_message);
        ratingBar.setOnRatingBarChangeListener(this);
        setView(view);
    }

    public void setRating(float rating) {
        this.rating = rating;
        ratingBar.setRating(rating);
    }

    public void setOnRatingChangedListener(OnRatingChangedListener listener) {
        this.listener = listener;
    }

    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        this.rating = rating;
        String message = getContext().getString(R.string.rating_message, rating);
        ratingMessage.setText(message);
    }

    @Override
    public void onStart() {
        super.onStart();
        onRatingChanged(ratingBar, rating, true);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        rating = savedInstanceState.getFloat(“rating”);
        ratingBar.setRating(rating);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putFloat(“rating”, rating);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (listener != null) {
            listener.onRatingChanged(rating);
        }
    }

    public interface OnRatingChangedListener {
        void onRatingChanged(float newRating);
    }
}


4. Класс FirstFragment:

public class FirstFragment extends Fragment {
    private RecyclerView recyclerView;
    private ProductAdapter productAdapter;

    public FirstFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);

        // Init RecyclerView and ProductAdapter
        recyclerView = view.findViewById(R.id.recycler_view_products);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        List<Product> products = getProducts();
        productAdapter = new ProductAdapter(getContext(), products);
        recyclerView.setAdapter(productAdapter);

        return view;
    }

    private List<Product> getProducts() {
        List<Product> products = new ArrayList<>();

        Product product1 = new Product(“Product 1”, “Description 1”, R.drawable.product1, 0.0f);
        products.add(product1);

        Product product2 = new Product(“Product 2”, “Description 2”, R.drawable.product2, 0.0f);
        products.add(product2);

        Product product3 = new Product(“Product 3”, “Description 3”, R.drawable.product3, 0.0f);
        products.add(product3);

        return products;
    }