我正在尝试将标签重定向到与该特定标签关联的帖子列表中,并且在下面给出的代码上查找和传递单击的回收器视图的位置时遇到问题
private final static String TAG_KEY = "1"; //How Do I Make it to take Position of RecyclerView so that post associate with that Tag can be opened
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<Tag> call = apiService.getTag(TAG_KEY);
call.enqueue(new Callback<Tag>() {
@Override
public void onResponse(Call<Tag> call, Response<Tag> response) {
int statusCode = response.code();
List<Post> tags = response.body().getTagPosts();
recyclerView.setAdapter(new PostListAdapter(tags, R.layout.list_item_post, getApplicationContext()));
}
@Override
public void onFailure(Call<Tag> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});
以及如何在适配器意图状态上调用接口
@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) {
//Toast.makeText(context, "Recycle Click" + position, Toast.LENGTH_SHORT).show();
if (position == 0){
Intent intent = new Intent (view.getContext(), TagToPostActivitiy.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
与标签关联的帖子列表 标签的 CList
您的问题不清楚,如果我没看错,我假设您希望根据当前单击的 recycleView 项目发送不同的请求。 所以:里面
public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
你正在夸大一些视图,所以只需将其设置为ClickListener:
view v = LayoutInflater.from...
v.setOnClickListener(
new MyOnClickListener() //pass here the event to click handler);
点击侦听器应以正确的位置处理 api 请求
public void onClick(View v) {
int itemPosition = recyclerView.indexOfChild(v);
//do your api request
}