获取被点击项目在Recycleview上的位置

时间:2018-07-24 04:16:33

标签: android android-recyclerview contextmenu

我想创建一个回收视图。购物车包含很多产品,因此只要您长时间单击产品列表项,回收视图就会显示一个上下文菜单,然后选择“删除”以将其从产品列表中删除。但是在获取要删除的产品的位置时,我遇到了一些问题。 这是我的Recycle View HOLDER代码

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener,View.OnCreateContextMenuListener {

    TextView quantity;
    TextView name;
    TextView price;
    ImageView pic;
    ImageButton increase;
    ImageButton decrease;
    int quantityInt=1;
    LongClickListener longClickListener;

    public int getQuantityInt() {
        return quantityInt;
    }

    void changeQuantityDisplay(int number)
    {
        quantity.setText(Integer.toString(number));
    }



    public void setQuantityIntPlus(int quantityInt) {
        this.quantityInt = quantityInt+1;
    }

    public void setQuantityIntMinus(int quantityInt) {
        this.quantityInt = quantityInt-1;
    }

    public MyViewHolder(View itemView) {
        super(itemView);
        View view=LayoutInflater.from(context).inflate(R.layout.activity_cart,null,false);
        name=(TextView)itemView.findViewById(R.id.listproduct_name);
        price=(TextView)itemView.findViewById(R.id.listproduct_price);
        pic=(ImageView)itemView.findViewById(R.id.listproduct_img);

        increase=(ImageButton)itemView.findViewById(R.id.increasebtn);
        decrease=(ImageButton)itemView.findViewById(R.id.decreasebtn);
        quantity=(TextView) itemView.findViewById(R.id.listproduct_quantity);
        itemView.setOnLongClickListener(this);
        itemView.setOnCreateContextMenuListener(this);


    }
    public void setLongClickListener(LongClickListener lc)
    {
        this.longClickListener=lc;

    }

    @Override
    public boolean onLongClick(View v) {
        this.longClickListener.onItemLongClick(getLayoutPosition());
        return false;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Settings");
        menu.add(0,0,0,"Remove");
    }
}

这是LongClickListener界面

public interface LongClickListener {
void onItemLongClick(int pos);

}

在onBindViewHolder函数中

  @Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
    holder.setLongClickListener(new LongClickListener() {
        @Override
        public void onItemLongClick(int pos) {
            Toast.makeText(context, "On Long Clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

在CartActivity中

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=LayoutInflater.from(getContext()).inflate(R.layout.activity_cart,container,false);
    textView=(TextView)view.findViewById(R.id.totalcost);
    //init Arr
    initArr();

    //Init cart List
    cartList=(RecyclerView)view.findViewById(R.id.cartList);
    cartListAdapter=new CartRecycleViewAdapter(getContext(),R.layout.card_row_layout,incartArr,textView);

    LinearLayoutManager cartListLayoutManager=new LinearLayoutManager(getContext());
    cartListLayoutManager.setOrientation(LinearLayout.VERTICAL);
    cartList.setLayoutManager(cartListLayoutManager);
    cartList.setAdapter(cartListAdapter);

    cartList.setItemAnimator(new DefaultItemAnimator());

    return view;
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    cartListAdapter.getItemSelected(item);
    return super.onContextItemSelected(item);
}

2 个答案:

答案 0 :(得分:4)

为每个项目创建一个MyViewHolder实例,您可以使用:

getAdapterPosition()

在MyViewHolder里面获取它的项目位置。

如果您位于MyViewHolder之外,则可以致电:

holder.getAdapterPosition()

知道它的位置。

答案 1 :(得分:0)

雷蒙德(Raymond)是绝对正确的,为了传递单击项的适配器位置,您应该按documentation page中所述调用ViewHolder类的内置方法。

为了通过适当的封装传递OnClick行为,我通常要做的是这样的事情:

class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

    interface OnClickListener {
        void onClick(Item item);
    }

    private final List<Item> dataset;
    private final OnClickListener l; 

    public MyAdapter(List<Item> data, OnClickListener listener) {
        this.dataset = data;
        this.l = listener;
    }

    @Override MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = //inflate layout
        MyClick click = new MyClick() {
            onClick(int position) {
                if (l != null) {
                    l.onClick(dataset.get(position));
                }
            }
        };
        return new MyViewHolder(click, v);
    }

    static class MyViewHolder {
        final MyClick l;

        public MyViewHolder(MyClick listener, View view) {
            super(view);
            l = listener;
            // inflate views
            view.setOnClickListener(new OnClickListener() {
                if (l != null) {
                    l.onClick(getAdapterPosition());
                }
            });
        }

        interface MyClick {
            void onClick(int position);
        }
    }
}

class MyActivity extends AppCompatActivity {

    @Override void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set layout
        MyAdapter adapter = new MyAdapter(getData(), new OnClickListener() {
            @Override onClick(Item item) {
                // do stuff with item
            }
        });
    }
}