我有3个文本的循环视图,有没有办法更新特定项目点击该位置,(我有每个项目的数量,我想提高该项目的数量点击该位置,0,1,2 ,3 ...) 这是我的receycleview:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
String [] Item_names,Price_items,Item_quantity;
public RecyclerAdapter(String [] Item_names,String [] Price_items,String [] Item_quantity)
{
this.Item_names=Item_names;
this.Price_items=Price_items;
this.Item_quantity=Item_quantity;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);
RecyclerViewHolder recyclerViewHolder=new RecyclerViewHolder(view);
return recyclerViewHolder;
}
@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
holder.ItemNameRecycler.setText(Item_names[position]);
holder.ItemPriceRecycler.setText(Price_items[position]);
holder.ItemQuantityRecycler.setText(Item_quantity[position]);
}
@Override
public int getItemCount() {
return Item_names.length;
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView ItemPriceRecycler,ItemNameRecycler,ItemQuantityRecycler;
public RecyclerViewHolder(View view)
{
super(view);
ItemNameRecycler=(TextView)view.findViewById(R.id.RowItemName);
ItemPriceRecycler=(TextView)view.findViewById(R.id.RowItemPrice);
ItemQuantityRecycler=(TextView)view.findViewById(R.id.RowItemQuantity);
ItemQuantityRecycler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
}
答案 0 :(得分:0)
您可以使用getAdapterPosition
来自文档:
你的代码中的返回由此表示的项的适配器位置 ViewHolder。
:
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView ItemPriceRecycler,ItemNameRecycler,ItemQuantityRecycler;
public RecyclerViewHolder(View view)
{
super(view);
ItemNameRecycler=(TextView)view.findViewById(R.id.RowItemName);
ItemPriceRecycler=(TextView)view.findViewById(R.id.RowItemPrice);
ItemQuantityRecycler=(TextView)view.findViewById(R.id.RowItemQuantity);
ItemQuantityRecycler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get position
int pos = getAdapterPosition(); //get position here
}
});
}
修改强>
用于更新recylerview
Textview
项onClick()
public static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView ItemPriceRecycler, ItemNameRecycler, ItemQuantityRecycler;
public RecyclerViewHolder(final View itemView) {
super(itemView);
ItemNameRecycler = (TextView) itemView.findViewById(R.id.RowItemName);
ItemPriceRecycler = (TextView) itemView.findViewById(R.id.RowItemPrice);
ItemQuantityRecycler = (TextView) itemView.findViewById(R.id.RowItemQuantity);
ItemQuantityRecycler.setOnClickListener(this);
}
// onClick Listener for view
@Override
public void onClick(View v) {
if (v.getId() == ItemQuantityRecycler.getId()) {
int pos = getAdapterPosition();
int value = pos + 1;
Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
ItemQuantityRecycler.setText(String.valueOf(value));
} else {
}
}
}