我在应用程序中具有Recycler视图,在导航抽屉中使用Recycler视图意味着片段 当reyclerview为空,并且当我在回收站视图中添加项目但未显示但刷新片段后,添加项目之后,则立即显示但第一次不显示原因。我被困住了。
这是我的代码:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.activity_consultant_note_fragment, container, false);
mSp = getActivity().getSharedPreferences(Constant.PREF, Context.MODE_PRIVATE);
consulatantnoterecyclerView = v.findViewById(R.id.consulatantnote_display_recycler);
swipeRefreshLayout = v.findViewById(R.id.swipeContainer);
nodatafound = v.findViewById(R.id.nodatafound);
// GetConsulatantNote();
RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity());
consulatantnoterecyclerView.setLayoutManager(manager);
consulatantnoterecyclerView.setItemAnimator(new DefaultItemAnimator());
return v;
}
void GetConsulatantNote() {
dialog = new ProgressDialog(getActivity());
dialog.show();
if (!Utils.isInternetAvailable(getActivity(), true))
return;
final ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
ConsultantNote request = new ConsultantNote();
request.setActionBy(Integer.valueOf(mSp.getString(Constant.ActionBy, "")));
request.setInPatientID(Integer.valueOf(mSp.getString(Constant.InPatientID, "")));
request.setToken(mSp.getString(Constant.TOKEN, ""));
request.setCreatedBy(Integer.valueOf(mSp.getString(Constant.MasterUserID, "")));
request.setIsActive(1);
ServiceRequest serviceRequest = new ServiceRequest();
serviceRequest.setOppID(**);
serviceRequest.setParameters(request);
Call<ConsultantNoteResponse> consultantNoteResponseCall = apiInterface.SaveConsulatantNote(serviceRequest);
consultantNoteResponseCall.enqueue(new Callback<ConsultantNoteResponse>() {
@Override
public void onResponse(Call<ConsultantNoteResponse> call, Response<ConsultantNoteResponse> response) {
if (response.body().getSuccess() == true && response.body().getValidated() == false) {
if (!response.body().getValidationErrors().isEmpty()) {
Nodatafound = true;
List<ValidationError> validationErrors = response.body().getValidationErrors();
for (int i = 0; i < validationErrors.size(); i++) {
ValidationError error = new ValidationError();
error.setErrorMessage(validationErrors.get(i).getErrorMessage());
Log.d("error", "" + validationErrors.get(i).getErrorMessage());
}
// Toast.makeText(DoctorCollection.this, "Records not found for opd", Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
}
if (response.body().getSuccess() == true && response.body().getValidated() == true) {
//data
consultantNotes = new ArrayList<>();
consultantNotes.clear();
List<ConsultantNote> notes = response.body().getConsultantNotes();
consulatantnoterecyclerView.setVisibility(View.VISIBLE);
nodatafound.setVisibility(View.GONE);
for (int i = 0; i < notes.size(); i++) {
ConsultantNote note = new ConsultantNote();
note.setCreatedByUserFullName(notes.get(i).getCreatedByUserFullName());
note.setConsultantNotesDateTime(notes.get(i).getConsultantNotesDateTime());
note.setPatientProgress(notes.get(i).getPatientProgress());
note.setCreatedBy(notes.get(i).getCreatedBy());
note.setInPatientConsultantNoteID(notes.get(i).getInPatientConsultantNoteID());
note.setProblemsAndInterventions(notes.get(i).getProblemsAndInterventions());
consultantNotes.add(note);
}
//consulatantnoterecyclerView.setAdapter(consultantNoteAdpater);
setAdapterData(consultantNotes);
onItemDelete();
if (dialog.isShowing()) {
dialog.dismiss();
}
}
if (Nodatafound) {
consulatantnoterecyclerView.setVisibility(View.GONE);
nodatafound.setVisibility(View.VISIBLE);
}
}
@Override
public void onFailure(Call<ConsultantNoteResponse> call, Throwable t) {
}
});
dialog.dismiss();
}
void onItemDelete() {
final ConsultantNoteAdpater noteAdpater = new ConsultantNoteAdpater(context, consultantNotes);
consulatantnoterecyclerView.setAdapter(noteAdpater);
noteAdpater.setOnItemDeleteClickListener(new ConsultantNoteAdpater.OnItemClickDeleteListener() {
@Override
public void onItemClick(final int position, final Integer NoteId) {
final AlertDialog.Builder innermsg = new AlertDialog.Builder(context);
innermsg.setMessage("Are you sure you want to delete this consultant note ?");
innermsg.setCancelable(false);
innermsg.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
consultantNotes.remove(position);
noteAdpater.notifyItemRemoved(position);
noteAdpater.notifyItemRangeChanged(position, consultantNotes.size());
DeleteConsulatantNote(NoteId);
noteAdpater.notifyDataSetChanged();
GetConsulatantNote();
}
});
innermsg.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
innermsg.show();
}
});
}
private void setAdapterData(List<ConsultantNote> data) {
if (data != null) {
ArrayList<ConsultantNote> notes = new ArrayList<>();
notes.clear();
notes.addAll(consultantNotes);
adapter = new ConsultantNoteAdpater(getActivity(), notes);
adapter.notifyItemRangeChanged(0, consultantNotes.size());
// adapter.notifyDataSetChanged();
consulatantnoterecyclerView.setAdapter(adapter);
}
}
这是我的recyclerview-适配器代码:
public class ConsultantNoteAdpater extends RecyclerView.Adapter<ConsultantNoteAdpater.holder> {
LayoutInflater layoutInflater;
Context context;
List<ConsultantNote> consultantNotes;
private SharedPreferences mSp;
public OnItemClickDeleteListener onItemClickDeleteListener;
public ConsultantNoteAdpater(Context c, List<ConsultantNote> notes) {
context = c;
consultantNotes = notes;
notifyDataSetChanged();
}
@NonNull
@Override
public holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.activity_consultant_note_adpater, parent, false);
mSp = context.getSharedPreferences(Constant.PREF, Context.MODE_PRIVATE);
return new holder(v);
}
public interface OnItemClickDeleteListener {
void onItemClick(int position, Integer NoteId);
}
public void setOnItemDeleteClickListener(OnItemClickDeleteListener onItemClickDeleteListener) {
this.onItemClickDeleteListener = onItemClickDeleteListener;
}
@Override
public void onBindViewHolder(@NonNull holder holder, final int position) {
final ConsultantNote note = consultantNotes.get(position);
holder.createdby.setText(Html.fromHtml(createdby + "" + note.getCreatedByUserFullName()));
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickDeleteListener != null) {
//get pateint consulatant note id
onItemClickDeleteListener.onItemClick(position, note.getInPatientConsultantNoteID());
}
}
});
}
@Override
public int getItemCount() {
return consultantNotes.size();
}
class holder extends RecyclerView.ViewHolder {
TextView date, createdby;
ImageView delete;
public holder(View itemView) {
super(itemView);
date = itemView.findViewById(R.id.consulatant_datetime);
createdby = itemView.findViewById(R.id.consulatant_createdby);
delete = itemView.findViewById(R.id.deleteconsultant);
}
}
}
我正在使用翻新从api获取数据,以便任何人都可以帮忙!
提前谢谢!