嘿伙计我正在创建聊天应用程序,比如whatsapp但是在RecyclerView中遇到问题Childs Visibilties这里它包含我的消息和来自服务器的消息我已经在单一布局中为我的消息和服务器消息定义了两个项目而我是基于布尔值设置可见性;但是项目没有正确呈现
适配器类:
public class MessageDetailsListAdapter extends RecyclerView.Adapter<MessageDetailsListAdapter.MessageViewHolder> {
private List<MessageModel> messagesList;
private Activity activityContext;
public MessageDetailsListAdapter(List<MessageModel> messagesList,Activity activityContext) {
this.messagesList = messagesList;
this.activityContext = activityContext;
}
@Override
public int getItemCount() {
return messagesList.size();
}
@Override
public void onBindViewHolder(MessageViewHolder messageViewHolder, final int i) {
final MessageModel msg = messagesList.get(i);
if(msg.getIsMessageSentbyMe())
{
messageViewHolder.card_linear.setVisibility(View.GONE);
messageViewHolder.messageTextMe.setText(msg.getMsgText());
messageViewHolder.userNameMe.setText(msg.getSenderName());
messageViewHolder.timeStampMe.setText(msg.getSentTime());
messageViewHolder.card_linearMe.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(activityContext.getString(R.string.brMessageDetailsDeleteItem));
intent.putExtra("deletePosition", i);
activityContext.sendBroadcast(intent);
return false;
}
});
}
else
{
messageViewHolder.card_linearMe.setVisibility(View.GONE);
messageViewHolder.messageText.setText(msg.getMsgText());
messageViewHolder.userName.setText(msg.getSenderName());
messageViewHolder.timeStamp.setText(msg.getSentTime());
messageViewHolder.card_linear.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(activityContext.getString(R.string.brMessageDetailsDeleteItem));
intent.putExtra("deletePosition", i);
activityContext.sendBroadcast(intent);
return false;
}
});
}
}
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.messagedetail_listitem, viewGroup,false);
return new MessageDetailsListAdapter.MessageViewHolder(itemView);
}
public static class MessageViewHolder extends RecyclerView.ViewHolder {
public LinearLayout card_linear,card_linearMe;
public TextView userName,messageText,timeStamp,userNameMe,messageTextMe,timeStampMe;
public MessageViewHolder(View v) {
super(v);
card_linear = (LinearLayout) v.findViewById(R.id.card_linear);
card_linearMe = (LinearLayout) v.findViewById(R.id.card_linearMe);
userName = (TextView) v.findViewById(R.id.userName);
messageText = (TextView) v.findViewById(R.id.messageText);
timeStamp = (TextView) v.findViewById(R.id.timeStamp);
userNameMe = (TextView) v.findViewById(R.id.userNameMe);
messageTextMe = (TextView) v.findViewById(R.id.messageTextMe);
timeStampMe = (TextView) v.findViewById(R.id.timeStampMe);
}
}
public void add(MessageModel messageModel) {
// TODO Auto-generated method stub
messagesList.add(messageModel);
notifyItemInserted(messagesList.size());
}
public void addAll(Collection<MessageModel> messages)
{
messagesList.clear();
messagesList.addAll(messagesList);
notifyDataSetChanged();
}
public void remove(MessageModel mDelete) {
// TODO Auto-generated method stub
messagesList.remove(mDelete);
notifyDataSetChanged();
}
}
messagedetail_listitem.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="@dimen/message_detail_padding_top" >
<LinearLayout
android:id="@+id/card_linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/card_liner_margin_left"
android:layout_marginRight="@dimen/card_liner_margin_right"
android:background="@drawable/message_holder_white"
android:orientation="vertical"
android:padding="@dimen/card_linear_padding" >
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Me"
android:paddingLeft="@dimen/card_linear_padding"
style="@style/MessageUserNameFont"
/>
<TextView
android:id="@+id/messageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/message_text_margin_top"
android:paddingBottom="@dimen/message_text_padding"
android:paddingLeft="@dimen/message_text_padding"
android:paddingTop="@dimen/message_text_padding"
style="@style/MessageTextFont"
android:text="hii fdsfdsfsdf sdf dsf sdfs"
/>
<TextView
android:id="@+id/timeStamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="@dimen/time_stamp_margin_right"
android:gravity="right"
android:text="22:20"
style="@style/MessageTextFont"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/card_linearMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/card_linearme_margin_left"
android:layout_marginRight="@dimen/card_linearme_margin_right"
android:background="@drawable/message_holder_blue"
android:orientation="vertical"
android:padding="@dimen/card_linearme_padding" >
<TextView
android:id="@+id/userNameMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Me"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/messageTextMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/message_text_margin_top"
android:paddingBottom="@dimen/message_text_padding"
android:paddingRight="@dimen/message_text_padding"
android:paddingTop="@dimen/message_text_padding"
android:text="hii"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/timeStampMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="@dimen/time_stamp_margin_right"
android:gravity="right"
android:text="22:20"
android:textColor="@android:color/white" />
</LinearLayout>
答案 0 :(得分:4)
重要的是要理解视图持有者的重用。
首先,你有这个条件:
if(msg.getIsMessageSentbyMe())
{
messageViewHolder.card_linear.setVisibility(View.GONE);
...
}
以后你有:
else
{
messageViewHolder.card_linearMe.setVisibility(View.GONE);
}
现在假设您第一次使用VH并且(例如)您发送的消息。您已有效隐藏card_linear
。
假设现在需要重新使用视图持有者来查找未由您发送的消息。您还将使card_linearMe
的可见性消失。
你的VH发生的事情是这两种布局在这样的场景之后是不可见的。要解决此问题,请添加以下行:
if(msg.getIsMessageSentbyMe())
{
messageViewHolder.card_linear.setVisibility(View.GONE);
messageViewHolder.card_linearMe.setVisibility(View.VISIBLE);
...
}
else
{
messageViewHolder.card_linear.setVisibility(View.VISIBLE);
messageViewHolder.card_linearMe.setVisibility(View.GONE);
}