如何在回收者视图中为卡视图和内部子项提供单击侦听器?

时间:2019-06-01 09:20:59

标签: android android-recyclerview onclicklistener

我有一个带有单击侦听器的回收站视图,然后单击侦听卡视图位置,这可以正常工作。

现在,我想为内部项目(即卡视图内的按钮)生成点击侦听器。所以我很困惑,是否单击该按钮是在听父卡片视图还是内部卡片视图按钮。

现在,点击侦听器可在项目位置(即卡片视图)上工作,但是在卡片视图内如何寻找按钮点击。

这是我的代码

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {


    private Context context;
    private List<ApiObject> apiObjectList;


    public RecyclerViewAdapter(Context context, List<ApiObject> apiObjects) {

        this.context = context;
        this.apiObjectList =  apiObjects;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.gameitem, parent, false);
        return new MyViewHolder(view , context , apiObjectList);
    }

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

        ApiObject apiObject = apiObjectList.get(position);
        holder.title.setText(apiObject.getGameid());
        holder.description.setText(apiObject.getGamesName());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            holder.GameIconImage.setElevation(10);
            holder.GameCardView.setElevation(5);
        }

        Picasso.get()
                .load(apiObject.getGameIcon())
                .noFade()
                .into(holder.GameIconImage);

    }

    @Override
    public int getItemCount() {
        return apiObjectList == null ? 0 : apiObjectList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener  {

        ImageView GameIconImage  ;
        public TextView title;
        CardView GameCardView;
        public TextView description;
        Context context;
        List<ApiObject> apiObjectList;

        public MyViewHolder(@NonNull View itemView , Context context,  List<ApiObject> apiObjectList) {
            super(itemView);

            title = (TextView) itemView.findViewById(R.id.gameid);
            description = (TextView) itemView.findViewById(R.id.gamename);
            GameIconImage = itemView.findViewById(R.id.imageView);
            GameCardView = itemView.findViewById(R.id.cardview);
            itemView.setOnClickListener(this);
            this.context =context;
            this.apiObjectList = apiObjectList;    }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            ApiObject apiObject = this.apiObjectList.get(position);
            Intent intent = new Intent(this.context,GameListCheatsActivity.class);
            intent.putExtra("GameID",apiObject.getGameid());
            this.context.startActivity(intent);
        }
    }

}

现在如何添加按钮点击侦听器。

0 个答案:

没有答案