从Activity的startOnActivityResult更新RecycleView特定位置

时间:2016-11-24 10:26:20

标签: java android

我想更新RecylerView的特定位置的视图,但由于某些问题而未发生这种情况,如下所示 - 1)向上/向下滚动时会造成混乱 2)我通过holder.setIsRecyclable(false);来处理这个问题,但是当向上/向下滚动时,它再次没有显示以前保存的子状态。

我的代码如下 -

ADAPTER CLASS

public class EventNameAdapter extends RecyclerView.Adapter<EventNameAdapter.MyViewHolder> {

private List<EventNameModel> eventNameList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView eventNameText;
    public ImageView    imageViewRadio;

    public MyViewHolder(View view) {
        super(view);
        eventNameText = (TextView) view.findViewById(R.id.eventNameText);
        imageViewRadio = (ImageView) view.findViewById(R.id.imageViewRadio);
    }
}


public EventNameAdapter(List<EventNameModel> eventNameList) {
    this.eventNameList = eventNameList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.event_name, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    EventNameModel movie = eventNameList.get(position);
    holder.setIsRecyclable(false);
    holder.eventNameText.setText(movie.getEventName());
}

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

}

更新位置代码

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 101) {
        if(resultCode == RESULT_OK){
            int responseInt =   data.getIntExtra("SUBMIT_DATA",-2);
            Toast.makeText(EventListActivity.this,"Response- "+responseInt,Toast.LENGTH_SHORT).show();
            if(responseInt>=0){
                    EventNameAdapter.MyViewHolder viewHolder = (EventNameAdapter.MyViewHolder) nameRecyclerView.findViewHolderForAdapterPosition(responseInt);
                    if (viewHolder != null) {
                            Toast.makeText(EventListActivity.this,"Response 2 - "+responseInt,Toast.LENGTH_SHORT).show();
                            viewHolder.imageViewRadio.setImageResource(R.drawable.radio_button_on);
                            Log.d("hellotom", "if i=" + responseInt);

                    } else {
                        Log.d("hellotom", "else i=" + responseInt);
                    }
            }
        }
    }
}

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:3)

EventNameModel课程中,有一个字段来维护单选按钮的状态,如下所示

public class EventNameModel {
    // your other fields
    private boolean isRadioOn;

    public void setIsRadioOn(boolean value){
        this.isRadioOn = value;
    }

    public boolean isRadioOn(){
        return isRadioOn;
    }
}

然后调整onBindViewHolder方法,如下所示

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    EventNameModel movie = eventNameList.get(position);
    holder.eventNameText.setText(movie.getEventName());
    if(movie.isRadioOn()){
        holder.imageViewRadio.setImageResource(R.drawable.radio_button_on);
    } else {
        holder.imageViewRadio.setImageResource(R.drawable.radio_button_off);
    }
}

在适配器类中使用这样的方法来更新从其他活动中检索的位置

public void updateItemAt(int position){
    EventNameModel movie = eventNameList.get(position);
    movie.setIsRadioOn(true);
    notifyItemChanged(position);
}

现在您可以从onActivityResult调用此方法,如下所示

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 101) {
        if(resultCode == RESULT_OK){
            int responseInt =   data.getIntExtra("SUBMIT_DATA",-2);
            if(responseInt>=0){
                mAdapter.updateItemAt(responseInt);
            }
        }
    }
}