Recyclerview itemclick键

时间:2019-01-02 10:27:52

标签: java android firebase firebase-realtime-database

我想在itemclick中获得recyclerview的密钥

firebaserecycler中是getRef(position).getKey();

没有getref方法,那我该如何实现?

当前代码

适配器

public class IdAdapter extends RecyclerView.Adapter<IdAdapter.ChatViewHolder> {

    private List<IdHelper> mUsers;
    private String mId;
    private static IdAdapter.ClickListener clickListener;
    DatabaseReference mDatabaseReference;

    public IdAdapter(List<IdHelper> mUsers) {
        this.mUsers = mUsers;
    }

public class ChatViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
    public TextView mName;
    public TextView mStatus;
    public CircularImageView mImage;

    public ChatViewHolder(View view) {
        super(view);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        mName = (TextView) view.findViewById(R.id.display_name);
        mStatus = (TextView) view.findViewById(R.id.display_status);
        mImage = (CircularImageView) view.findViewById(R.id.circleImageView);
        mId = FirebaseAuth.getInstance().getCurrentUser().getUid();
        mDatabaseReference = FirebaseDatabase.getInstance().getReference();

    }

    @Override
    public void onClick(View v) {
        clickListener.onItemClick(getAdapterPosition(), v);
    }

    @Override
    public boolean onLongClick(View v) {
        clickListener.onItemLongClick(getAdapterPosition(), v);
        return false;
    }
}

    public void setOnItemClickListener(IdAdapter.ClickListener clickListener) {
        IdAdapter.clickListener = clickListener;
    }

    public interface ClickListener {
        void onItemClick(int position, View v);

        void onItemLongClick(int position, View v);
    }


    @Override
    public IdAdapter.ChatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_activity_id, parent, false);
        return new IdAdapter.ChatViewHolder(V);
    }

    //BindViewHolder..................................................................................//
    @Override
    public void onBindViewHolder(IdAdapter.ChatViewHolder holder, int position) {
        final IdHelper mHelper = mUsers.get(position);

        holder.mName.setText(mHelper.getName());
        holder.mStatus.setText(mHelper.getStatus());
        Picasso.get().load(mHelper.getImage()).into(holder.mImage);

    }

    //ItemCount.......................................................................................//
    @Override
    public int getItemCount() {
        return mUsers.size();
    }
}

片段

mAdapter.setOnItemClickListener(new IdAdapter.ClickListener() {
    @Override
    public void onItemClick(int position, View v) {
        Intent intent = new Intent(getActivity(), Chat.class);
        intent.putExtra("Recievers_Id", userId);
        intent.putExtra("Recievers_Name", mName);
        startActivity(intent);
    }

    @Override
    public void onItemLongClick(int position, View v) {
        Log.d("onItemLongClick", userId);
    }
});

无论哪个项目,我都单击显示相同用户名的项目。我意识到了这个问题,因为我根本没有使用位置,因此它只返回一个id。如何根据点击获取ID?

1 个答案:

答案 0 :(得分:0)

您做得正确,只是未使用您通过视图侦听器从视图持有者传递到片段的位置。

mAdapter.setOnItemClickListener(new IdAdapter.ClickListener() {
                @Override
                public void onItemClick(int position, View v) {
                    Intent intent = new Intent(getActivity(), Chat.class);
                    userId = mUsers.get(position).getUserId(); 
                    intent.putExtra("Recievers_Id", userId);
                    intent.putExtra("Recievers_Name", mName);
                    startActivity(intent);
                }

                @Override
                public void onItemLongClick(int position, View v) {
            Log.d("onItemLongClick", userId);
                }
            });

只需从usersList中的特定位置获取用户。