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 に答える