2017-03-08 14 views
0

私はListViewを持っていますので、リストアイテムを押したときにリストアイテムの背景色が変わり、リストのフォーカスがアイテムの中央になります。すべてが期待通りに機能し、問題はありません。複数のアイテムがAndroid ListViewでスクロールされて選択されます

ただし、リストをスクロールすると、背景が変更され、別のリスト項目が検索され、別のリスト項目が巡回的に検索されます。私が理解していることから、ListViewのアイテムはリサイクルされており、これがこれが起こっている理由です。

たとえば、私のリストには、スクロールする前に画面に表示できる12の項目が含まれています。最初の項目を選択すると、実際のデバイス上のリストをスクロールすると、15番目の要素も背景が変更されます。

このような追加の「選択されたアイテム」の繰り返しパターンではなく、背景が変更されたアイテムを1つだけ持つなど、この問題を解決する方法を知っている人はいますか?ここで

は、そのためのコードです:

@Override 
     public void onItemClick(final AdapterView<?> parent, final View view, final int position, long id) { 
      //this deselects the previously selected item (changes its background to the default one) 
      if(listItemPosition!=null) { 
       previouslySelected = vehicleListArrayAdapter.getView(listItemPosition,previousView,parent); 
       previouslySelected.setBackgroundColor(getResources().getColor(R.color.applicationBackground)); 
      } 
      //verify if the pressed item has been set as itemPosition and if not, set it 
      if (listItemPosition == null || listItemPosition != position){ 
       listItemPosition = position; 
       previousView = vehicleListArrayAdapter.getView(position,view,parent); 
      } 
      //set the selection to the listItemPosition (if we subtract 5, we set the focus on the center of the ListView) 
      realTimeVehicleListView.setSelection(listItemPosition-5); 
      //focus the list on that item 
      realTimeVehicleListView.requestFocus(); 
      //change the background color of the selected item to emphasize it 
      currentlySelectedListItem = vehicleListArrayAdapter.getView(listItemPosition,view,parent); 
      currentlySelectedListItem.setBackgroundColor(getResources().getColor(R.color.selectedItem)); 

は、ここでフラグを使用しようとしているいくつかのコードです:

@Override 
     public void onItemClick(final AdapterView<?> parent, final View view, final int position, long id) { 
      vehicleListItemSelected = true; 
      //this deselects the previously selected item (changes its background to the default one) 
      if(listItemPosition!=null) { 
       previouslySelected = vehicleListArrayAdapter.getView(listItemPosition,previousView,parent); 
       previouslySelected.setBackgroundColor(getResources().getColor(R.color.applicationBackground)); 
      } 
      //verify if the pressed item has been set as itemPosition and if not, set it 
      if (listItemPosition == null || listItemPosition != position){ 
       listItemPosition = position; 
       previousView = vehicleListArrayAdapter.getView(position,view,parent); 
      } 
      //set the selection to the listItemPosition (if we subtract 5, we set the focus on the center of the ListView) 
      realTimeVehicleListView.setSelection(listItemPosition-5); 
      //focus the list on that item 
      realTimeVehicleListView.requestFocus(); 
      //change the background color of the selected item to emphasize it 
      if (vehicleListItemSelected){ 
       currentlySelectedListItem = vehicleListArrayAdapter.getView(listItemPosition,view,parent); 
       currentlySelectedListItem.setBackgroundColor(getResources().getColor(R.color.selectedItem)); 
       vehicleListArrayAdapter.notifyDataSetChanged(); 
       vehicleListItemSelected = false; 
      } 

EDIT:それは私が当初考えていたよりもさらに悪いことです。最初の項目を選択するとその背景が変更され、リストをランダムにスクロールして最初の項目に戻ると、選択されなくなります。代わりに、他のアイテムが選択されています.2番目または3番目のアイテムです。実際に選択した最初のアイテムではなく、背景が変更されています。

答えて

0

アダプタが選択されているかどうかを示す変数を渡します。 開始時に各要素に対してfalseを選択し、ユーザーがリスト内の項目をクリックしたとき。アダプタで使用されているリスト内のその位置のブール値をtrueに設定します。これはリサイクルを防止します。

項目を選択し、あなたが本当の変数を渡すされるたびに、アダプター

+0

まあ上notifyDatastChangedを使用することを覚えて、私はそれを実行しようとしましたが、それは動作しません。私のコードは次のとおりです: 'if(vehicleListItemSelected){ currentlySelectedListItem = vehicleListArrayAdapter.getView(listItemPosition、view、parent); currentlySelectedListItem.setBackgroundColor(getResources()。getColor(R.color.selectedItem)); vehicleListArrayAdapter.notifyDataSetChanged(); vehicleListItemSelected = false; } ' 返信の文字数制限のために投稿できませんが、動作しません。 –

+0

VIEWHOLDER @RobertRuxandrescu –

+0

私の編集したオリジナルのメッセージをチェックしてください。 –

関連する問題