1

基本上,我的问题与此类似:

滚动 ListView 将所有内容从白色更改为黑色

唯一的区别是我正在处理一个在滚动时改变颜色的 TextView(TextView 在 ListView 内)。

我查找了 TextView 是否有类似于 setCacheColorHint(Color.WHITE) 的方法 - 我没有找到。

也许我应该动态设置默认的TextColor?因为目前,它是在 XML 中设置的,然后在代码中进行了更改。

我该如何处理?

将颜色更改为蓝色的代码:

private void highlightSelectedFile(View vw)
{
    TextView fileName = (TextView) vw.findViewById(R.id.file_name);

    //Log.v("color: ", Integer.toString(fileName.getCurrentTextColor()));       

    if(fileName.getCurrentTextColor() == Color.BLACK) {
        fileName.setTextColor(Color.BLUE);

    } else {
        fileName.setTextColor(Color.BLACK);
        removeFromSelectedFiles(new File(fileName.getText().toString()));
    }
}

在我滚动它们所在的 ListView 后,这些 TextView 会变回黑色:

    ListView lv = (ListView) ac.findViewById(android.R.id.list);
4

2 回答 2

0

在您的适配器中,您可能正在使用将数据应用于您的视图的数据项Adapter。当您滚动时,您View的 s 将被回收,并getView()使用不同的View对象调用您的。您的问题很容易解决。只需int color;在您的数据项中再添加一个变量并像这样使用它。

public View getView(int position, View convertView, ViewGroup parent) {
    // efficient stuff here

    // after applying your data
    fileName.setTextColor(data.color);
}

也改变这个方法:

private void highlightSelectedFile(DataItem data) { 
    if(data.color == Color.BLACK) {
        data.color = Color.BLUE; 
    } else {
        data.color = Color.BLACK;
        removeFromSelectedFiles(new File(fileName.getText().toString()));
    }
   // to update ListView
    myAdapter.notifyDataSetChanged();
}
于 2013-02-18T11:36:34.147 回答
0

仅将此语句放在 XML 文件的列表中。android:scrollingCache="false" 它会解决你的问题。

于 2013-02-18T12:06:41.073 回答