无法在自定义 BaseAdapter 提供的网格项上放置 onClickListener

Not being able to place an onClickListener on the Grid Items provided by a custom BaseAdapter

我目前有一个在片段内使用基础适配器的网格视图,我试图在单击其中一个项目时转移到另一个片段,但是 none 我在堆栈溢出中找到的解决方案有工作了。我可能会错过一些东西。

适配器

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 android.widget.TextView;

import com.licenta.joberfrontend.R;
import com.licenta.joberfrontend.rest.backend_entieties.Category;

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

public class CategoriesAdapter extends BaseAdapter {

public class ViewHolder {
    TextView textName;
    ImageView imageView;
}

private ArrayList<Category> categoryList;
public Context context;

public CategoriesAdapter(List<Category> apps, Context context) {
    this.context = context;
    this.categoryList = (ArrayList<Category>) apps;
}

@Override
public int getCount() {
    return categoryList.size();
}

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

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

@Override
public View getView(final int position, View view, ViewGroup parent) // inflating the layout and initializing widgets
{
    ViewHolder viewHolder;

    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.category_list_content, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.textName = view.findViewById(R.id.textName);
        viewHolder.imageView = view.findViewById(R.id.iconView);
        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();
    }

    // here we are setting up the names and images
    viewHolder.textName.setText(categoryList.get(position).getName());
    viewHolder.imageView.setImageResource(this.context.getResources().getIdentifier(categoryList.get(position).getCategoryIconId(), "mipmap", this.context.getPackageName()));

    return view;
}

}

Fragment 的 OnCreate

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

    final LocalStorageSaver localStorageSaver = new LocalStorageSaver(Objects.requireNonNull(getContext()));
    final ToastShower toastShower = new ToastShower();

    //REST services creation
    final RetrofitCreator retrofitCreator = new RetrofitCreator();
    final Retrofit retrofit = retrofitCreator.getRetrofit();
    final CategoryService categoryService = retrofit.create(CategoryService.class);

    final Call<List<Category>> getCategoriesRequest = categoryService.getAllCategoriesAndTheirJobs(localStorageSaver.getValueFromStorage(Constants.TOKEN));

    getCategoriesRequest.enqueue(
            new Callback<List<Category>>() {

                @Override
                public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
                    toastShower.showToast("Categories succesfully retrieved from backend.", getContext());
                    final GridView gridView = Objects.requireNonNull(getView()).findViewById(R.id.gridViewNewContract);
                    gridView.setAdapter(new CategoriesAdapter(response.body(), getActivity()));
                }

                @Override
                public void onFailure(Call<List<Category>> call, Throwable t) {
                    toastShower.showToast("There has been a problem with retrieving the categories data!", getContext());
                }
            }
    );

}

网格视图中的项目

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.cardview.widget.CardView
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:cardCornerRadius="15dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="15dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/iconView"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:src="@mipmap/ic_list"
            android:tint="@color/colorAccent" />

        <TextView
            android:id="@+id/textName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:fontFamily="sans-serif"
            android:maxLength="12"
            android:text="@string/appName"
            android:textColor="@color/colorAccent"
            android:textSize="13sp" />

    </LinearLayout>

</androidx.cardview.widget.CardView>


</RelativeLayout>

如果您需要更多信息,我很乐意提供。 我可以提一下,我已经尝试将侦听器直接放在网格上的适配器和片段中。

请试试这个

viewHolder.imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //do stuff
                }
            });