我如何获得一个RecyclerView选择的位置?如何获得、位置、RecyclerView

2023-09-06 13:55:41 作者:那抹天真dē微笑早已不存在

我尝试用支持库的recyclerview和卡片。我有卡recyclerview。每张卡有在右上角的X图标,将其删除:

I am experimenting with the support library's recyclerview and cards. I have a recyclerview of cards. Each card has an 'x' icon at the top right corner to remove it:

该卡的XML,list_item.xml

The card xml, list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/taskDesc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:textSize="40sp"
        android:text="hi"/>
    <ImageView
        android:id="@+id/xImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_remove"/>
</RelativeLayout>
</android.support.v7.widget.CardView>

//我试图用我会用的位置标记的行notifyItemRemoved(位置),在TaskAdapter.java:

//I attempted to tag the row with the position I would use in "notifyItemRemoved(position)" in TaskAdapter.java:

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskViewHolder>  {

private List<Task> taskList;
private TaskAdapter thisAdapter = this;


// cache of views to reduce number of findViewById calls
public static class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    protected TextView taskTV;
    protected ImageView closeBtn;
    public TaskViewHolder(View v) {
        super(v);
        taskTV = (TextView)v.findViewById(R.id.taskDesc);
    }

    @Override
    public void onClick(View v) {
        int position = v.getTag();
        adapter.notifyItemRemoved(position);
    }
}


public TaskAdapter(List<Task> tasks) {
    if(tasks == null)
        throw new IllegalArgumentException("tasks cannot be null");
    taskList = tasks;
}


// onBindViewHolder binds a model to a viewholder
@Override
public void onBindViewHolder(TaskViewHolder taskViewHolder, int pos) {
    final int position = pos;
    Task currTask = taskList.get(pos);
    taskViewHolder.taskTV.setText(currTask.getDescription());

    taskViewHolder.closeBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            thisAdapter.notifyItemRemoved(position);
        }
    });
}

@Override
public int getItemCount() {
    return taskList.size();
}


// inflates row to create a viewHolder
@Override
public TaskViewHolder onCreateViewHolder(ViewGroup parent, int pos) {
    View itemView = LayoutInflater.from(parent.getContext()).
                                   inflate(R.layout.list_item, parent, false);

    return new TaskViewHolder(itemView);
}
}

//这是行不通的,因为你不能设置一个标签,也可以从我的onClick访问适配器

//This won't work because you can't set a tag nor can I access the adapter from onClick

推荐答案

设置 onBindViewHolder 您的onclick听众,并从那里你可以访问的位置。如果您在ViewHolder设置它们,你不会知道在什么位置被点击,除非你传递的位置进入viewholder

set your onclick listeners on onBindViewHolder and you can access the position from there. If you set them in your ViewHolder you wont know what position was clicked unless you also pass the position into the viewholder

修改

由于 pskink 指出ViewHolder有一个为getPosition 让你origionally这样做是正确的方式。

As pskink pointed out ViewHolder has a getPosition so the way you were origionally doing it was correct.

在视图中单击,您可以在您的视图持有人使用为getPosition 键,返回的位置

when the view is clicked you can use getPosition in your view holder and it returns the position

更新

为getPosition()现在去precated和替换getAdapterPosition()

getPosition() is now deprecated and replaced with getAdapterPosition()