android 如何在gridview上实现onClick事件的单个子级

pcrecxhr  于 2022-12-25  发布在  Android
关注(0)|答案(1)|浏览(124)

首先,我看了很多关于这个的资源,但是没有一个有用的,因为他们使用的是viewholder。但是我只使用了适配器类。那是为了,我想知道如何在适配器类上实现onClick事件。

    • 主要活动. java**
package com.sakibxhossain.gridlayout_practice;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.GridView;

import com.sakibxhossain.gridlayout_practice.helper.Main_Adapter;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    int[] itemsImage = new int[] {
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo
    };

    String[] itemTitle = new String[]{
            "Sorting", "Searching",
            "Stack & Queue", "Tree",
            "Graph", "Dynamic",
            "Greedy", "String",
            "Hashing"
    };

    String[] itemDifficulty = new String[]{
            "Easy",
            "Easy",
            "Easy",
            "Medium",
            "Hard",
            "Hard",
            "Hard",
            "Hard",
            "Medium"
    };

    GridView gridView;
    Main_Adapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridView = findViewById(R.id.grid_view);

        adapter = new Main_Adapter(this, itemsImage, itemTitle, itemDifficulty);
        gridView.setAdapter(adapter);
    }
}
    • 主适配器. Java**
package com.sakibxhossain.gridlayout_practice.helper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import com.sakibxhossain.gridlayout_practice.R;

import java.util.Objects;

public class Main_Adapter extends BaseAdapter {
    Context context;
    int[] item_image;
    String[] item_Title;
    String[] item_Difficulty;

    public Main_Adapter(Context context, int[] item_image, String[] item_Title, String[] item_Difficulty) {
        this.context = context;
        this.item_image = item_image;
        this.item_Title = item_Title;
        this.item_Difficulty = item_Difficulty;
    }

    @Override
    public int getCount() {
        return item_Difficulty.length;
    }

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

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

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {

        if(view == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.item,null);
        }

        ImageView item_image_assign = view.findViewById(R.id.item_image);
        TextView item_Title_assign = view.findViewById(R.id.item_name);
        TextView item_difficulty_assign = view.findViewById(R.id.item_difficulty_name);

        item_image_assign.setImageResource(item_image[position]);
        item_Title_assign.setText(item_Title[position]);
        item_difficulty_assign.setText(item_Difficulty[position]);

        if (Objects.equals(item_Difficulty[position], "Hard")) {
            item_difficulty_assign.setTextColor(Color.parseColor("#E91E63"));
        }else if(Objects.equals(item_Difficulty[position], "Medium")){
            item_difficulty_assign.setTextColor(Color.parseColor("#AA00FF"));
        }

        return view;
    }
}

有没有可能做到这一点。
我的尝试:我尝试用一个适配器类实现onClick事件gridview上的每一个单独的子节点,但是每一个都用viewholder来解释。
我的期望:我想知道是否可以在适配器类上实现onClick事件。如果可以,那么如何实现?

lsmepo6l

lsmepo6l1#

您需要使用Interface来实现您的需求:
1 -创建一个Interface,例如ClickListener

public interface ClickListener {
    void onItemClicked(int position);
}

2 -将其添加到适配器:

public class Main_Adapter extends BaseAdapter {
    Context context;
    int[] item_image;
    String[] item_Title;
    String[] item_Difficulty;
    private ClickListener clickListener;

    public Main_Adapter(Context context, int[] item_image, String[] item_Title, String[] item_Difficulty, ClickListener clickListener) {
        this.context = context;
        this.item_image = item_image;
        this.item_Title = item_Title;
        this.item_Difficulty = item_Difficulty;
        this.clickListener = clickListener;
    }

    @Override
    public int getCount() {
        return item_Difficulty.length;
    }

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

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

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
    
    if(view == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.item,null);
    }

    ImageView item_image_assign = view.findViewById(R.id.item_image);
    TextView item_Title_assign = view.findViewById(R.id.item_name);
    TextView item_difficulty_assign = view.findViewById(R.id.item_difficulty_name);
    CardView cardView = view.findViewById(R.id.cardView);
    item_image_assign.setImageResource(item_image[position]);
    item_Title_assign.setText(item_Title[position]);
    item_difficulty_assign.setText(item_Difficulty[position]);

    if (Objects.equals(item_Difficulty[position], "Hard")) {
        item_difficulty_assign.setTextColor(Color.parseColor("#E91E63"));
    }else if(Objects.equals(item_Difficulty[position], "Medium")){
        item_difficulty_assign.setTextColor(Color.parseColor("#AA00FF"));
    }
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            clickListener.onItemClicked(position);
        }
    });
}

3 -现在从主活动调用:

public class MainActivity extends AppCompatActivity {

    int[] itemsImage = new int[] {
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo
    };

    String[] itemTitle = new String[]{
            "Sorting", "Searching",
            "Stack & Queue", "Tree",
            "Graph", "Dynamic",
            "Greedy", "String",
            "Hashing"
    };

    String[] itemDifficulty = new String[]{
            "Easy",
            "Easy",
            "Easy",
            "Medium",
            "Hard",
            "Hard",
            "Hard",
            "Hard",
            "Medium"
    };

    GridView gridView;
    Main_Adapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridView = findViewById(R.id.grid_view);

        adapter = new Main_Adapter(this, itemsImage, itemTitle, itemDifficulty, new ClickListener() {
            @Override
            public void onItemClicked(int position) {
                Toast.makeText(MainActivity.this, "Item Clicked : "+position, Toast.LENGTH_SHORT).show();
            }
        });
        gridView.setAdapter(adapter);
    }
}

相关问题