如何在 android java 中使用接口和 Cardview 的 RecyclerView 适配器中的 ViewBinding?

How to use ViewBinding in a RecyclerView Adapter with Interface and Cardview in android java?

我总是主要需要使用 android 中的界面进行视图绑定的 Recyclerview java 有没有人帮忙做同样的事情??

首先把下面的代码放在 build.Gradle(Module) 的 android 方括号里

buildFeatures{
        viewBinding true
    }

第 1 步 - 在主 activity xml 布局中创建回收器视图

<?xml version="1.0" encoding="utf-8"?>
<!-- 1 -->
<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/recycle_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ></androidx.recyclerview.widget.RecyclerView>

</RelativeLayout>

第 2 步 - 创建自定义项目布局 xml 通过右键单击布局新建 -> 布局资源文件

<?xml version="1.0" encoding="utf-8"?>
<!-- 2 -->
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_click"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="3dp"
    app:cardCornerRadius="10dp"
    app:cardUseCompatPadding="true"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <ImageView
            android:id="@+id/tv_logo"
            android:src="@color/black"
            android:padding="5dp"
            android:layout_width="100dp"
            android:layout_height="100dp" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="5dp">
            <TextView
                android:id="@+id/tv_title"
                android:text="Amazon"
                android:textSize="25sp"
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <RatingBar
                android:id="@+id/ratingBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:progressTint="#EFD80E"
                android:rating="3"
                style="@style/Widget.AppCompat.RatingBar.Indicator" />
        </LinearLayout>
    </LinearLayout>
</androidx.cardview.widget.CardView>

第 3 步 - 在 java

中创建项目 Class
 package com.example.recyclerviewdemonewwithinterface.model;


    // 3
    public class Item {
        private int id;
        private String name;
        private float rating;
        private int image;
    
        public Item(int id, String name, float rating, int image) {
            this.id = id;
            this.name = name;
            this.rating = rating;
            this.image = image;
        }
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public float getRating() {
            return rating;
        }
        public void setRating(float rating) {
            this.rating = rating;
        }
        public int getImage() {
            return image;
        }
        public void setImage(int image) {
            this.image = image;
        }
    }

第 4 步 - 在 java

中创建 CustomItemAdapter Class
package com.example.recyclerviewdemonewwithinterface.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.recyclerviewdemonewwithinterface.SetOnClickListener;
import com.example.recyclerviewdemonewwithinterface.databinding.CustomItemLayoutBinding;
import com.example.recyclerviewdemonewwithinterface.model.Item;
import java.util.List;

// 4
public class CustomItemAdapter extends RecyclerView.Adapter<CustomItemAdapter.MyViewHolder> {
    CustomItemLayoutBinding binding;
    private Context context;
    private List<Item> itemList;
    // i3 & create constructor also
    private SetOnClickListener setOnClickListener;

    public CustomItemAdapter(Context context, List<Item> itemList, SetOnClickListener setOnClickListener) {
        this.context = context;
        this.itemList = itemList;
        this.setOnClickListener = setOnClickListener;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        binding = CustomItemLayoutBinding.inflate(LayoutInflater.from(context),parent,false);
        MyViewHolder holder = new MyViewHolder(binding);
        return holder;
    }
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Item item = itemList.get(position);
        holder.binding.tvLogo.setImageResource(item.getImage());
        holder.binding.tvTitle.setText(item.getName());
        holder.binding.ratingBar.setRating(item.getRating());

        // i4
        holder.binding.cardClick.setOnClickListener(view -> {
            setOnClickListener.onItemClick(item,position);
        });
        // click event from adapter
        /*holder.binding.cardClick.setOnClickListener(v -> {
            Toast.makeText(context, ""+item.getName(), Toast.LENGTH_SHORT).show();
        });*/
    }
    @Override
    public int getItemCount() {
        return itemList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        CustomItemLayoutBinding binding;
        public MyViewHolder(@NonNull CustomItemLayoutBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}

第 5 步 - 在 java

中创建 SetOnClickListener 接口
package com.example.recyclerviewdemonewwithinterface;

import com.example.recyclerviewdemonewwithinterface.model.Item;

// 6
public interface SetOnClickListener {
    // i1
    void onItemClick(Item item,int position);
    void onLongItemClick(Item item, int position);
}

第 6 步 - 在 Main 中应用以下代码 Activity Java

package com.example.recyclerviewdemonewwithinterface;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.recyclerviewdemonewwithinterface.adapter.CustomItemAdapter;
import com.example.recyclerviewdemonewwithinterface.databinding.ActivityMainBinding;
import com.example.recyclerviewdemonewwithinterface.model.Item;

import java.util.ArrayList;
import java.util.List;

// i2 & implement methods
public class MainActivity extends AppCompatActivity implements SetOnClickListener {
    // 5
    ActivityMainBinding binding;
    List<Item> itemList;
    CustomItemAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        prepareData();

        // i5 set interface in argument (Last this)
        adapter = new CustomItemAdapter(MainActivity.this, itemList, this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
        // to show listview horizontaly
        // LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this,LinearLayoutManager.HORIZONTAL,false);
        // to show grid view
        // GridLayoutManager layoutManager = new GridLayoutManager(MainActivity.this,3);
        // to show Straggered
        // StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        // layout manager in recycle view
        binding.recycleView.setLayoutManager(layoutManager);
        binding.recycleView.setAdapter(adapter);
    }

    private void prepareData() {
        itemList = new ArrayList<>();

        itemList.add(new Item(1, "Instagram", 3.5f, R.drawable.ic_launcher_background));
        itemList.add(new Item(2, "Pinterest", 2.5f, R.drawable.ic_launcher_background));
        itemList.add(new Item(3, "YouTube", 3.f, R.drawable.ic_launcher_background));
        itemList.add(new Item(4, "Twitter", 5.5f, R.drawable.ic_launcher_background));
        itemList.add(new Item(5, "Whatsapp", 4.5f, R.drawable.ic_launcher_background));
    }

    @Override
    public void onItemClick(Item item, int position) {
        // i6
        // Toast.makeText(this, ""+itemList.get(position).getName(), Toast.LENGTH_SHORT).show();
        //
        Toast.makeText(this, "" + item.getId()+"___"+position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLongItemClick(Item item, int position) {

    }
}