【问题标题】:ListView dynamic datarow changing on scrollingListView 动态数据行在滚动时发生变化
【发布时间】:2015-11-02 09:13:37
【问题描述】:

我想将文本视图的文本和背景颜色从“未上传”更改为“正在上传...”,并且还想更改背景颜色。但是当我单击上传按钮时,它会更改所选视图的颜色和文本,但它也会对最后一个可见项目+1 进行相同的更改。当我滚动列表时,更改效果也会继续在其他视图上移动。

这是我的自定义适配器 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;
}

【问题讨论】:

  • 当你只有一个 if 和 else 条件时不要使用 else if。改用“其他”。尝试删除 else if 并插入 else。可能会解决您的问题。
  • @DroidAks 如果他只需要条件(“txt”)怎么办?还是其中的任何一个?
  • 你应该使用 ViewHolder
  • 列表视图的问题在于,如果您只输入“if”条件,那么一旦您滚动列表视图,它就会为其他视图应用颜色更改。需要有“其他”部分,这样它就不会在滚动后覆盖其他视图的颜色。
  • @DroidAks 你是对的......我已经改变了条件,现在它没有将更改转移到其他元素..请建议我在运行时临时更改列表项的最佳方法是什么这应该将消息更改为 NotUploaded=>Uploading....=> Uploaded

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


【解决方案1】:

在实现ListView 时始终使用ViewHolder。参考this tutorial

【讨论】:

  • 谢谢兄弟...让我试试这个教程,如果它对我有用,我会接受你的回答:)
  • 我在我的自定义适配器上使用了 viewHolder + notifydatasetchanged() 来获得我需要的效果.. 谢谢 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多