RecyclerView - 从我的 ViewHolder 获取对象

RecyclerView - Getting Objects from my ViewHolder

我正在创建一个接受 Match 对象列表的 RecyclerView。我已经为 listItem 设置了一个 onClickListener。我想在单击此列表项时将匹配设置为活动。当我单击它时,如何访问存储在特定 listItem 中的 Match 对象。

这是我的 Adapter/ViewHolder class RecyclerView:

package com.checkinsystems.ez_score.utils;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.checkinsystems.ez_score.EditMatchActivity;
import com.checkinsystems.ez_score.MainActivity;
import com.checkinsystems.ez_score.MatchActivity;
import com.checkinsystems.ez_score.R;
import com.checkinsystems.ez_score.model.Match;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;



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

    private List<Match> mMatches;
    private final Context mContext;

    public MatchItemAdapter(Context context, List<Match> items) {
        this.mContext = context;
        this.mMatches = items;
    }

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

        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = inflater.inflate(R.layout.list_item_match, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MatchItemAdapter.ViewHolder holder, int position) {
        Match match = mMatches.get(position);

        try {
            holder.tvName.setText(match.getMatchName());
            holder.tvDate.setText(match.getMatchDate());
            holder.imageView.findViewById(R.id.imageView);
            holder.hiddenMatchId.setText(match.getMatchId());
            holder.container.findViewById(R.id.list_item_container);

            Typeface bold = Typeface.create("san-serif", Typeface.BOLD);

            SharedPreferences sharedPreferences = mContext.getSharedPreferences(
                    MY_GLOBAL_PREFS, mContext.MODE_PRIVATE);
            String currentMatchName = sharedPreferences.getString(CURRENT_MATCH_NAME, "current_match_name");


            String itemName = holder.tvName.getText().toString();

            if (itemName.equals(currentMatchName)) {
                holder.tvName.setTypeface(bold);
                holder.container.setBackgroundColor(Color.parseColor("#a4a4a4"));
                holder.tvName.setTextSize(24);
                holder.matchIsActive = true;


                SharedPreferences.Editor editor = mContext.getSharedPreferences(MY_GLOBAL_PREFS, mContext.MODE_PRIVATE).edit();
                editor.putString(CURRENT_MATCH_ID, match.getMatchId());
                editor.putString(CURRENT_MATCH_NAME, match.getMatchName());
                editor.putString(CURRENT_MATCH_CLUB_ID, match.getClubId());
                editor.putString(CURRENT_MATCH_DATE, match.getMatchDate());
                editor.putString(CURRENT_MATCH_LEVEL, match.getMatchLevel());
                int spinnerPos;
                switch (match.getMatchLevel()){
                    case "I":
                        spinnerPos = 0;
                        break;
                    case "II":
                        spinnerPos = 1;
                        break;
                    case "III":
                        spinnerPos = 2;
                        break;
                    case "IV":
                        spinnerPos = 3;
                        break;
                    default:
                        spinnerPos = 0;
                }
                editor.putInt(POSITION_OF_SPINNER, spinnerPos);
                editor.apply();

            }


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

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


    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvName;
        public TextView tvDate;
        public ImageView imageView;
        public TextView hiddenMatchId;
        public View container;

        boolean matchIsActive = false;


        public ViewHolder(final View itemView) {
            super(itemView);

            tvName = (TextView) itemView.findViewById(R.id.match_name);
            tvDate = (TextView) itemView.findViewById(R.id.match_date);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            hiddenMatchId = (TextView) itemView.findViewById(R.id.match_id_hidden);
            container = (View) itemView.findViewById(R.id.list_item_container);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    final PopupMenu popup = new PopupMenu(view.getContext(), imageView);
                    if (!matchIsActive) {
                        popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
                        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                            @Override
                            public boolean onMenuItemClick(MenuItem item) {
                                switch (item.getItemId()) {
                                    case R.id.edit_match:

                                        /////////////////////////////////////////////////////////
                                        // Make this match the current one before sending the intent
                                        String matchId = hiddenMatchId.getText().toString();

                                        Intent intent = new Intent(itemView.getContext(), EditMatchActivity.class);
                                        intent.putExtra(EditMatchActivity.EXTRA_MATCH_ID, matchId);
                                        itemView.getContext().startActivity(intent);
                                        break;
                                    case R.id.delete_match:


                                        // to delete a match here
                                        // also remove from recyclerview

                                        break;
                                }
                                return false;
                            }
                        });
                    } else {

                        // If the current item is selected and you click on the side menu -> don't show delete
                        // delete isn't an option for active Matches
                        popup.getMenuInflater().inflate(R.menu.popup_menu_no_delete, popup.getMenu());
                        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                            @Override
                            public boolean onMenuItemClick(MenuItem item) {
                                switch (item.getItemId()) {
                                    case R.id.edit_match:
                                        // Make this match the current one before sending the intent


                                        Intent intent = new Intent(itemView.getContext(), EditMatchActivity.class);
                                        itemView.getContext().startActivity(intent);
                                        break;

                                }
                                return false;
                            }
                        });
                    }

                    //displaying the popup
                    popup.show();
                }
            });

            container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    matchIsActive = true;   // maybe this is redundant

                    String matchName = tvName.getText().toString();
                    Gson gson = new Gson();

                    SharedPreferences.Editor editor = itemView.getContext().getSharedPreferences(
                            MY_GLOBAL_PREFS, itemView.getContext().MODE_PRIVATE).edit();
                    //editor.putString(CURRENT_MATCH_NAME, matchName);
                    editor.apply();


                    Intent intent = new Intent(itemView.getContext(), MainActivity.class);
                    itemView.getContext().startActivity(intent);

                }
            });

        }

    }
}

您应该像这样在 onBindViewHolder() 方法中设置 onClickListener():

holder.imageView.setOnClickListener(...
Log.d("HEY, THIS IS CURRENT MATCH", mMatches.get(position));
...);

清楚了吗?

您可以使用标签来处理 ID 和索引 为了存储 imageView.setTag(position); 为了找回某处:

@Override
public void onClick(View v) {
        int pos = (int) v.getTag();
        Match matches = mMatches.get(pos);

}

正如帕维尔所说, 在你的 onBindViewHolder() 函数中,你应该为你的项目设置监听器。

holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { .... } });

holder.container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { ... } });

一种方法是您可以先给出列表项布局的 ID,例如您选择了一个 LinearLayout 然后您应该给出一个 ID,然后在视图持有者中初始化它 class喜欢。

private LinearLayout container = findViewBYiD(ID_YOU_HAVE CHOOSEN);

然后在 OnBindViewHolder 中访问它:

holder.container.setOnClickListener(CLICK_LISTENER);

OnClick 方法中,您可以通过以下方式访问对象:

Match matches = mMatches.getItem(position);