gpt4 book ai didi

java - 如何从 recyclerview 适配器中消除对话框 fragment

转载 作者:行者123 更新时间:2023-12-02 05:12:45 25 4
gpt4 key购买 nike

这是一个愚蠢的问题,但我是 Android 新手,我花了几个小时环顾四周,但找不到答案。

我有一个对话框 fragment ,其中有一个回收器 View 。我希望当用户单击 recyclerview 项目时关闭对话框。如何从 recyclerview 适配器中的监听器调用 Dismiss() ?

我已经在对话框 fragment 中尝试了 recyclerview 的监听器,但它什么也没做。请帮忙。

这是对话框 fragment 。在底部 onClick 将其关闭:

public class EvMySchedDlg extends DialogFragment implements View.OnClickListener {
View view;
String eventId;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.event_my_sched, container, false);

ImageView closeButton = view.findViewById(R.id.closeButton3);
closeButton.setOnClickListener(this);
final TextView eventNameBox = view.findViewById(R.id.eventNameBox);
eventNameBox.setOnClickListener(this);
TextView coNameBox = view.findViewById(R.id.coNameBox);

Bundle extraData = getArguments();
eventId = extraData.getString("eventId");
String coName = extraData.getString("coName");
String eventName = extraData.getString("eventName");

coNameBox.setText(coName);
eventNameBox.setText(eventName);
//get list of days from server
String userId = ((DrawerActivity)getContext()).getUserData("userId");
final String[] cred = new String[]{"user_id", userId, "event_id", eventId};
VolleyCalls.postRequest(this.getContext(), "getEventDays", cred, new VolleyCalls.ServerReply() {
@Override
public void onSuccess(String theReply){
final List<EvMySchedData> dayList = new ArrayList<>();
EvMySchedAdapter tAdapter = new EvMySchedAdapter(dayList);

RecyclerView recyclerView = view.findViewById(R.id.schedRecycler);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(recyclerView.getContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(tAdapter);
tAdapter.emptyRecycler();

try {
JSONObject jsonServerReply = new JSONObject(theReply);
int evCount = jsonServerReply.getInt("recCount");

EvMySchedData thisLine;

if (evCount > 0) {
JSONObject cRec =null;
for (int thisRec=0; thisRec<evCount; thisRec++ ) {
cRec = jsonServerReply.getJSONObject(String.valueOf(thisRec));
thisLine = new EvMySchedData(cRec.getString("day_date"), cRec.getString("day_times"), cRec.getString("role"), cRec.getString("room"), view.getContext());
dayList.add(thisLine);
}
}

} catch (Exception e) {

}
}
public void onError(String error) {

}
});

return view;
} // end onCreateView

public void onClick(View v) {
this.dismiss();
}

}

recyclerview 的每行都有一只眼睛的图片。我想加载一个新 fragment 并在单击眼睛时关闭对话框 fragment 。这是适配器代码:

public class EvMySchedAdapter extends RecyclerView.Adapter<EvMySchedAdapter.EventViewHolder> {

private List<EvMySchedData> dayList;

public EvMySchedAdapter(List<EvMySchedData> dayList) {
this.dayList = dayList;
}

@Override
public EvMySchedAdapter.EventViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
final View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_my_sched_row, parent, false);
ImageView eye = itemView.findViewById(R.id.dayOverviewButton);

try {
eye.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//Load a new fragment
//dismiss the dialog
}
});

} catch (Exception e) {

}

return new EvMySchedAdapter.EventViewHolder(itemView);
}

@Override
public void onBindViewHolder(EvMySchedAdapter.EventViewHolder holder, int position) {
holder.dayDateBox.setText(dayList.get(position).getDayDate());
holder.dayTimeBox.setText(dayList.get(position).getDayTime());
holder.dayRoleBox.setText(dayList.get(position).getDayRole());
holder.dayRoomBox.setText(dayList.get(position).getDayRoom());

} // end onBindViewHolder

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

public class EventViewHolder extends RecyclerView.ViewHolder {
public TextView dayDateBox;
public TextView dayTimeBox;
public TextView dayRoleBox;
public TextView dayRoomBox;
public Context context;

public EventViewHolder(View view) {
super(view);
dayDateBox = view.findViewById(R.id.dateBox);
dayTimeBox = view.findViewById(R.id.timeBox);
dayRoleBox = view.findViewById(R.id.roleBox);
dayRoomBox = view.findViewById(R.id.roomBox);
}
}

public void emptyRecycler() {
final int size = dayList.size();
dayList.clear();
notifyItemRangeRemoved(0, size);
}

}

谢谢

最佳答案

  1. 向适配器添加一个接收 DialogFragment 并将其保存为字段的构造函数。
  2. 创建适配器时,执行 new EvMySchedAdapter(dayList, EvMySchedDlg.this);

  3. 在 onClick 内部调用 dialgFragment.dismiss();

关于java - 如何从 recyclerview 适配器中消除对话框 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56316131/

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