如何实现 SetOnItemClickListener FirebaseRecyclerViewAdapter [英] how to implement a SetOnItemClickListener FirebaseRecyclerViewAdapter

查看:18
本文介绍了如何实现 SetOnItemClickListener FirebaseRecyclerViewAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Firebase RecyclerView Adapter 中实现 SetOnItemClickListener 事件?我使用文档、聊天应用程序的示例:

how to implement a SetOnItemClickListener event in a Firebase RecyclerView Adapter? I use the example of documentation, chat app:

 private FirebaseRecyclerViewAdapter mAdapter;

RecyclerView recycler = (RecyclerView) findViewById(R.id.messages_recycler);
    recycler.setHasFixedSize(true);
    recycler.setLayoutManager(new LinearLayoutManager(this));

    mAdapter = new FirebaseRecyclerViewAdapter<ChatMessage, ChatMessageViewHolder>(ChatMessage.class, android.R.layout.two_line_list_item, ChatMessageViewHolder.class, mRef) {
        @Override
        public void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder, ChatMessage chatMessage) {
            chatMessageViewHolder.nameText.setText(chatMessage.getName());
            chatMessageViewHolder.messageText.setText(chatMessage.getMessage());
        }
    };
    recycler.setAdapter(mAdapter);

推荐答案

可能有很多方法可以做到这一点,但这是我刚刚尝试过的一种.

There are probably many ways to do this, but this is the one I just quickly tried.

ChatMessageViewHolder 类有一个成员字段,用于保存每条聊天消息的根视图:

The ChatMessageViewHolder class has a member field where it keeps the root view for each chat message:

public static class ChatHolder extends RecyclerView.ViewHolder {
    View mView;

    public ChatHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

我们可以使用 mView 成员来访问可点击的视图.您应该mView 添加一个 getter,但我正在尝试尽量减少此处的更改.

We can use that mView member to get access to the clickable view. You should add a getter for mView, but I'm trying to minimize the changes here.

那么在 populateView 中,我们可以使用具有附加 position 参数的重载,并将其与 OnClickListener 连接起来,如下所示:

So then in populateView we can use the overload that has an additional position parameter and wire things up with an OnClickListener like this:

    mRecycleViewAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(Chat.class, R.layout.message, ChatHolder.class, mChatRef) {
        @Override
        public void populateViewHolder(ChatHolder chatView, Chat chat, final int position) {
            chatView.setName(chat.getName());
            chatView.setText(chat.getText());

            if (mAuthData != null && chat.getUid().equals(mAuthData.getUid())) {
                chatView.setIsSender(true);
            } else {
                chatView.setIsSender(false);
            }

            chatView.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.w(TAG, "You clicked on "+position);
                    mRecycleViewAdapter.getRef(position).removeValue();                        
                }
            });
        }
    };

这个问题的答案提供了很多灵感:RecyclerView onClick.只要能确定用户点击的位置,就可以使用mRecycleViewAdapter.getRef(position)代码段来获取被点击的item的数据库引用.

The answers to this question offer tons of inspiration: RecyclerView onClick. As long as you can determine the position that the user clicked on, you can use the mRecycleViewAdapter.getRef(position) snippet to get the database reference of the item that was clicked.

我想我可能更喜欢这个答案https://stackoverflow.com/a/26196831/209103:

On second thought I might like this answer better https://stackoverflow.com/a/26196831/209103:

    mMessages.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Log.w(TAG, "You clicked on "+position);
            mRecycleViewAdapter.getRef(position).removeValue();
        }
    }));

它使用我链接的答案中的 RecyclerItemClickListener 帮助程序类.正如已经说过的:只要您有一个点击/触摸侦听器来告诉您所选项目的位置,它的其余 FirebaseUI 方面将是相同的:调用 adapter.getRef(position)获取所选项目的数据库引用.

It uses the RecyclerItemClickListener helper class from the answer I linked. And as already said: as long as you have a click/touch listener that tells you the position of the item that was selected, the rest FirebaseUI aspect of it will be the same: call adapter.getRef(position) to get a database reference for the selected item.

这篇关于如何实现 SetOnItemClickListener FirebaseRecyclerViewAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆