FirebaseRecyclerAdapter上的setOnItemClickListener

时间:2018-06-23 14:20:32

标签: java android firebase google-cloud-firestore

我的项目使用Firebase Firestore UI来在一个片段中使用RecyclerView + CardView组合显示集合,我使用了FirestoreUI本身提供的适配器。

FirestoreRecyclerAdapter

但是,我无法在显示的每张卡上设置onItemClickListener

FirestoreRecyclerAdapter adapter = new FirestoreRecyclerAdapter<Record, recordViewHolder>(options) {
        @Override
        public void onDataChanged() {
            progressBar.setVisibility(View.GONE);

        }

        @Override
        public void onError(@NonNull FirebaseFirestoreException e) {
            Log.e(TAG, "onError: ", e);
        }

        @Override
        protected void onBindViewHolder(@NonNull recordViewHolder holder, int position, @NonNull Record record) {

            holder.date.setText(String.valueOf(record.getFullDate()));
            holder.qty.setText(String.valueOf(record.getQty()));
            holder.rate.setText(String.valueOf(record.getRate()));
            holder.total.setText(String.valueOf(record.getTotal()));
            holder.milkType.setText(String.valueOf(record.getMilkType()));

        }

        @NonNull
        @Override
        public recordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            View view = inflater.inflate(R.layout.record_logs_card, parent, false);
            return new recordViewHolder(view, mListener);
        }


    };

像这样设置onItemClickListener会导致语法错误。

FirestoreRecyclerAdapter adapter = new FirestoreRecyclerAdapter<Record, recordViewHolder>(options, setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });) {
        @Override
        public void onDataChanged() {
            progressBar.setVisibility(View.GONE);

        }

        @Override
        public void onError(@NonNull FirebaseFirestoreException e) {
            Log.e(TAG, "onError: ", e);
        }

        @Override
        protected void onBindViewHolder(@NonNull recordViewHolder holder, int position, @NonNull Record record) {

            holder.date.setText(String.valueOf(record.getFullDate()));
            holder.qty.setText(String.valueOf(record.getQty()));
            holder.rate.setText(String.valueOf(record.getRate()));
            holder.total.setText(String.valueOf(record.getTotal()));
            holder.milkType.setText(String.valueOf(record.getMilkType()));

        }

        @NonNull
        @Override
        public recordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            View view = inflater.inflate(R.layout.record_logs_card, parent, false);
            return new recordViewHolder(view, mListener);
        }


    };

当适配器本身是变量而不是onItemClickListener的扩展类时,我该如何设置RecyclerAdapter并传递位置以提取准确的卡?

谢谢

3 个答案:

答案 0 :(得分:1)

创建一个界面来处理RecyclerViews的点击

首先要创建一个名为FirebaseViewHolder

的类
public class FirebaseViewHolder extends RecyclerView.ViewHolder {

    public ImageView mImageView;
    public TextView mPostTitle;
    public TextView mPostDesc;

    public FirebaseViewHolder(View itemView) {
        super(itemView);

        mImageView = (ImageView) itemView.findViewById(R.id.image_recyclerview);
        mPostTitle = (TextView) itemView.findViewById(R.id.tv_title_recyclerview_item);
        mPostDesc = (TextView) itemView.findViewById(R.id.tv_desc_recyclerview_item);

        //listener set on ENTIRE ROW, you may set on individual components within a row.
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.onItemClick(v, getAdapterPosition());

            }
        });
        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mClickListener.onItemLongClick(v, getAdapterPosition());
                return true;
            }
        });

    }
    private FirebaseViewHolder.ClickListener mClickListener;

    //Interface to send callbacks...
    public interface ClickListener{
        public void onItemClick(View view, int position);
        public void onItemLongClick(View view, int position);
    }

    public void setOnClickListener(FirebaseViewHolder.ClickListener clickListener){
        mClickListener = clickListener;
    }
}

,然后在您的onCreateViewHolder

 @NonNull
        @Override
        public recordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            View view = inflater.inflate(R.layout.record_logs_card, parent, false);

 viewHolder.setOnClickListener(new FirebaseViewHolder.ClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Toast.makeText(getActivity(), "Item clicked at " + position, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onItemLongClick(View view, int position) {
                        Toast.makeText(getActivity(), "Item long clicked at " + position, Toast.LENGTH_SHORT).show();
                    }
                });

            return new recordViewHolder(view, mListener);
        }

请注意,第一个类仅声明要单击的视图,因此,您应该使用自己的视图来代替我提到的变量。

答案 1 :(得分:0)

在下面的代码onBindViewHolder中尝试

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

                        DatabaseReference dbRef = firebaseRecyclerAdapter.getRef(position);
                        String device = dbRef.getKey();

                        Toast.makeText(getApplicationContext(), device, Toast.LENGTH_SHORT).show();

                    }
                });

但这是Firebase Database UI代码,我认为这将有助于您了解用法。

答案 2 :(得分:0)

我尝试了第二个答案“GastónSaillén”。但是我将viewHolder.setOnClickListener的第二个代码放在onBindViewHolder中,而不是onCreateViewHolder中,没有返回语句。效果很好