从ViewHolder类更新项目数据

问题描述 投票:0回答:1

我对ViewHolder很困惑。我刚开始使用适配器和支架将所有ArrayAdapter和ListView转换为RecyclerView。转换时,我陷入了用于设置点击监听器的方式。

基本上,在过去,我会得到位置,在pos位置检索项目,然后根据我的ArrayList项目中的数据执行项目的布局。

@SuppressLint("ClickableViewAccessibility")
@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.list_items, null, true);


    Data currentData = (Data) getItem(position);

    myTextView.setText(currentData.title);
    myTextView.setOnClickListener(v->actionThatDependsOnPositionAndOrData());


 }

所以在我的ClickListener中,我会有一些取决于位置的东西,以及某种更新ArrayList或类似东西的回调。

现在,似乎我无法再找回位置了。

@Override
public <T extends DownloadableData> void bindData(T data) {
    super.bindData(data);

    myTextView.setOnClickListener(v->actionThat????)
}

那我该如何指定位置呢?

因为现在,我在clickListener中的AsyncTask中找到的唯一方法是调用具有RecyclerView的Fragment并执行此操作

 public void myCallback(Data data, String newTitle){

     int index=allData.indexOf(data);
     performModificationOfData(data); //like data.title=newTitle;
     allData.set(index, data);
     adapter.notifyDataSetChanged(); //not even sure this is needed..
    }

是不是有更聪明的方法呢?

android android-recyclerview android-viewholder listview-adapter
1个回答
0
投票

请检查以下适配器,它将帮助您了解如何在适配器内的视图上设置单击侦听器。在onBindViewHolder方法内部setOnItemClick监听器。然后在该回调的活动集中,您将获得具有数据和位置的回调的覆盖方法。然后在项目点击时执行任何操作。

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.ImageView;  
import android.widget.RelativeLayout;  
import android.widget.TextView;  
import android.widget.Toast;  


public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder>{  
    private MyListData[] listdata;  
    private ItemclickCallback callback;

   // RecyclerView recyclerView;  
    public MyListAdapter(MyListData[] listdata) {  
        this.listdata = listdata;  
    }

    public void setCallback(ItemclickCallback callback) {
        this.callback = callback;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());  
        View listItem= layoutInflater.inflate(R.layout.list_item, parent, false);  
        ViewHolder viewHolder = new ViewHolder(listItem);  
        return viewHolder;  
    }  

    @Override  
    public void onBindViewHolder(ViewHolder holder, int position) {  
        final MyListData myListData = listdata[position];  
        holder.myTextView.setText(listdata[position].getDescription());  
        holder.imageView.setImageResource(listdata[position].getImgId());  
        holder.myTextView.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View view) {  
                Toast.makeText(view.getContext(),"click on item: "+myListData.getDescription(),Toast.LENGTH_LONG).show(); 
                onTextViewClick(myListData,position); 
            }  
        });  
    }  


    interface ItemclickCallback{
        void onTextViewClick(MyListData myListData,int position);
    }

    @Override  
    public int getItemCount() {  
        return listdata.length;  
    }  

    public static class ViewHolder extends RecyclerView.ViewHolder {  
        public ImageView imageView;  
        public TextView myTextView;  
        public ViewHolder(View itemView) {  
            super(itemView);  
            this.imageView = (ImageView) itemView.findViewById(R.id.imageView);  
            this.myTextView = (TextView) itemView.findViewById(R.id.myTextView);  
        }  
    }  
}  
© www.soinside.com 2019 - 2024. All rights reserved.