Listview中位置更改时Textview填充更改

时间:2014-06-18 14:32:56

标签: android listview textview

我正在尝试创建一个listview项目,在textview右侧有一个imageview ..让我们说如果文本是3行我希望前两行的文本在图像视图后面开始有一些填充..并且第三行位于图像的底部,没有填充..

我在列表适配器中使用此代码来执行此操作..

    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());
        }
    }

我不知道为什么会这样。你能帮帮我吗?

3 个答案:

答案 0 :(得分:1)

好的..我在设置我的持有人的标签之前,通过我的getView()方法在convertview的条件下设置我的imageView背景解决了这个问题..就像这样:

    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(); 
    }

答案 1 :(得分:0)

请注意适配器中getView()方法中的if ... else块。如果在if语句中将LinearLayout的背景颜色设置为红色,则应将“原始”颜色放在else语句中。这是由ListView回收视图引起的。您可以更好地解释here

答案 2 :(得分:0)

尝试在if(sd.exist())块中添加“else”。我认为这段代码在getView()中。