java - 每次当我滚动随机项目时更改项目背景颜色时,项目也会更改背景颜色

标签 java android listview

每次当我滚动随机项目时更改Item背景颜色时,背景颜色也会更改,我不知道为什么。 发生的另一件事是,如果该项目未显示在屏幕中,它不会更改背景颜色。

这是我的代码:

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);
    }
}
}

最佳答案

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

您需要设置 getView() 以便您的 View 可以被回收。

关于java - 每次当我滚动随机项目时更改项目背景颜色时,项目也会更改背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26131832/

相关文章:

java - 如何使用 spring 的 jdbcTemplate 在 SQL 查询中指定参数

java - 为什么 GSON 在我所有原本不包含任何小数的 JSON 数字之后都加上一个小数点和一个零?

java - 操作项图标未显示在操作栏中

android - 具有两个不同列表项的 ListView

java - AWS S3 以小于 5MB 的 block 部分上传文件

java - Flash 属性不适用于 https - 适用于 http

java - 将参数传递给 AIDL

android - 应用程序在调试时卡住

android - 从 ListView 调用不同的 Activity ?

c# - 在 C# Windows 窗体中将组和项目添加到 ListView