5

私はカスタムリストアダプターでListViewを持っています。これは、正常に動作していたOnItemClickListenerとOnItemLongClickListnerを持っています。その後、リストアイテムのレイアウトにボタンを置く必要があり、アイテムのクリックやロングクリックリスナーの動作が停止しました。ボタンがリストにあるときにListViewの長いクリックが機能しない

ListView lv=(ListView)findViewbyId(R.id.listview); 
lv.setAdapter(listviewadapter); 
lv.setOnItemLongClickListener(new OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) { 
      // My code 
      } 
}); 

これは、リスト項目のレイアウトにボタンを追加する前に、正常に動作するために使用:ここに私のサンプルコードで、私が活動にonItemLongClickListener実装にインラインリスナーを変更しようとした

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="2dp" 
    > 
    <TextView 
     android:id="@+id/symbol_name" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="2.5" 
     android:layout_gravity="left" 
     /> 
    <TextView 
     android:id="@+id/ltp" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="1" 
     /> 
    <TextView 
     android:id="@+id/change_in_perc" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="1" 
     /> 

    <TextView 
     android:id="@+id/volume" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="1" 
     /> 

    <ImageButton 
     android:id="@+id/chart" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="0.5" 
     android:src="@drawable/charts" 
     android:contentDescription="Chart Link" 
     /> 
</LinearLayout> 

しかし、誰成功まで今。ありがとう。

+0

これがケースかもしれません。私はそれを試しています。しかし、それがフォーカスを取っている場合、私はリストビューをクリックすると、ボタンのクリックイベントが引き起こされるべきではない? – Sourabh

+2

これは..私がそれを受け入れることができるようにあなたの答えとしてこれを追加してください。 – Sourabh

答えて

13

あなたの画像ボタンは、おそらくあなたがリスト項目をクリックするとフォーカスを引き出しています。

は、だからあなたのルート要素に

http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:descendantFocusability

アンドロイドをこの

android:descendantFocusability="blocksDescendants" 

を追加します。descendantFocusability

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus. 

Must be one of the following constant values. 

Constant Value Description 
beforeDescendants 0 The ViewGroup will get focus before any of its descendants. 
afterDescendants 1 The ViewGroup will get focus only if none of its descendants want it. 
blocksDescendants 2 The ViewGroup will block its descendants from receiving focus. 
This corresponds to the global attribute resource symbol descendantFocusability. 
+1

ありがとうございます。 stackoverflowが許可するとすぐにこの回答を受け入れます。 :) – Sourabh

+0

私を助けてください。私はこの7時間、これについてきました。それはとても簡単な質問です。 http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 –

0

XMLでandroid:longClickable = "true"を設定しましたが、listview.setLongClickable(true);コード内で?

+0

私を助けてください。私はこの7時間、これについてきました。それはとても簡単な質問です。 http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 –

関連する問題