ListView动态数据行在滚动时更改

时间:2015-11-02 09:13:38

标签: android android-layout android-listview android-view android-recyclerview

我想更改文本视图的文本和背景颜色,从#34; Not Uploaded"到"上传......"并希望改变背景颜色。但是当我点击上传按钮时,它会更改所选视图的颜色和文本,但它也会对最后一个可见项+ 1进行相同的更改。当我滚动列表时,更改效果也会继续转移到其他视图上。 的 strong text

以下是我的自定义适配器getview功能的代码

 public View getView(int position, View convertView, ViewGroup parent) {

    View v = null;

    if(convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v=inflater.inflate(R.layout.fileview,parent,false);
        TextView tv = (TextView) v.findViewById(R.id.tv_file);
        TextView tv_date = (TextView) v.findViewById(R.id.tv_fileDate);
        TextView tvFileSize = (TextView) v.findViewById(R.id.tvFileSize);
        TextView tvFileStatus = (TextView) v.findViewById(R.id.tvUploadStatus);
        ImageView _img = (ImageView) v.findViewById(R.id.iv_file);
        ImageView btnTrash = (ImageView) v.findViewById(R.id.btnTrash);
        ImageView btnEdit = (ImageView) v.findViewById(R.id.btnEdit);
        ImageView btnUpload = (ImageView) v.findViewById(R.id.btnUpload);

        btnTrash.setOnClickListener(new myclickListener(context,files[position],position));
        btnEdit.setOnClickListener(new myclickListener(context,files[position],position));
        btnUpload.setOnClickListener(new myclickListener(context,files[position],position));



        int index = files[position].lastIndexOf(".");
        String file_extention = files[position].substring(index+1,files[position].length());
        file_extention=file_extention.toLowerCase();

        if(file_extention.equals("png"))
        {
            _img.setImageResource(images[0]);
        }
        else if(file_extention.equals("txt"))
        {
            _img.setImageResource(images[1]);
        }

        if(appPreferences.getUploadValue(files[position]).equals(statusUploaded))
        {
            tvFileStatus.setTextColor(Color.WHITE);
            tvFileStatus.setText(statusUploaded);
            tvFileStatus.setBackgroundResource(R.drawable.fileview2);
        }
        else if (appPreferences.getUploadValue(files[position]).equals(statusUploading)) {
            tvFileStatus.setTextColor(Color.BLACK);
            tvFileStatus.setText(statusUploading);
            tvFileStatus.setBackgroundResource(R.drawable.fileview3);
        }



        tv.setText(files[position]);
        tvFileSize.setText("Size:" + file_sizes[position]);
        tv_date.setText(file_dates[position]);

    }
    else
    {
        v=convertView;
    }
    return v;
}

1 个答案:

答案 0 :(得分:0)

在实施ViewHolder时始终使用ListView。请参阅this tutorial