2017-06-11 10 views
0

ListViewのOnItemClickListenerが必ず呼び出されるとは限りません。 ListViewは埋め込みTextViewを使用します。ListView OnItemClickListenerが常に起動しているとは限りません

各アイテムの上部または下部の3ピクセルをクリックすると、リスナーは発砲します。

TextViewのテキストをクリックすると、リスナーはクリックを消費/ブロックしているかのように起動しません。

focusable="false",clickable="false"およびfocusableInTouchMode="false"をTextViewに追加し、android:descendantFocusability="beforeDescendants"をルートビューに追加するという多くの組み合わせを試しました。私はこの単純な活動に時間を費やしてきたよう

activity_local_explorer.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:descendantFocusability="beforeDescendants" 
tools:context="my.package.FileBrowser"> 

<ListView 
    android:id="@+id/local_file_view" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_marginBottom="8dp" 
    android:layout_marginEnd="8dp" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginTop="8dp" 
    android:clickable="true" /> 

<TextView 
    android:id="@+id/local_file_view_text" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:clickable="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:textSize="25sp"/> 

</android.support.constraint.ConstraintLayout> 

活動

public class LocalExplorer extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_local_explorer); 

    String[] values = { "Hello", "My", "Friend"}; 

    ListView listView = (ListView) findViewById(R.id.local_file_view); 
    listView.setAdapter(new ArrayAdapter<>(this, R.layout.activity_local_explorer, R.id.local_file_view_text, values)); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 

      String selectedValue = getItemAtPosition(position); 
      Snackbar.make(listView, selectedValue, 2).show(); 
     }  
    }); 
} 

任意の助けもいただければ幸いです!

+0

私は明らかに間違って何も表示されません。 ListViewから 'android:clickable'を削除してみてください。ユーザーがビュー全体ではなく個々の行をクリックできるようにします。 –

+0

TextViewの目的は何ですか? –

+0

layout.xmlは本当にactivity_local_explorer.xmlだと思います。あれは正しいですか? –

答えて

0

最初に私は私の質問に答えるのを手伝ってくれた人に感謝したいと思います。あなたの時間は非常に感謝していました。

OKアプローチを少し変更することで、これはやっと最後に機能しました。 activity_local_explorer.xmlのTextViewを独自のxmlレイアウトファイルに移動し、アダプタを作成するときに別のコンストラクタを使用しました。

activity_local_explorer.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:descendantFocusability="blocksDescendants" 
tools:context="my.package.FileBrowser"> 

    <ListView 
     android:id="@+id/local_file_view" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginBottom="8dp" 
     android:layout_marginEnd="8dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     tools:layout_constraintBottom_creator="1" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintTop_creator="1" /> 

</android.support.constraint.ConstraintLayout> 

local_explorer_row.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rowTextView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:textSize="16sp" > 
</TextView> 

活動

public class LocalExplorer extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_local_explorer); 

    String[] values = { "Hello", "My", "Friend"}; 

    ListView listView = (ListView) findViewById(R.id.local_file_view); 
    listView.setAdapter(new ArrayAdapter<>(this, R.layout.local_explorer_row, values)); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 

      String selectedValue = getItemAtPosition(position); 
      Snackbar.make(listView, selectedValue, 2).show(); 
     }  
    }); 
} 
関連する問題