gpt4 book ai didi

java - 从 Activity 启动 OnActivityResult 更新 RecyclerView 特定位置

转载 作者:行者123 更新时间:2023-12-01 21:13:10 24 4
gpt4 key购买 nike

我想更新 RecylerView 特定位置的 View ,但由于以下一些问题而没有发生 -1)当向上/向下滚动时会造成困惑2)我通过 holder.setIsRecyclable(false); 处理这个问题,但是当再次向上/向下滚动时,它不会显示以前保存的子状态。

我的代码如下-

适配器类别

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);
}
}
}
}
}

请帮我解决这个问题。

最佳答案

在您的 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);
}
}

在您的适配器类中有一个类似的方法来更新从其他 Activity 检索到的位置

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);
}
}
}
}

关于java - 从 Activity 启动 OnActivityResult 更新 RecyclerView 特定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40783894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com