1

我正在尝试更改列表视图中所选行的背景颜色,并且我能够做到。但是当我点击另一行时,之前选择的行的背景颜色保持不变。我有先前选择的行的位置,谁能帮我改变以前选择的行的背景颜色?

4

3 回答 3

1

I think this might be easier if you look at it another way.

Currently, your logic is "if I click on this row, change its color to a special color and change the old row's color back to the original color". However, this doesn't seem to be the logic you actually want. Rather, you want the last clicked (aka selected) row to be a different color.

You haven't posted any code, so I don't know if you are implementing your own ListAdapter in this project. That is the approach I would take. Create a class which extends ListAdapter, and create an additional private variable that stores the position of the last selected row. Then in the overriden getView() method, do a quick check

if(rowPosition == lastSelectedRowPosition) 
   viewToReturn.setBackgroundColor();

If you aren't sure how to make your own list adapter, check out the tutorial at http://jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx.

于 2012-07-24T03:37:39.703 回答
1

如果您跟踪并更新模型中的 listItems 单击状态,您只需将用于显示的代码放在has been clicked-color适配器中,然后调用

adapter.notifyDatasetChanged();

于 2012-07-23T13:23:09.690 回答
0

在您的活动中使用 Your_List 对象的 setOnItemClickListener。

请参阅演示代码:

list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
                v.setBackgroundColor(Color.BLUE); // <--- Use color you like here
            //  ^ this v gives current row. 
     }
});

这将使该行的背景颜色永远改变。

于 2012-07-23T13:20:38.817 回答