2

私は、retrofit Interfaceを呼び出す別のアクティビティにrecyclerviewの位置を渡そうとしています。私は自分のエンドポイントは、私がRecyclerView位置でIDを交換したいRecyclerViewの位置をアクティビティに渡す方法は?

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

ある

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

      } 
     } 

APIとは異なるポストをロードすることができますので、私はRecyclerviewアイテムの位置を渡すことができます。

+0

なぜ、 'position'ではなく' int pos =(int)getItemId(position)+ 1'ですか? – xiaoyuan

+0

RecyclerView位置は0から始まり、API IDは1から始まります –

+0

ああ、あります。あなたのコードに問題はありますか? – xiaoyuan

答えて

0

代わりのリサイクラービューの位置を通過中AdapterView.OnItemClickListenerを実装特定の投稿のコメントに移動するために移動することができます。

1.渡された意図データ

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

3を取得するアダプター

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のBindviewホルダーをクリックしたポストのIDを持つ意向エキストラを提供します。私は、これが問題のこのタイプに遭遇するために誰かを助けることを願っています改造エンドポイントレトロフィットエンドポイント

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

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

に取得したデータを渡します。

ハッピーコーディング:)

2

インターフェイスを作成し、アダプタからアクティビティに位置を転送することができます。インタフェースを実装し、あなたの活動に

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); 
       //create your interface in Activity, then send it to adapter and call it here: 
       yourListener.onTransferPosition(position); 
      } 
     } 

public void onTransferPosition(int position) { 
    this.position= position; 
    } 
0

あなたがテントクラスを使用してビューの位置を通過しているので、あなたは、単に

int position = getIntent().getExtras().getInt("pos"); 

以下のように2つ目の活動での位置を取得することができますまたは、インターフェイスを使用して値を渡すことができます

public interface OnRecyclerClickListener { 
    void makeRestCall(int position); 
} 

インスタンス化

holder.itemView.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     if (position < 8){ 
      int pos = (int) getItemId(position) + 1; 
      startActivity(new Intent (view.getContext(), Tag1Post.class)); 
      onRecyclerClickListener.makeRestCall(pos);  
     } 
    } 

下回る最後に、これはあなたの中で2番目の活動に

public class SecondActivity extends AppCompatActivity implements OnRecyclerClickListener{ 

@Override 
public void makeRestCall(int pos) { 
    // do your GET request 
} 
0
private AdapterView.OnItemClickListener mOnClickListener; 

使用するインターフェイスを実装するように従って

onRecyclerClickListener = (OnRecyclerClick) this.mContext; 

としてインターフェイスが表示位置を送信あなたのコンストラクタからこれを尋ねてください。ホルダークラスのあなたのonclickの方法で

Adapter(AdapterView.OnItemClickListener onClickListener){ 
this.mOnClickListener=onClickListener; 
} 

次のように実行します。私はIDを渡す

@Override 
     public void onClick(View view) { 

      mOnClickListener.onItemClick(null, view, getAdapterPosition(), view.getId()); 
     } 

そして、あなたの活動

@Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       // here you will get the postion of the clicked item 
    } 
関連する問題