我在聊天应用程序中使用以下适配器进行recyclerview但在滚动时我自动定位已更改。我使用了getItemViewType()我认为这会带来问题。
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyViewHolder> {
Context context;
List<Message> messageList;
private int SELF = 7861212;
public MessageAdapter(Context context, List<Message> messages) {
messageList = messages;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mview = null;
if (viewType == SELF) {
mview = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_user2_item, parent, false);
} else {
mview = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_user1_item, parent, false);
}
return new MyViewHolder(mview);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Message message = messageList.get(position);
holder.message_text.setText(message.getText());
holder.chat_company_reply_author.setText(message.getName());
if (message.isfile()) {
if (message.getMessageStatus() != null && message.getMessageStatus() == Status.DELIVERED) {
holder.rl_file.setVisibility(View.VISIBLE);
holder.progress_bar1.setVisibility(View.GONE);
try {
holder.user_reply_status.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_double_tick));
} catch (Exception e) {
e.printStackTrace();
}
}
if (message.getFiletype() != null) {
holder.rl_file.setVisibility(View.VISIBLE);
}
} else {
if (message.getMessageStatus() != null && message.getMessageStatus() == Status.DELIVERED) {
try {
holder.user_reply_status.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_double_tick));
} catch (Exception e) {
e.printStackTrace();
}
}
}
try {
Picasso.with(context).
load(Constant.BASE_URL_FORDOWNLOD + "" + message.getFileurl()).
placeholder(R.mipmap.ic_launcher).
into(holder.img_chat);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
Message message = messageList.get(position);
if (message.getUserType() == UserType.SELF) {
return SELF;
}
return position;
}
@Override
public int getItemCount() {
return messageList.size();
}
public void add(Message message, RecyclerView recyclerView) {
messageList.add(message);
notifyItemInserted(messageList.size() - 1);
}
public void mesage_status(int issent, int position) {
Message message = messageList.get(position);
if (issent == 1) {
message.setMessageStatus(Status.SENT);
}
if (issent == 2) {
message.setMessageStatus(Status.DELIVERED);
}
notifyItemChanged(position);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView chat_company_reply_author, message_text, time_text;
public ImageView img_chat, user_reply_status;
public RelativeLayout rl_file;
public ProgressBar progress_bar1;
public MyViewHolder(View itemView) {
super(itemView);
img_chat = (ImageView) itemView.findViewById(R.id.img_chat);
user_reply_status = (ImageView) itemView.findViewById(R.id.user_reply_status);
chat_company_reply_author = (TextView) itemView.findViewById(R.id.chat_company_reply_author);
message_text = (TextView) itemView.findViewById(R.id.message_text);
time_text = (TextView) itemView.findViewById(R.id.time_text);
rl_file = (RelativeLayout) itemView.findViewById(R.id.rl_file);
progress_bar1 = (ProgressBar) itemView.findViewById(R.id.progress_bar1);
}
public void setvalue(Message message, int position) {
message_text.setText(message.getText());
chat_company_reply_author.setText(message.getName());
if (message.isfile()) {
if (message.getMessageStatus() != null && message.getMessageStatus() == Status.DELIVERED) {
rl_file.setVisibility(View.VISIBLE);
progress_bar1.setVisibility(View.GONE);
try {
user_reply_status.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_double_tick));
} catch (Exception e) {
e.printStackTrace();
}
}
if (message.getFiletype() != null) {
rl_file.setVisibility(View.VISIBLE);
}
} else {
if (message.getMessageStatus() != null && message.getMessageStatus() == Status.DELIVERED) {
try {
user_reply_status.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_double_tick));
} catch (Exception e) {
e.printStackTrace();
}
}
}
try {
Picasso.with(context).
load(Constant.BASE_URL_FORDOWNLOD + "" + message.getFileurl()).
placeholder(R.mipmap.ic_launcher).
into(img_chat);
} catch (Exception e) {
e.printStackTrace();
}
}
}}
答案 0 :(得分:-1)
我遇到的问题是当我使用holder.rl_file.setVisibility(View.VISIBLE);
时,holder.rl_file.setVisibility(View.GONE);
处于错误状态。