我创建了聊天应用程序。在那我添加了显示时间并完成了所有组,但是每当我向上滚动回收视图时,我就会得到重复的项目。我通过不可见的方式来处理该组,但是当向上滚动和向下滚动时它仍然看起来很完美。
适配器类:-
public class WocdataAdapter extends RecyclerView.Adapter {
private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
private static final String TAG =
WocdataAdapter.class.getSimpleName();
private WocdeleteInterface wocdeleteInterface;
private Context mContext;
private List<Getmessage> mMessageList;
private boolean sameDay, yesterDay,minutenow;
private String timeset = "";
private String tnow = "Now", tminute = "Minute", ttoday =
"Today", tyesterday = "Yesterday", tweek = "Week", tmonth =
"Month", ndate = "";
public WocdataAdapter(Context context, List<Getmessage> message)
{
mContext = context;
mMessageList = message;
}
public void setinstance(WocdeleteInterface wocdesignActivity) {
wocdeleteInterface = wocdesignActivity;
}
@Override
public int getItemViewType(int position) {
Getmessage getmessage = mMessageList.get(position);
if (getmessage.getCheck() == 1) {
return VIEW_TYPE_MESSAGE_SENT;
} else {
return VIEW_TYPE_MESSAGE_RECEIVED;
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull
ViewGroup viewGroup, int i) {
View view;
if (i == VIEW_TYPE_MESSAGE_RECEIVED) {
view=LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.include_recycle_receivemsg, viewGroup, false);
return new ReceivedMessageHolder(view);
} else if (i == VIEW_TYPE_MESSAGE_SENT) {
view =
LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.include_recycle_
sentmsg, viewGroup, false);
return new SentMessageHolder(view);
}
throw new RuntimeException("Could not inflate layout");
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder
viewHolder, int i) {
Getmessage getmessage = mMessageList.get(i);
switch (viewHolder.getItemViewType()) {
case VIEW_TYPE_MESSAGE_SENT:
((SentMessageHolder) viewHolder).bind(getmessage, i);
break;
case VIEW_TYPE_MESSAGE_RECEIVED:
((ReceivedMessageHolder) viewHolder).bind(getmessage,
i);
break;
}
}
private class SentMessageHolder extends RecyclerView.ViewHolder {
TextView msgsent, timesent, snow;
ImageView msgsentdelete, msgsentedit, msgimgsent;
LinearLayout wocdaylay;
SentMessageHolder(View view) {
super(view);
msgsent = view.findViewById(R.id.msgsent);
timesent = view.findViewById(R.id.timesent);
msgsentdelete = view.findViewById(R.id.msgsentdelete);
msgsentedit = view.findViewById(R.id.msgsentedit);
msgimgsent = view.findViewById(R.id.msgimgsent);
wocdaylay = view.findViewById(R.id.wocdaylay);
snow = view.findViewById(R.id.snow);
}
void bind(final Getmessage getmessage, final int i) {
sentset(wocdaylay, snow, i);
File file = new File(getmessage.getImagepath());
if (file.exists()) {
Bitmap photo =
BitmapFactory.decodeFile(file.getAbsolutePath());
msgimgsent.setImageBitmap(photo);
msgimgsent.setVisibility(View.VISIBLE);
} else {
msgimgsent.setVisibility(View.GONE);
}
msgsent.setText(getmessage.getMessage());
msgsentdelete.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
msgdelete(v, i);
}
});
msgsentedit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
edittext(getmessage, v, i);
}
});
timesent.setText(getmessage.getTime());
}
private void sentset(LinearLayout wocdaylay, TextView snow,
int i) {
String temp = tempset(i);
if (i == 0) {
timeset = temp;
wocdaylay.setVisibility(View.VISIBLE);
snow.setText(temp);
} else {
if (temp.equals(timeset)) {
wocdaylay.setVisibility(View.GONE);
} else {
timeset = temp;
wocdaylay.setVisibility(View.VISIBLE);
snow.setText(temp);
}
}
}
}
private String tempset(int i) {
ncalender(i);
String temp = "";
temp = ndate;
if (yesterDay) {
temp = tyesterday;
}
if (sameDay) {
temp = ttoday;
}
return temp;
}
private class ReceivedMessageHolder extends
RecyclerView.ViewHolder {
ImageView msgreceiveedit, msgreceivedelete, msgimgreceive;
TextView msgreceive, timereceive, rnow;
LinearLayout wocdaylay1;
ReceivedMessageHolder(View view) {
super(view);
msgreceive = view.findViewById(R.id.msgreceive);
timereceive = view.findViewById(R.id.timereceive);
msgreceiveedit = view.findViewById(R.id.msgreceiveedit);
msgreceivedelete =
view.findViewById(R.id.msgreceivedelete);
msgimgreceive = view.findViewById(R.id.msgimgreceive);
wocdaylay1 = view.findViewById(R.id.wocdaylay1);
rnow = view.findViewById(R.id.rnow);
}
void bind(final Getmessage getmessage, final int i) {
receiveset(wocdaylay1, rnow, i);
File file = new File(getmessage.getImagepath());
if (file.exists()) {
Bitmap photo =
BitmapFactory.decodeFile(file.getAbsolutePath());
msgimgreceive.setVisibility(View.VISIBLE);
msgimgreceive.setImageBitmap(photo);
} else {
msgimgreceive.setVisibility(View.GONE);
}
msgreceive.setText(getmessage.getMessage());
msgreceivedelete.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
msgdelete(v, i);
}
});
msgreceiveedit.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
edittext(getmessage, v, i);
}
});
timereceive.setText(getmessage.getTime());
}
private void receiveset(LinearLayout wocdaylay1, TextView
rnow, int i) {
String temp = tempset(i);
if (i == 0) {
timeset = temp;
wocdaylay1.setVisibility(View.VISIBLE);
rnow.setText(temp);
} else {
if (temp.equals(timeset)) {
wocdaylay1.setVisibility(View.GONE);
} else {
timeset = temp;
wocdaylay1.setVisibility(View.VISIBLE);
rnow.setText(temp);
}
}
}
}
private void ncalender(int i) {
Getmessage previousMessage = mMessageList.get(i);
Calendar previoustime = previousMessage.getPast();
Calendar newtime = Calendar.getInstance();
minutenow = newtime.get(Calendar.YEAR) ==
previoustime.get(Calendar.YEAR)
&& newtime.get(Calendar.MONTH) ==
previoustime.get(Calendar.MONTH)
&& newtime.get(Calendar.DAY_OF_YEAR) ==
previoustime.get(Calendar.DAY_OF_YEAR)
&& newtime.get(Calendar.HOUR_OF_DAY) ==
previoustime.get(Calendar.HOUR_OF_DAY)
&& Math.round(newtime.get(Calendar.MINUTE) -
previoustime.get(Calendar.MINUTE)) == 0;
sameDay = newtime.get(Calendar.YEAR) ==
previoustime.get(Calendar.YEAR)
&& newtime.get(Calendar.MONTH) ==
previoustime.get(Calendar.MONTH)
&& newtime.get(Calendar.DAY_OF_YEAR) ==
previoustime.get(Calendar.DAY_OF_YEAR);
yesterDay = newtime.get(Calendar.YEAR) ==
previoustime.get(Calendar.YEAR)
&& newtime.get(Calendar.MONTH) ==
previoustime.get(Calendar.MONTH)
&& Math.round(newtime.get(Calendar.DAY_OF_YEAR) -
previoustime.get(Calendar.DAY_OF_YEAR)) == 1;
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy",
Locale.getDefault());
ndate = format.format(previoustime.getTime());
}
private void msgdelete(View v, final int i) {
final AlertDialog alertDialog = new
AlertDialog.Builder(v.getContext()).create();
alertDialog.setIcon(R.drawable.ic_delete);
alertDialog.setCancelable(false);
alertDialog.setTitle("Delete");
alertDialog.setMessage("Are You Sure ?");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "YES", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mMessageList.remove(i);
notifyDataSetChanged();
dialog.dismiss();
if (wocdeleteInterface != null) {
Log.i(TAG, "onClick:delete pos " + i);
wocdeleteInterface.getpossition(i);
}
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "NO", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "No",
Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
alertDialog.show();
}
private void edittext(final Getmessage getmessage, View v, final
int i) {
final Dialog dialog = new Dialog(v.getContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Objects.requireNonNull
(dialog.getWindow()).requestFeature(Window.FEATURE_NO_T
ITLE);
}
dialog.setContentView(R.layout.include_wocedittext);
final TextView wocedittext =
dialog.findViewById(R.id.wocedittext);
Button woceditsave = dialog.findViewById(R.id.woceditsave);
dialog.show();
wocedittext.setText(getmessage.getMessage());
woceditsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if
(wocedittext.getText().toString().equals(getmessage.getMessage()))
{
dialog.dismiss();
} else {
getmessage.setMessage(wocedittext.getText().toString());
notifyDataSetChanged();
Toast.makeText(v.getContext(), "Saved",
Toast.LENGTH_SHORT).show();
if (wocdeleteInterface != null) {
Log.i(TAG, "onClick:delete pos " + i);
wocdeleteInterface.getpossition(i);
}
dialog.dismiss();
}
}
});
}
@Override
public int getItemCount() {
return mMessageList.size();
}
}
Activity Class where Adapter Call :-
private void populateadapter() {
wocdataAdapter = new WocdataAdapter(getApplicationContext(),
messageList);
wocdataAdapter.setinstance(this); // Interface
wocdesignrecycle.setAdapter(wocdataAdapter);
}
并且在Insertdata中轻松更改Notifydataset。