我如何在 java android 的 Gridview 的父 class BaseAdapter 中实现 viewBinding?

How do i implement viewBinding in parent class BaseAdapter for Gridview in java android?

这是我的BaseAdapter class,我想在这里实现视图绑定。我是怎么做到的。从而我可以减少没有。我的应用程序中的行数。

public class MyAdapter extends BaseAdapter {

    Context c;
    int items[];

    MyAdapter(Context c, int arr[]) {
        this.c = c;
        items = arr;
    }
    @Override
    public int getCount() {
        return items.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.grid_layout, null);
        }

        ImageView imageView = convertView.findViewById(R.id.imageView);
        imageView.setImageResource(items[position]);
        return convertView;
    }
}

这是MainActivity.java文件,对基础适配器的调用来自这个class文件

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        int[] itemsarray = new int[] {
                R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
                R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
                R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
                R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
                R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
                R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
        };

        MyAdapter adapter = new MyAdapter(this, itemsarray);
        binding.gvImages.setAdapter(adapter);
    }
}

这是activity_main.xml,MainActivity.javaclass的xml文件。这里都是我们设计的地方

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <GridView
        android:id="@+id/gvImages"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="auto_fit"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

这个idgrid_layout.xml,也就是这个xml文件是用来自定义grid视图中的view的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="center"
        tools:srcCompat="@tools:sample/avatars" />
</LinearLayout>```

*Thanks in advance*

像这样更改您的 BaseAdapter 代码


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.aijishnu.baseadaptersample.databinding.GridLayoutBinding;

public class MyAdapter extends BaseAdapter {

    Context c;
    int items[];

    MyAdapter(Context c, int arr[]) {
        this.c = c;
        items = arr;
    }
    @Override
    public int getCount() {
        return items.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {

            GridLayoutBinding itemBinding = GridLayoutBinding.inflate(LayoutInflater.from(parent.getContext()), parent,false);

            holder = new ViewHolder(itemBinding);
            holder.view = itemBinding.getRoot();
            holder.view.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();
        }
        holder.binding.imageView.setImageResource(items[position]);
        return holder.view;
    }
    
    private static class ViewHolder {
        private View view;
        private GridLayoutBinding binding;

        ViewHolder(GridLayoutBinding binding) {
            this.view = binding.getRoot();
            this.binding = binding;
        }
    }
}