【问题标题】:Problems with scroll in listview列表视图中的滚动问题
【发布时间】:2013-07-22 20:22:03
【问题描述】:

我有一个自定义适配器,可以用名称和数字填充ListView。如果该数字具有特定的价值,则该数字具有不同的颜色。 当它显示时它工作正常,但是当我滚动列表时,其他不具备条件的数字也会获得颜色。这是自定义适配器和getView() 方法。

private class MyCustomAdapter extends ArrayAdapter<CategoriaD> {

    private ArrayList<CategoriaD> elementList;

    public MyCustomAdapter(Context context, int textViewResourceId,
                           ArrayList<CategoriaD> elementList) {
        super(context, textViewResourceId, elementList);
        this.elementList = new ArrayList<CategoriaD>();
        this.elementList.addAll(elementList);
    }

    private class ViewHolder {
        TextView texto;
        TextView cantidad;
    }

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

        ViewHolder holder = null;
        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.activity_inspeccion_categoria, null);

            holder = new ViewHolder();
            holder.texto = (TextView) convertView.findViewById(R.id.texto);
            holder.cantidad = (TextView) convertView.findViewById(R.id.estadistica);

            convertView.setTag(holder);

        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        CategoriaD Elemento = elementList.get(position);
        holder.texto.setText(Elemento.getNombre());
        holder.cantidad.setText(Elemento.getEstadistica());
        if(Elemento.getEstadistica().equals(“50”)){
            holder.cantidad.setTextColor(Color.RED);
        }

        holder.cantidad.setTag(Elemento);

        return convertView;

    }

}

【问题讨论】:

    标签: android listview scroll


    【解决方案1】:

    你的问题在这里:

        if(Elemento.getEstadistica().equals(“50”)){
            holder.cantidad.setTextColor(Color.RED);
        }
    

    你需要确保你有一个 else 语句来将你的颜色设置回你的默认值,因为当使用转换视图时,当发生更改时它会持续存在,因为视图不会被重新充气。

    if(Elemento.getEstadistica().equals(“50”)){
        holder.cantidad.setTextColor(Color.RED);
    } else {
        //TODO change to your default color color
        holder.cantidad.setTextColor(Color.BLACK);
    }
    

    编辑:此外,您的更改很小,因此这比膨胀 2 个单独的 convertViews 更好,但是如果您对每个项目的 convertView 或更多项目进行了更显着的更改,那么您可以覆盖 getItemViewType(int) 方法并且将允许您为每种类型的项目膨胀不同的转换视图,并在请求正确的项目类型时自动重用它。如果您用于更改文本颜色的其他情况,您只需将其输出和整数依赖于相同的情况。 (可能分别是 0 和 1。)

    Adapter getItemViewType(int)

    【讨论】:

      【解决方案2】:

      在自定义适配器的情况下,我发现覆盖所有基础是要走的路。 if-block 应该有一个补充 else-block,即使它看起来是多余的。

      尝试使用以下getView() 方法代替您的方法:

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
      
          ViewHolder holder = null;
          Log.v("ConvertView", String.valueOf(position));
      
          if (convertView == null) {
              LayoutInflater vi = (LayoutInflater)getSystemService(
                      Context.LAYOUT_INFLATER_SERVICE);
              convertView = vi.inflate(R.layout.activity_inspeccion_categoria, null);
      
              holder = new ViewHolder();
              holder.texto = (TextView) convertView.findViewById(R.id.texto);
              holder.cantidad = (TextView) convertView.findViewById(R.id.estadistica);
      
              convertView.setTag(holder);
      
          }
          else {
              holder = (ViewHolder) convertView.getTag();
          }
      
          CategoriaD Elemento = elementList.get(position);
          holder.texto.setText(Elemento.getNombre());
          holder.cantidad.setText(Elemento.getEstadistica());
          if(Elemento.getEstadistica().equals(“50”)){
              holder.cantidad.setTextColor(Color.RED);
          } else {
              holder.cantidad.setTextColor(Color.GREEN);    // Change this to whatever Color
          }
      
          holder.cantidad.setTag(Elemento);
      
          return convertView;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-26
        • 1970-01-01
        相关资源
        最近更新 更多