好,所以我在使用listview适配器时遇到了问题。当我向下或向上滚动时,它的位置错误,因此在我的视图上生成了错误的信息。
然后我在getView
方法中更改了
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(mContext);
v = vi.inflate(resourceLayout, null);
}
到
LayoutInflater vi;
vi = LayoutInflater.from(mContext);
View v = vi.inflate(resourceLayout, null);
它解决了问题。但是我不知道为什么。我的猜测是它正在创建新视图而不是回收它们。如果是这样,会消耗大量内存并降低性能吗?
编辑: 有人告诉我最好发布我的getView方法,所以它是这样的:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater vi;
vi = LayoutInflater.from(mContext);
View v = vi.inflate(resourceLayout, null);
MessageInChat message = getItem(position);
if (message.uri == null) {
if (message.location == null) {
RoundedImageView imageView = v.findViewById(R.id.image_in_chat);
imageView.setVisibility(View.GONE);
TextView textView = v.findViewById(R.id.message_template_text);
LinearLayout container = v.findViewById(R.id.message_container);
container.setVisibility(View.VISIBLE);
LinearLayout linearLayout = v.findViewById(R.id.linear_message_container);
textView.setText(message.text);
if (message.author) {
textView.setTextColor(mContext.getResources().getColor(R.color.white));
linearLayout.setGravity(Gravity.END);
container.setBackground(mContext.getResources().getDrawable(R.drawable.self_message_background));
} else {
textView.setTextColor(mContext.getResources().getColor(R.color.textColor));
linearLayout.setGravity(Gravity.START);
container.setBackground(mContext.getResources().getDrawable(R.drawable.other_message_background));
}
} else {
RoundedImageView imageView = v.findViewById(R.id.image_in_chat);
imageView.setVisibility(View.GONE);
final TextView textView = v.findViewById(R.id.message_template_text);
LinearLayout container = v.findViewById(R.id.message_container);
container.setVisibility(View.VISIBLE);
LinearLayout linearLayout = v.findViewById(R.id.linear_message_container);
textView.setText(message.location);
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
if (message.author) {
textView.setTextColor(mContext.getResources().getColor(R.color.white));
linearLayout.setGravity(Gravity.END);
container.setBackground(mContext.getResources().getDrawable(R.drawable.self_message_background));
} else {
textView.setTextColor(mContext.getResources().getColor(R.color.textColor));
linearLayout.setGravity(Gravity.START);
container.setBackground(mContext.getResources().getDrawable(R.drawable.other_message_background));
}
final String location = message.location;
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, ViewLocationActivity.class);
intent.putExtra("URL", location);
mContext.startActivity(intent);
}
});
}
} else {
ImageView imageView = v.findViewById(R.id.image_in_chat);
imageView.setVisibility(View.VISIBLE);
LinearLayout container = v.findViewById(R.id.message_container);
container.setVisibility(View.GONE);
LinearLayout linearLayout = v.findViewById(R.id.linear_message_container);
if (message.author) {
linearLayout.setGravity(Gravity.END);
} else {
linearLayout.setGravity(Gravity.START);
}
String uri = message.uri;
try {
int width = message.width;
int height = message.height;
int layoutWidth = imageView.getLayoutParams().width;
int resultHeight = layoutWidth * height / width;
imageView.getLayoutParams().height = resultHeight;
Glide.with(mContext).load(Uri.parse(uri)).into(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
return v;
}
它用于基本的聊天屏幕,其中包含三种类型的消息:普通文本,图像和Google地图链接