我正在android中使用xmpp.My实现聊天应用聊天工作正常,但是当我为发件人和接收者在聊天中设置布局时,inflater正在更改布局所有现有的列表项目。 下面是截图,当用户发送“你好” Recycler视图改变所有列表项目的布局不适用于单个列表项目
但是,当接收器发送“你好”,那么这聊天消息给正在添加左侧位置,但在发送邮件时它也采取其他消息向左现在的位置和相同.. 下面是截图,当用户在发送时,“你好吗”
我不其中ia接受“你好”
,随后由该错了
我回收视图发件人布局
chat_layout_self.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="right|bottom"
android:gravity="right|bottom"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/chatsymbolself">
<TextView
android:id="@+id/cltv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hi"
android:textSize="20sp"
android:textColor="#fff"
android:padding="10dp"
android:layout_gravity="left|bottom"/>
</LinearLayout>
</LinearLayout>
chat_layout_other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="left|bottom"
android:gravity="left|bottom"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/chatsymbolother">
<TextView
android:id="@+id/cltv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hi"
android:textSize="20sp"
android:textColor="#fff"
android:padding="10dp"
android:layout_gravity="left"/>
</LinearLayout>
</LinearLayout>
MessageAdapter.java
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyViewHolder> {
private String user="[email protected]/Android";
private LayoutInflater inflater;
List<Message> data= Collections.emptyList();
public MessageAdapter(Context context,List<Message> data){
inflater=LayoutInflater.from(context);
this.data=data;
}
@Override
public int getItemViewType(int position) {
Message message = data.get(position);
Log.e("MAdapter", "getItemViewType: from "+message.from);
if (message.from.equals(user)){
return 0;
}
else {
return 1;
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
MyViewHolder holder;
if(viewType==0){
Log.e("MAdapter", "onCreateViewHolder: SEFL");
view=LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_layout_self,parent,false);
}
else{
Log.e("MAdapter", "onCreateViewHolder: OTHER");
view=LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_layout_other,parent,false);
}
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final int itemType = getItemViewType(position);
Message current=data.get(position);
holder.chat.setText(current.msg);
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView chat;
public MyViewHolder(View itemView) {
super(itemView);
chat=(TextView)itemView.findViewById(R.id.cltv);
}
}
}
你可以发布日志吗?特别是Log.e(“MAdapter”,“getItemViewType:from”+ message.from);'应该提供一些洞察力 –
你也可以在你处理一个新的'Message'的地方发布代码吗?即如何将新对象添加到“列表数据”以及如何向MessageAdapter通知更改。 –
我认为没有什么与日志...这些只是在logcat显示用户名.. @Mr。凯文托马斯 –