如何将RecyclerView的位置传递给Activity? [英] How to Pass Position of RecyclerView to Activity?

查看:393
本文介绍了如何将RecyclerView的位置传递给Activity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将recyclerview的位置传递给另一个我正在调用改造接口的活动.我可以传递Recyclerview项目的位置,以便从api加载不同的帖子

I am trying to pass position of recyclerview to another activity where I am calliing retrofit Interface. Can I pass position of Recyclerview item so I can load different post from api

 @Override
public void onBindViewHolder(TagAdapter.Tag_ViewHolder holder, final int position) {
    holder.tagName.setText(Tags.get(position));
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (position < 8){
                int  pos = (int) getItemId(position) + 1;
                Intent intent = new Intent (view.getContext(), Tag1Post.class);
                intent.putExtra("pos", pos);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);

            }
        }

我的端点是

@GET("/tag/{id}/")
Call<Tag> test(@Path("id") int id);

我想用RecyclerView位置替换id.

I want to replace id by RecyclerView position.

推荐答案

我没有传递Recycler View的位置,而是传递特定帖子的ID来导航以对该帖子发表评论.

Instead of passing the position of Recycler View I pass the id of the particular post to navigate to comment on that post.

1.在适配器的Bindview支架上为Intent Extra提供单击的帖子的ID

public void onBindViewHolder(PostViewHolder holder, final int position) {
        holder.postTitle.setText(posts.get(position).getTitle());
        holder.postBody.setText(posts.get(position).getBody());
        setAnimation(holder.itemView);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = posts.get(position).getId(); // get Id
                Intent intent = new Intent(context, CommentActivity.class);
                intent.putExtra("pos", id); // Pass Id
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
    }

2.检索传递的意图数据

int mPostId = getIntent().getIntExtra("pos", 0);

3.将检索到的数据传递到改造端点

Call<List<Comment>> call = apiService.getComments(mPostId);

翻新端点

@GET("/posts/{id}/comments/")
    Call<List<Comment>> getComments(@Path("id") int id);

我希望这会帮助某人遇到这类问题.

I hope this will help someone to encounter this type of problems.

快乐编码:)

这篇关于如何将RecyclerView的位置传递给Activity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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