4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Android】ListFragmentでonListItemClickが呼ばれない時の対処法

Last updated at Posted at 2016-01-06

ListFragment内でonListItemClickをオーバーライドしたのに呼ばれない場合の対処法をメモっとく。

#原因
原因は、CustomAdapterのrow内のView(TextViewなど)にフォーカスが当たっているため。
TextViewにフォーカスが当たってしまう理由の一例は、android:inputType=""を指定している場合など。

#対処法
rowのViewに

.xml
android:clickable="false"
android:focusable="false"

を設定してあげればよい。
例えば、

.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_row"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:clickable="false"
        android:focusable="false" />

</LinearLayout>

みたいな感じ。
もしくはArrayAdaptergetView内で動的に設定してあげる。

.java

   @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = layoutInflater_.inflate(R.layout.list_row, null);
        }

        TextView text = (TextView)convertView.findViewById(R.id.text1);
        shopNameText.setClickable(false);
        shopNameText.setFocusable(false);

        return convertView;
    }


今回の場合は,
android:inputType="textMultiLine"

android:ellipsize="end"
に変えても解決できた.

4
4
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?