如何将 RecyclerView 的位置传递给 Activity?
How to Pass Position of RecyclerView to Activity?
我正在尝试将 recyclerview 的位置传递给另一个 activity 我正在调用改造界面的位置。我可以传递 Recyclerview 项目的位置以便我可以从 api
加载不同的 post
@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。
您可以创建一个接口,然后将位置从适配器传输到 activity。
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);
}
}
在实现接口的 activity 上:
public void onTransferPosition(int position) {
this.position= position;
}
由于您使用 Intent class 传递视图位置,您可以简单地在第二个 activity 中获得位置,如下所示
int position = getIntent().getExtras().getInt("pos");
或者您可以使用接口传递值
public interface OnRecyclerClickListener {
void makeRestCall(int position);
}
实例化接口如下
onRecyclerClickListener = (OnRecyclerClick) this.mContext;
像下面一样发送视图位置
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);
}
}
最后,在你的第二个 activity
中实现接口
public class SecondActivity extends AppCompatActivity implements OnRecyclerClickListener{
@Override
public void makeRestCall(int pos) {
// do your GET request
}
private AdapterView.OnItemClickListener mOnClickListener;
在你的适配器中使用它并从你的构造函数中询问它。
Adapter(AdapterView.OnItemClickListener onClickListener){
this.mOnClickListener=onClickListener;
}
在 holder class 中的 onclick 方法中这样做:
@Override
public void onClick(View view) {
mOnClickListener.onItemClick(null, view, getAdapterPosition(), view.getId());
}
并在你activity
中实施AdapterView.OnItemClickListener
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// here you will get the postion of the clicked item
}
我没有传递 Recycler View 的位置,而是传递了特定 post 的 ID 以导航到对该 post 的评论。
1.在 Adapter
的 Bindview 持有者上提供点击 ID post 的 Intent Extra
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);
我希望这能帮助遇到此类问题的人。
快乐编码:)
我正在尝试将 recyclerview 的位置传递给另一个 activity 我正在调用改造界面的位置。我可以传递 Recyclerview 项目的位置以便我可以从 api
加载不同的 post @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。
您可以创建一个接口,然后将位置从适配器传输到 activity。
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);
}
}
在实现接口的 activity 上:
public void onTransferPosition(int position) {
this.position= position;
}
由于您使用 Intent class 传递视图位置,您可以简单地在第二个 activity 中获得位置,如下所示
int position = getIntent().getExtras().getInt("pos");
或者您可以使用接口传递值
public interface OnRecyclerClickListener {
void makeRestCall(int position);
}
实例化接口如下
onRecyclerClickListener = (OnRecyclerClick) this.mContext;
像下面一样发送视图位置
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);
}
}
最后,在你的第二个 activity
中实现接口public class SecondActivity extends AppCompatActivity implements OnRecyclerClickListener{
@Override
public void makeRestCall(int pos) {
// do your GET request
}
private AdapterView.OnItemClickListener mOnClickListener;
在你的适配器中使用它并从你的构造函数中询问它。
Adapter(AdapterView.OnItemClickListener onClickListener){
this.mOnClickListener=onClickListener;
}
在 holder class 中的 onclick 方法中这样做:
@Override
public void onClick(View view) {
mOnClickListener.onItemClick(null, view, getAdapterPosition(), view.getId());
}
并在你activity
中实施AdapterView.OnItemClickListener
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// here you will get the postion of the clicked item
}
我没有传递 Recycler View 的位置,而是传递了特定 post 的 ID 以导航到对该 post 的评论。
1.在 Adapter
的 Bindview 持有者上提供点击 ID post 的 Intent Extrapublic 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);
我希望这能帮助遇到此类问题的人。
快乐编码:)