更改RecyclerView中已点击项目的值

时间:2015-01-23 13:00:17

标签: android android-recyclerview

我创建了一个RecyclerView,它可以从xml文件中提升三个文本视图和一个图像视图。我想知道如何更改(更新)已单击的视图的值?

到目前为止,我已经设置了OnItemTouchListener,它可以正常工作,我可以使用它来检测从中查看的视图。我不知道从哪里开始,如何在不点击整个RecyclerView的情况下更改点击视图中的数据?我使用this教程开始使用RecyclerView。

我试过搜索stackoverflow并给谷歌一个镜头,但没有发现任何相关内容。

编辑:

我正在使用ItemData:

对RecyclerView进行充气
ItemData itemsData[] = {
            new ItemData(getString(R.string.upgrade1_title),R.drawable.upgrade1image,getString(R.string.upgrade1_description),getString(R.string.upgrade1_price)),
            new ItemData(getString(R.string.upgrade2_title),R.drawable.upgrade2image,getString(R.string.upgrade2_description),getString(R.string.upgrade2_price)),
            new ItemData(getString(R.string.upgrade3_title),R.drawable.upgrade3image,getString(R.string.upgrade3_description),getString(R.string.upgrade3_price)),

ItemData类setter&吸气剂

public class ItemData {

private String title;
private String description;
private String price;
private int imageUrl;

public ItemData(String title,int imageUrl, String desciption, String price){

    this.title = title;
    this.description = desciption;
    this.price = price;
    this.imageUrl = imageUrl;
}

public CharSequence getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}
// The rest were removed to save space in the question

最后是适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ItemData[] itemsData;

public MyAdapter(ItemData[] itemsData) {
    this.itemsData = itemsData;
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View itemLayoutView = LayoutInflater.from(parent.getContext())
                           .inflate(R.layout.custom_row, null);

    // create ViewHolder

    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

    // - get data from your itemsData at this position
    // - replace the contents of the view with that itemsData

    viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
    viewHolder.txtViewDescription.setText(itemsData[position].getDesciption());
    viewHolder.txtViewPrice.setText(itemsData[position].getPrice());
    viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());

}

// inner class to hold a reference to each item of RecyclerView 
public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView txtViewTitle, txtViewDescription, txtViewPrice;
    public ImageView imgViewIcon;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.textTitle);
        txtViewDescription = (TextView) itemLayoutView.findViewById(R.id.textDescription);
        txtViewPrice = (TextView) itemLayoutView.findViewById(R.id.textPrice);
        imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.imageIcon);
    }
}

public void updateListValues (int position, int newVal) {

}
// Return the size of your itemsData (invoked by the layout manager)
@Override
public int getItemCount() {
    return itemsData.length;
}
}

3 个答案:

答案 0 :(得分:2)

单击更改适配器中的相应项目并调用notifyItemChanged(position)。然后,适配器将通知RecyclerView更改,它将再次呈现该项目,但只有该项目才有效。

答案 1 :(得分:1)

要么在onclick中执行某些操作,要么使用界面来获取数据并执行操作。之后,您需要致电notifyDataSetChanged()

static class ViewHolder extends RecyclerView.ViewHolder implements OnClickListener {

    public ViewHolder(View view) {
        // your code
        view.setOnClickListener(this)
    }

    @Override
    public void onClick(View v) {
        int position = getPosition();
        // your changes
        notifyDataSetChanged();
    }
}

答案 2 :(得分:0)

我建议你在ItemData类中添加一个布尔字段,称之为选中它。然后在bindViewHolder上,检查是否选择了该位置的ItemData,是否更改了视图的属性以指示选择,否则将其重置为默认属性。您可以在onItemTouched侦听器上切换选定的值,并且不要忘记最好使用位置调用notifyDataSetChanged()。

这样的事情:

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData

viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
viewHolder.txtViewDescription.setText(itemsData[position].getDesciption());
viewHolder.txtViewPrice.setText(itemsData[position].getPrice());
viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());

if(itemsData[position].isSelected()) {
    //Do something about it.
} else {
    //Else return to default state, this is important because views will be recycled.
}
}