数据流是正确的,我可以在日志中查看行的值,但我的视图在屏幕上不可见。有人可以查看这段代码。不确定为什么我的观点没有出现在屏幕上。
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Message message = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
if (message.getImagePath().isEmpty()) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.message_text_item, parent, false);
if (message.getSender().equalsIgnoreCase("alpha")) {
TextView text = (TextView) convertView.findViewById(R.id.text1);
text.setGravity(Gravity.LEFT);
text.setText(message.getText());
} else {
TextView text = (TextView) convertView.findViewById(R.id.text1);
text.setGravity(Gravity.RIGHT);
text.setText(message.getText());
}
} else {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.message_image_item, parent, false);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
Picasso.with(getContext()).load(message.getImagePath()).fit().centerCrop().into(imageView);
}
}
// Return the completed view to render on screen
return convertView;
}
message_image_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/common_full_open_on_phone"
android:id="@+id/imageView1" />
</LinearLayout>
message_text_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearlayout1">
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
感谢
答案 0 :(得分:0)
您没有重复使用虚增的视图。你应该这样做:
public View getView(int position, View convertView, ViewGroup parent) {
// check the view creation
if (convertView == null) {
// inflate your view here
} else {
// reuse the view inflated
}
// set the text, image, etc.
return convertView; // return it
}
从您的代码中,您可以选择切换到ViewType
条件。这将允许您获得两个不同的布局。此外,使用ViewHolder
可能会更好。这是实施:
private final static ROWTYPE_WITH_TEXT = 0;
private final static ROWTYPE_WITH_IMAGE = 1;
private class ViewHolder {
private TextView text;
private ImageView imageView;
}
...
@Override
public int getViewTypeCount() { return 2; }
@Override
public int getItemViewType(int position) {
return ((Message) getItem(position)).getImagePath().isEmpty()
? ROWTYPE_WITH_TEXT : ROWTYPE_WITH_IMAGE;
// if the item in the list of messages contains
// an image path, then choose the row ROWTYPE_WITH_IMAGE
}
...
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder holder;
int rowtype = getItemViewType(position); // select the rowtype
if (view == null) {
holder = new ViewHolder(); // init the viewholder
LayoutInflater inflater = LayoutInflater.from(context); // init the inflater
// select the specific layout
if (rowtype == ROWTYPE_WITH_TEXT) {
view = inflater.inflate(R.layout.message_text_item, null);
holder.text = (TextView) view.findViewById(R.id.text1);
} else {
view = inflater.inflate(R.layout.message_image_item, null);
holder.imageView = (ImageView) view.findViewById(R.id.imageView1);
}
view.setTag(holder); // keep the existing view
} else {
holder = (ViewHolder) view.getTag(); // reuse the view
}
Message message = getItem(position); // get the message
// from the rowtype, set the elements (texts, images, etc)
if (rowtype == ROWTYPE_WITH_TEXT)
if (message.getSender().equalsIgnoreCase("alpha")) {
holder.text.setGravity(Gravity.LEFT);
holder.text.setText(message.getText());
} else {
holder.text.setGravity(Gravity.RIGHT);
holder.text.setText(message.getText());
}
} else {
Picasso.with(getContext())
.load(message.getImagePath())
.fit()
.centerCrop()
.into(holder.imageView);
}
return convertView; // return it
}