如何实现SetOnItemClickListener FirebaseRecyclerViewAdapter

时间:2015-12-05 20:30:13

标签: android firebase firebase-realtime-database firebaseui

如何在Firebase RecyclerView适配器中实现SetOnItemClickListener事件? 我使用文档示例,聊天应用程序:

 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);

2 个答案:

答案 0 :(得分:14)

可能有很多方法可以做到这一点,但这是我很快就尝试过的方法。

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

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

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

我们可以使用mView成员来访问可点击的视图。你应该mView添加一个getter,但我正在尝试最小化这里的更改。

然后在populateView中,我们可以使用具有额外position参数的重载,并使用OnClickListener进行连接:

    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)代码段获取所点击项目的数据库参考。

更新

第二个想法我可能更喜欢这个答案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)来获取数据库参考所选项目。

答案 1 :(得分:2)

这是我的解决方案。这对我有用

ViewHolder类

public class StudentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public TextView textViewName;
public TextView textViewRoll;
public TextView textViewAge;


public MyViewHolderClickListener mListener;

private String TAG= getClass().getSimpleName();

public static interface MyViewHolderClickListener{

    public void onTextViewNameClick(View view, int position);
    public void onTextViewRollClick(View view, int position);
}

public StudentViewHolder(View itemView) {
    super(itemView);
    textViewName = (TextView) itemView.findViewById(R.id.textViewName);
    textViewRoll = (TextView) itemView.findViewById(R.id.textViewRoll);
    textViewAge = (TextView) itemView.findViewById(R.id.textViewAge);

    textViewName.setOnClickListener(this);
    textViewRoll.setOnClickListener(this);
}


public void bindToStudent(Student student){
    if (student == null) {
        return;
    }

    textViewName.setText("Name: " +  student.getName());
    textViewRoll.setText( "Roll: " + student.getRoll());
    textViewAge.setText( "Age: " +  student.getAge());
}

public void setCustomOnClickListener(MyViewHolderClickListener listener  ){
    this.mListener = listener;
}

@Override
public void onClick(View view) {

    Log.d(TAG, "onClick at " + getAdapterPosition());
    Log.d(TAG, "id: " + view.getId());

    if( mListener!= null ){
        switch (view.getId()) {
            case R.id.textViewName:
                mListener.onTextViewNameClick(view, getAdapterPosition());
                break;
            case R.id.textViewRoll:
                mListener.onTextViewRollClick(view, getAdapterPosition());
                break;

            default:
                break;
        }

    }

 }



}

适配器类

public class StudentListAdapter extends FirebaseRecyclerAdapter<Student, StudentViewHolder> {

Activity parentActivity;
StudentViewHolder.MyViewHolderClickListener myViewHolderClickListener;

private String TAG = getClass().getSimpleName();

public StudentListAdapter(Activity mActivity, Class<Student> modelClass, int modelLayout, Class<StudentViewHolder> viewHolderClass, Query ref) {
    super(modelClass, modelLayout, viewHolderClass, ref);
    parentActivity = mActivity;
}


@Override
public StudentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    StudentViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
    // adding my MyViewHolderClickListener here
    viewHolder.setCustomOnClickListener(myViewHolderClickListener);
    return viewHolder;
}

@Override
protected void populateViewHolder(StudentViewHolder viewHolder, final Student model, final int position) {

    Log.d(TAG, model.toString());
    Log.d(TAG, getRef(position).getKey());

    // bind view
    viewHolder.bindToStudent(model);
}


public void setMyViewHolderClickListener(StudentViewHolder.MyViewHolderClickListener listener){
    this.myViewHolderClickListener = listener;
}


}

在活动类

        mAdapter = new StudentListAdapter(this, Student.class, R.layout.list_item, StudentViewHolder.class, studentQuery);
    mAdapter.setMyViewHolderClickListener(new StudentViewHolder.MyViewHolderClickListener() {
        @Override
        public void onTextViewNameClick(View view, int position) {
            Log.d(TAG, "Name Click at " +position);
        }

        @Override
        public void onTextViewRollClick(View view, int position) {
            Log.d(TAG, "Roll Click at " +position);
        }
    });

    recyclerView.setAdapter(mAdapter);