如何在列表视图中隐藏同一用户消息的第二个用户名?
How to hide a second name of user of message the same user in listview?
我试着和 socket.io 聊天。我希望只有同一用户的消息的用户名字可见。
例如:
Username | Message
item: Hans: hello (first message with name of user)
item: How are you? (second message without username)
item: Look: Thanks, good!
item: you? (second message without username)
下面的代码时好时坏。在滚动列表视图时,有时同一用户的第二个用户名是可见的,其他时候是隐藏的。我怎样才能隐藏同一用户的用户的第二个名字和分隔符?
简单代码:
package com.example.seadog.fb_dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class MyBaseAdapter extends BaseAdapter {
Context context;
ArrayList<ListData> items = new ArrayList();
LayoutInflater inflater;
int id = 0;
public MyBaseAdapter(Context context, ArrayList items) {
this.context = context;
this.items = items;
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return items.size();
}
@Override
public ListData getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
ListData currentListData = getItem(position);
mViewHolder.Title.setText(currentListData.getTitle());
mViewHolder.Desc.setText(currentListData.getDescription());
if (id != currentListData.getID() || position == 0) {
mViewHolder.Title.setVisibility(View.VISIBLE);
}else{
mViewHolder.Title.setVisibility(View.GONE);
}
id = currentListData.getID();
return convertView;
}
private class MyViewHolder {
TextView Title, Desc;
public MyViewHolder(View item) {
Title = (TextView) item.findViewById(R.id.txtitem);
Desc = (TextView) item.findViewById(R.id.descitem);
}
}
}
您可以将其更改为:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
id = position > 0 ? getItem(position - 1).getID() : -1;
ListData currentListData = getItem(position);
mViewHolder.Title.setText(currentListData.getTitle());
mViewHolder.Desc.setText(currentListData.getDescription());
if (id == -1 && id != currentListData.getID()) {
mViewHolder.Title.setVisibility(View.VISIBLE);
} else {
mViewHolder.Title.setVisibility(View.GONE);
}
return convertView;
}
您无法确定 getView() 是否会按顺序位置调用,例如在向上滚动时。
我试着和 socket.io 聊天。我希望只有同一用户的消息的用户名字可见。
例如:
Username | Message
item: Hans: hello (first message with name of user)
item: How are you? (second message without username)
item: Look: Thanks, good!
item: you? (second message without username)
下面的代码时好时坏。在滚动列表视图时,有时同一用户的第二个用户名是可见的,其他时候是隐藏的。我怎样才能隐藏同一用户的用户的第二个名字和分隔符?
简单代码:
package com.example.seadog.fb_dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class MyBaseAdapter extends BaseAdapter {
Context context;
ArrayList<ListData> items = new ArrayList();
LayoutInflater inflater;
int id = 0;
public MyBaseAdapter(Context context, ArrayList items) {
this.context = context;
this.items = items;
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return items.size();
}
@Override
public ListData getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
ListData currentListData = getItem(position);
mViewHolder.Title.setText(currentListData.getTitle());
mViewHolder.Desc.setText(currentListData.getDescription());
if (id != currentListData.getID() || position == 0) {
mViewHolder.Title.setVisibility(View.VISIBLE);
}else{
mViewHolder.Title.setVisibility(View.GONE);
}
id = currentListData.getID();
return convertView;
}
private class MyViewHolder {
TextView Title, Desc;
public MyViewHolder(View item) {
Title = (TextView) item.findViewById(R.id.txtitem);
Desc = (TextView) item.findViewById(R.id.descitem);
}
}
}
您可以将其更改为:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
id = position > 0 ? getItem(position - 1).getID() : -1;
ListData currentListData = getItem(position);
mViewHolder.Title.setText(currentListData.getTitle());
mViewHolder.Desc.setText(currentListData.getDescription());
if (id == -1 && id != currentListData.getID()) {
mViewHolder.Title.setVisibility(View.VISIBLE);
} else {
mViewHolder.Title.setVisibility(View.GONE);
}
return convertView;
}
您无法确定 getView() 是否会按顺序位置调用,例如在向上滚动时。