从RecyclerView.ViewHolder更新RecyclerView.Adapter的ArrayList?

时间:2017-08-06 13:14:10

标签: android android-recyclerview recycler-adapter android-viewholder

在我的MainActivity中,我有类似的东西:

recyclerAdapter = new RecyclerAdapter(this, arrayList);
recyclerView.setAdapter(recyclerAdapter);

要更新我的RecyclerView,我会:(在MainActivity中)

Item item = new Item();
item.setId(id);
item.setMessage(message);

arrayList.set(position, item);
recyclerAdapter.notifyItemChanged(position);

我想在每个RecyclerView列表项中都有一个按钮,可以启用/禁用当前条目。因此,我需要获得当前的切换状态。

我的问题是,如何在ViewHolder中更新MainActivity中的RecyclerView? RecyclerView.ViewHolder:

public class SetViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{//, View.OnLongClickListener{
public ImageView toggle;

public SetViewHolder(View itemView){
    super(itemView);
    toggle = (ImageView) itemView.findViewById(R.id.img_main_card_toggle);
    toggle.setOnClickListener(this);
}


@Override
public void onClick(View v) {
    if (v.getId() == toggle.getId()) {
        int clicked_position = getAdapterPosition();
        //TODO update Item of RecyclerView of MainActivity
    }
}

我的Recycler.Adapter:

public class RulesAdapter extends RecyclerView.Adapter<SetViewHolder>{
private Activity activity;
List<Item> items = Collections.emptyList();
private OnTapListener onTapListener;
public RulesAdapter(Activity activity, List<Item> items){
    this.activity = activity;
    this.items = items;
}

@Override
public SetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rules_list_item,parent,false);
    return new SetViewHolder(view);
}

@Override
public void onBindViewHolder(SetViewHolder holder, final int position) {
    holder.recevied_message.setText(items.get(position).getReceived_message());
    holder.reply_message.setText(items.get(position).getReply_message());
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(onTapListener != null){
                onTapListener.OnTapView(position);
            }
        }
    });
}

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

public void setOnTapListener(OnTapListener onTapListener){
    this.onTapListener = onTapListener;
}

}

2 个答案:

答案 0 :(得分:2)

定义Interface

public interface OnRecyclerItemToggleClickListener {

    void onToggleClick(int adapterPosition);
}

adapter中实施此功能。并通过ViewHolder回调中的setter方法将适配器实例传递给onCreateViewHolder

public class SetViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{//, View.OnLongClickListener{
    public ImageView toggle;
    private OnRecyclerItemToggleClickListener listener;

    public SetViewHolder(View itemView){
        super(itemView);
        toggle = (ImageView) 
        itemView.findViewById(R.id.img_main_card_toggle);
        toggle.setOnClickListener(this);
    }


    public void setRecyclerItemToggleClickListener( OnRecyclerItemToggleClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == toggle.getId()) {
            int clicked_position = getAdapterPosition();


       //TODO update Item of RecyclerView of MainActivity

       listener.onToggleClick(clicked_position);
    }
}

在适配器中:

@Override
public SetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rules_list_item,parent,false);
    SetViewHolder holder = new SetViewHolder(view);
    holder.setOnRecyclerItemToggleClickListener(this);
    return holder; 
}

@Override public void onToggleClick(int adapterPosition) {
    // you will get the callback here when the toggle button is clicked.
    // you can update the ArrayList here.
    // Or similarly, call an interface method to Activity.
}

点击切换后,从onToggleClick调用此ViewHolder方法。您将收到adapter的回调,您可以在其中更新ArrayList。否则,您可以使用另一个类似的Interface来从适配器回拨Activity

答案 1 :(得分:1)

您只需要添加静态函数并使项目也静态:

public static ArrayList<Items> getAreayList()
{
    return items;
}

并在MainActivity中获得此功能:

onStart()