每当我滚动randoms时我更改项目背景颜色项目也会更改背景颜色

时间:2014-09-30 23:25:02

标签: java android listview

每当我滚动randoms项目时我改变项目背景颜色也会改变背景颜色,我不知道为什么 另一件事是,如果项目没有显示在屏幕上,它不会改变bg颜色

这是我的代码:

public class MyActivity extends ListActivity {

char[] items = { 'e', 'f', 'f', 'f', 'e', 'f', 'e', 'e', 'f', 'f', 'e', 'e', 'f', 'f', 'f', 'e', 'e', 'f', 'f', 'e', 'e', 'f', 'f', 'f', 'e', 'e', 'f', 'f', 'e', 'e', 'f', 'f', 'f', 'e', 'e', 'f', 'f', 'p', 'e', 'e', 'p', 'f', 'f', 'f', 'e', 'e', 'a', 'f', 'p', 'f', 'e', 'e', 'f', 'f', 'p', 'f', 'e', 'a', 'e', 'p', 'f', 'f', 'e', 'p', 'e', 'f', 'f', 'f', 'a', 'p', 'e', 'e', 'p', 'f', 'f', 'e', 'p', 'e', 'f', 'p', 'f', 'e', 'f', 'e', 'f' };
int[] horas =   {00, 01, 03, 04, 04, 05, 05, 05, 06, 06, 06, 06, 07, 07, 07, 07, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21, 22, 22, 23, 23,};
int[] minutos = {20, 00, 00, 00, 30, 10, 25, 40, 10, 20, 40, 50, 00, 10, 30, 50, 00, 20, 40, 00, 10, 20, 30, 50, 10, 20, 40, 00, 20, 30, 40, 50, 10, 30, 40, 10, 20, 30, 40, 50, 00, 00, 10, 30, 50, 00, 10, 20, 30, 40, 00, 10, 30, 50, 50, 00, 10, 20, 20, 30, 40, 00, 20, 30, 30, 50, 10, 20, 20, 30, 30, 40, 50, 00, 20, 40, 50, 50, 10, 15, 30, 00, 40, 10, 50};
String[] total = new String[horas.length];

// declare class variables
private ArrayList<Item> m_parts = new ArrayList<Item>();
private Runnable viewParts;
private ItemAdapter m_adapter;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    for (int i = 0; i < items.length; i++) {
        total[i] = String.format("%02d",horas[i]) + ":" + String.format("%02d",minutos[i]);
    }

    int cuantos_f = cuantosF('f');

    // instantiate our ItemAdapter class
    m_adapter = new ItemAdapter(this, R.layout.row, m_parts);

    for (int i = 0; i < items.length; i++) {
        m_parts.add(new Item(horas[i], minutos[i], items[i], total[i]));
    }

    m_adapter = new ItemAdapter(MyActivity.this, R.layout.row, m_parts);

    // display the list.
    setListAdapter(m_adapter);

    final Button prox = (Button) findViewById(R.id.prox);
    prox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mostrarSiguiente();
        }
    });

    final Handler mHandler = new Handler();


    new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(1000);
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            // Write your code here to update the UI.
                            m_adapter.notifyDataSetChanged();
                        }
                    });
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }
    }).start();

}

private int cuantosF(char mi_char) {
    int cant = 0;
    for (int i = 0; i < horas.length; i++) {
        if(items[i] == mi_char){
            cant++;
        }
    }
    return cant;
}

private void mostrarSiguiente() {
    Calendar rightNow = Calendar.getInstance();
    int hour = rightNow.get(Calendar.HOUR_OF_DAY);
    int min = rightNow.get(Calendar.MINUTE);

    int mejor_pos = 999, menor_resta = 1500;

    for (int i = 0; i < horas.length; i++) {
        if((horas[i]*60 + minutos[i] - (hour*60+min)) > 0){
            if ((horas[i]*60 + minutos[i] - (hour*60+min)) < menor_resta){
                mejor_pos = i;
                menor_resta = (horas[i]*60 + minutos[i] - (hour*60+min));
            }
        }
    }

    ChangeColors(mejor_pos);

}

void ChangeColors(final int numero){
    getListView().smoothScrollToPosition(numero);

    ListView lv = getListView();
    lv.setCacheColorHint(0);
    final View view = getViewByPosition(numero, lv);

    changeColor(numero,view);


}
void changeColor (int numero, View v){
    v.setBackgroundColor(Color.parseColor("#3a536b"));
}
public View getViewByPosition(int position, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (position < firstListItemPosition || position > lastListItemPosition ) {
        return listView.getAdapter().getView(position, null, listView);
    } else {
        final int childIndex = position - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}
}

先谢谢!

1 个答案:

答案 0 :(得分:0)

在这个答案中查看getView()方法:https://stackoverflow.com/a/25457446/617044

您需要设置getView(),以便可以回收您的视图。