1

我对 Listview 和所选项目有一个奇怪的问题。在我选择一个项目并滚动列表之前,一切正常。如果我快速滚动并且所选项目不再可见,则所选项目的背景颜色在滚动期间保持可见,即使在两行之间也是如此。这是我的列表视图 XML:

    <ListView
    android:id="@+id/my_id"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="@android:color/transparent"
    android:drawSelectorOnTop="false"
    android:listSelector="@android:color/holo_blue_light"
    android:fastScrollEnabled="true"
    android:scrollbars="vertical"
    android:scrollingCache="false" />

这是我的适配器:

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


    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.ricerca_impianti_list_adapter, null);
        icvh = new ImpiantiCercatiViewHolder();
        icvh.sigla = (TextView) convertView.findViewById(R.id.txv_impiantiSigla);
        icvh.tipo = (TextView) convertView.findViewById(R.id.txv_impianti_tipo);
        icvh.denDitta = (TextView) convertView.findViewById(R.id.txv_impianti_denominazione_ditta);
        icvh.indDitta = (TextView) convertView.findViewById(R.id.txv_impianti_indirizzo_ditta);
        icvh.dataScad = (TextView) convertView.findViewById(R.id.txv_impianti_data_scadenza);
        convertView.setTag(icvh);
    } else {
        icvh = (ImpiantiCercatiViewHolder) convertView.getTag();
    }
    Impianto i = impianti.get(position);
    if (i != null) {
        icvh.tipo.setText(VimpUtils.ritornaSiglaTipoImpianto(i.getTipoImpianto()));
        icvh.sigla.setText(i.getSigla() + "-" + i.getNumero() + "-" + i.getAnno());
        icvh.denDitta.setText(i.getDenominazioneDitta());
        icvh.indDitta.setText(i.getIndirizzoDitta() + " - " + i.getComuneDitta());
        icvh.dataScad.setText(df.format(i.getDataScadenzaVerifica()));
    }
    return convertView;
}   

static class ImpiantiCercatiVi`enter code here`ewHolder {
    TextView sigla;
    TextView tipo;
    TextView denDitta;
    TextView indDitta;
    TextView dataScad;
}

这是我的适配器 xml:

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/img_lista_impianto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:src="@drawable/ic_impianto_dark" />

<TextView
    android:id="@+id/txv_impiantiSigla"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:textAppearance="@style/bigTextBlack" />

<TextView
    android:id="@+id/txv_impianti_tipo"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textAppearance="@style/bigTextBlack" />

<TextView
    android:id="@+id/txv_impianti_data_scadenza"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:textAppearance="@style/bigText" />

<TextView
    android:id="@+id/txv_impianti_denominazione_ditta"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="4"
    android:textAppearance="@style/bigText" />

<TextView
    android:id="@+id/txv_impianti_indirizzo_ditta"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:gravity="right"
    android:textAppearance="@style/bigText" />

编辑: 这里有问题的屏幕。

4

1 回答 1

0
View selectedview;
int selectedposition
public View getView(int position, View convertView, ViewGroup parent) {
    ImpiantiCercatiViewHolder icvh;



        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.ricerca_impianti_list_adapter, null);
        icvh = new ImpiantiCercatiViewHolder();
        icvh.sigla = (TextView) convertView.findViewById(R.id.txv_impiantiSigla);
        icvh.tipo = (TextView) convertView.findViewById(R.id.txv_impianti_tipo);
        icvh.denDitta = (TextView) convertView.findViewById(R.id.txv_impianti_denominazione_ditta);
        icvh.indDitta = (TextView) convertView.findViewById(R.id.txv_impianti_indirizzo_ditta);
        icvh.dataScad = (TextView) convertView.findViewById(R.id.txv_impianti_data_scadenza);
        convertView.setTag(icvh);

    Impianto i = impianti.get(position);
    if (i != null) {
        icvh.tipo.setText(VimpUtils.ritornaSiglaTipoImpianto(i.getTipoImpianto()));
        icvh.sigla.setText(i.getSigla() + "-" + i.getNumero() + "-" + i.getAnno());
        icvh.denDitta.setText(i.getDenominazioneDitta());
        icvh.indDitta.setText(i.getIndirizzoDitta() + " - " + i.getComuneDitta());
        icvh.dataScad.setText(df.format(i.getDataScadenzaVerifica()));
    }
    convertView.setBackgroundColor(Color.WHITE);
                if(selectedposition == position){
                    convertView.setBackgroundColor(Color.LTGRAY);
                    selectedview = convertView;
                }

    convertView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
       if (selectedview != null && selectedview != view && selectedposition != position)
     {

    selectedview.setBackgroundColor(Color.WHITE);
            }

     selectedview = view;

    selectedposition = position;

    selectedview.setBackgroundColor(color); //highlighted color

        }
    });

     return convertView;
    }   
于 2016-02-10T12:55:10.627 回答