Android 当Listview中的位置更改时,Textview填充会更改
我正在尝试创建一个listview项目,其文本视图右侧有一个imageview。。假设文本为3行,我希望前2行的文本在图像视图之后开始,并添加一些填充。。第三行在图像的底部,没有填充 我在列表适配器中使用了此代码来完成此操作Android 当Listview中的位置更改时,Textview填充会更改,android,listview,textview,Android,Listview,Textview,我正在尝试创建一个listview项目,其文本视图右侧有一个imageview。。假设文本为3行,我希望前2行的文本在图像视图之后开始,并添加一些填充。。第三行在图像的底部,没有填充 我在列表适配器中使用了此代码来完成此操作 TextView commenterName = (TextView) row .findViewById(R.id.commenter_name); commentText = (TextView) row.findViewById
TextView commenterName = (TextView) row
.findViewById(R.id.commenter_name);
commentText = (TextView) row.findViewById(R.id.tv);
ImageView commenterPhoto = (ImageView) row
.findViewById(R.id.commenterPhoto);
Display display = ((WindowManager) row.getContext()
.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
FlowTextHelper.tryFlowText(commentsArray.get(pos).getComment(),
commenterPhoto, commentText, display, 0);
commenterName.setText(commentsArray.get(pos).getCommenter());
commentText.setTextColor(context.getResources().getColor(R.color.Black));
commentText.setGravity(Gravity.RIGHT);
这个工作很好。。但是,当滚动列表视图时,文本会丢失其填充,并越过图像而离开屏幕
更新:
这就是导致问题的部分。。我将else条件设置为默认照片,但问题仍然存在
String path = url.substring(url.lastIndexOf("/") + 1, url.length());
sd = new File(Environment.getExternalStorageDirectory()
+ Constants.user_images_path + path);
if (sd.exists()) {
Bitmap thumbnail = null;
try {
//File filePath = context.getFileStreamPath(url);
FileInputStream fi = new FileInputStream(sd);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
thumbnail = BitmapFactory.decodeStream(fi, null, options);
if(thumbnail != null)
holder.commenterPhoto.setImageBitmap(thumbnail);
else
holder.commenterPhoto.setBackgroundResource(R.drawable.commenter_default);
} catch (Exception ex) {
Log.e("getThumbnail() on internal storage", ex.getMessage());
}
}
我不知道为什么会这样。。你能帮我吗 请小心适配器中getView()方法内的if…else块。如果在If语句中以红色设置LinearLayout的背景色,则应在else语句中设置“原始”颜色。这是由ListView循环视图引起的。您可以看到更好的解释尝试在if(sd.exist())块中添加一个“else”。我假设这段代码在getView()中。好的。。我解决了这个问题,在设置我的持有者的标记之前,我在getView()方法中将imageView背景设置为convertview的条件。。就这样,
View row = convertView;
if (row == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = infalInflater.inflate(resource, null);
holder = new ViewHolder();
holder.commentText = (TextView) row.findViewById (R.id.tv);
holder.commenterPhoto = (ImageView) row.findViewById (R.id.commenterPhoto);
holder.commenterName = (TextView) row.findViewById (R.id.commenter_name);
holder.date = (TextView) row.findViewById (R.id.date);
holder.show = (ImageButton) row.findViewById(R.id.show);
String path = url.substring(url.lastIndexOf("/") + 1, url.length());
sd = new File(Environment.getExternalStorageDirectory()
+ Constants.user_images_path + path);
Bitmap thumbnail = null;
if (sd.exists()) {
try {
//File filePath = context.getFileStreamPath(url);
FileInputStream fi = new FileInputStream(sd);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
thumbnail = BitmapFactory.decodeStream(fi, null, options);
if(thumbnail != null)
holder.commenterPhoto.setImageBitmap(thumbnail);
else
holder.commenterPhoto.setBackgroundResource(R.drawable.commenter_default);
} catch (Exception ex) {
Log.e("getThumbnail() on internal storage", ex.getMessage());
}
}else{
Toast.makeText(context, "No Image Found", Toast.LENGTH_LONG).show();
}
row.setTag(holder);
}
else{
holder = (ViewHolder) row.getTag();
}
请提供完整的ListAdapter代码。getView()方法中似乎有些地方不太正确。请提供这一点。既然可以在xml中轻松应用,为什么要在java中设置这么多属性?谢谢。。这真的帮助了我:)我已经试过了。。它不起作用:(是的,这段代码在getView()中);