如何为适配器中的项设置监听器?

huangapple go评论87阅读模式
英文:

how to set listener for items in adapter?

问题

bankCardAdapter:

Have a lyser on the adapter to change something when the method clicks.
I dont know what i shoud write on body of onClickListener.
And listener.onClick(); needs (View View,StructCard data,int posotion)
Thank you in advance And I'm sorry for my weakness in English

英文:

Have a lyser on the adapter to change something when the method clicks.
I dont know what i shoud write on body of onClickListener.
And listener.onClick(); needs (View View,StructCard data,int posotion)
Thank you in advance And I'm sorry for my weakness in English

bankCardAdapter:

package ir.acopal.android.test.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Random;

import ir.acopal.android.test.R;
import ir.acopal.android.test.classes.StructCard;
import ir.acopal.android.test.interfaces.setOnclickListenerBankCard;
import ir.acopal.android.test.view.CustomBankCard;

public class BankCardAdapter extends RecyclerView.Adapter<BankCardAdapter.ViewHolder> {


    private ArrayList<StructCard> accountCards;
    private Context context;
    setOnclickListenerBankCard listener;
    Boolean a = true;
    View view;


    public BankCardAdapter(ArrayList<StructCard> accountCards, Context context) {
        this.accountCards = accountCards;
        this.context = context;
    }

    public void setListener(setOnclickListenerBankCard listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        StructCard cur = accountCards.get(position);
        if (a) {
            int firstCard = Color.parseColor("#BDBAAB");
            holder.card.setCardBackgroundColor(firstCard);
            a = false;
        } else {
            Random rnd = new Random();
            int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
            holder.card.setCardBackgroundColor(color);
        }
        holder.card.setCardNumber(cur.getAccountCardNumbers());
        holder.card.setCustomCardExpireDateText(cur.getAccountExpirationDate());
        holder.card.setNewCardText("");
//        holder.txtExpirationDate.setText(number.getAccountExpirationDate());


    }

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


    public class ViewHolder extends RecyclerView.ViewHolder {
        CustomBankCard card;


        public ViewHolder(@NonNull final View itemView) {
            super(itemView);
            findViews(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                }
            });
        }

        private void findViews(View itemView) {
            card = itemView.findViewById(R.id.itemBankCard_cbcCard);
        }
    }
}

Struct card:

package ir.acopal.android.test.classes;

public class StructCard {
    private String accountCardNumbers;
    private String accountExpirationDate;
    private boolean empty;


    public String getAccountCardNumbers() {
        return accountCardNumbers;
    }
    public void setAccountCardNumbers(String accountCardNumbers) {
        this.accountCardNumbers = accountCardNumbers;
    }



    public String getAccountExpirationDate() {
        return accountExpirationDate;
    }
    public void setAccountExpirationDate(String accountExpirationDate) {
        this.accountExpirationDate = accountExpirationDate;
    }


    public boolean isEmpty() {
        return empty;
    }
    public void setEmpty(boolean empty) {
        this.empty = empty;
    }
}

listener:

package ir.acopal.android.test.interfaces;

import android.view.View;

import ir.acopal.android.test.classes.StructCard;

public interface setOnclickListenerBankCard {
    void onClick(View view, StructCard data, int posotion);


}

答案1

得分: 0

In your onCreateViewHolder 方法中添加以下代码。你可以通过适配器的位置获取 structCard 的值。

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);
    ViewHolder holder = new ViewHolder(view);
    
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "position = " + holder.getAdapterPosition());
            StructCard structCard = accountCards[holder.getAdapterPosition()];
        }
    });

    return holder;
}
英文:

on your onCreateViewHolder add the following code. you will get structCard value by using adapter position

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

 View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);
 holder.itemView.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         Log.d(TAG, "position = " + holder.getAdapterPosition());
         StructCard structCard = accountCards[holder.getAdapterPosition()]
     }
 });
return new ViewHolder(view);
}

答案2

得分: 0

请将您的onBindViewHolder更改如下:

 @Override
 public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    StructCard cur = accountCards.get(position);
    if (a) {
        int firstCard = Color.parseColor("#BDBAAB");
        holder.card.setCardBackgroundColor(firstCard);
        a = false;
    } else {
        Random rnd = new Random();
        int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
        holder.card.setCardBackgroundColor(color);
    }
    holder.card.setCardNumber(cur.getAccountCardNumbers());
    holder.card.setCustomCardExpireDateText(cur.getAccountExpirationDate());
    holder.card.setNewCardText("");

    /*在此处添加以下行以进行项目单击。单击项目后,调用listener.onClick并传递所需的参数。 */

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.onClick(holder.itemView, cur, position)
        }
    });
}
英文:

Change your onBindViewHolder like this.

 @Override
 public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    StructCard cur = accountCards.get(position);
    if (a) {
        int firstCard = Color.parseColor("#BDBAAB");
        holder.card.setCardBackgroundColor(firstCard);
        a = false;
    } else {
        Random rnd = new Random();
        int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
        holder.card.setCardBackgroundColor(color);
    }
    holder.card.setCardNumber(cur.getAccountCardNumbers());
    holder.card.setCustomCardExpireDateText(cur.getAccountExpirationDate());
    holder.card.setNewCardText("");

    /*Add these lines for item click. After Click on item 
    call listener.onClick and pass requires parameters. */

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.onClick(holder.itemView, cur, position)
        }
    });
}

</details>



huangapple
  • 本文由 发表于 2020年8月1日 18:11:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63204065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定