2016-09-16 37 views
0

なぜ私のItemClickイベントはトリガーされませんか?ListView ItemClickイベントがトリガーされないのはなぜですか?

_specialtyListView.ItemClick += (s, e) => _viewModel.Specialty = (string)_specialtyListView.Adapter.GetItem((int)e.Id); 

注:

  1. リストビューが移入されます。
  2. アイテムはフォーカスを受け取ります。

しかし、私はitemclickイベントをトリガーすることができません。

私はListViewコントロールのために、次の初期化コードを持っている:

_specialtyListView = FindViewById<ListView>(Resource.Id.SpecialtyListView); 
_specialtyListView.ChoiceMode = ChoiceMode.Single; 
_viewModel.LoadSpecialties(); 
_specialtyListView.Adapter = new SpecialtiesAdapter(this, new List<string>(_viewModel.Specialties)); 
_specialtyListView.ItemClick += (s, e) => _viewModel.Specialty = (string)_specialtyListView.Adapter.GetItem((int)e.Id); 

私は、次のListViewItemのレイアウトを持っている:

<ListView 
     android:id="@+id/SpecialtyListView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="5dp" 
     android:background="@android:color/transparent" 
     android:cacheColorHint="@android:color/transparent" 
     android:divider="#CCCCCC" 
     android:dividerHeight="1dp" 
     android:paddingLeft="2dp" /> 

ここではレイアウトが私のListItemのためです:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/ProviderSpecialty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="14sp" /> 
</LinearLayout> 

答えて

1

より良いメモリ操作があるので、RecyclerViewに切り替えることをお勧めします。単一のアイテムを更新するのではなく、それらをすべて更新することができます。

とにかく、ローカルで問題を再現できません。私は次のようにしました:

var listView = FindViewById<ListView>(Resource.Id.listview); 
listView.ChoiceMode = ChoiceMode.Single; 
listView.ItemClick += (s, e) => 
{ 
    System.Diagnostics.Debug.WriteLine($"Clicked {e.Position}"); 
}; 

var items = new string[] { "Havarti", "Brie", "Cheddar", "Muenster", "Swiss" }; 
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items); 
listView.Adapter = adapter; 

投稿したのと全く同じレイアウトを使いました。 ItemClickハンドラは一貫してヒットします。コードはカッコの間に包まれていない場合、ブレークポイントがトリガされませんバグ

+0

ArrayAdapterは必要なものに対して機能します。まだ私のカスタムアダプターがうまくいかなかった理由がわかりません... –

+0

カスタムアダプターを投稿できる場合は、そのアダプターをデバッグするのに役立ちます。 – Cheesebaron

+0

私の解決のために私のアップデートを確認してください。 –

0

あります:

listview.ItemClick += (s, e) => selectedItem = listview.GetItemAtPosition(e.Position).ToString(); 

ブレークポイントを観察するためには、私は次のようなコードをフォーマットする必要がありました:

listview.ItemClick += (s, e) => 
    { 
     selectedItem = listview.GetItemAtPosition(e.Position).ToString(); 
    }; 
+0

これはバグではありません。ブレークポイントは、その場合のイベントハンドラの割り当てに対して設定され、イベントハンドラのボディには設定されません。 – Cheesebaron

+0

Visual Studioでは、ハンドラ自体がブレークポイントを受け取れる行にブレークポイントを設定できます。したがって、行全体が茶色に強調表示されません。ハンドラコードは茶色で強調表示されています。それはバグではありませんか? –

関連する問題