列表视图中的单击项不起作用

时间:2016-09-20 04:22:25

标签: android listview android-studio android-adapter onitemclicklistener

当我点击任何列表项时没有任何反应,有人可以帮忙或告诉我问题出在哪里......?

以下是我的Listview:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".defineBybeldbAlles">


    <ListView
        android:id="@+id/BybelHoofstukListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#ff303030"
        android:dividerHeight="1dp"
        android:layout_marginTop="5dp"
        android:choiceMode="singleChoice" />
</RelativeLayout>

以下是我的Listview项目:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:descendantFocusability="blocksDescendants"
    android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/customButtonLayout"
            android:layout_height="50dp"
            style="@android:style/Widget.Button"
            android:focusable="false"
            android:layout_width="200dp">

            <! hoofstuk_id is hidden and is only for reference >
            <TextView
                android:text="id"
                android:id="@+id/hoofstuk_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"
                >
            </TextView>

            <TextView
                android:textColor="#000"
                android:text="nommer"
                android:layout_height="wrap_content"
                android:id="@+id/custom_row_hoofstuktext"
                android:layout_width="wrap_content"
                android:layout_marginLeft="10dp">
            </TextView>

        </LinearLayout>

    </LinearLayout>

以下是活动中的OnItemclicklistener:

 listviewHoofstuk.setOnItemClickListener(new AdapterView.OnItemClickListener(){

@Override
public void onItemClick (AdapterView<?> arg0, View view, int arg2, long arg3){

//on selecting a hoofstk
//BybelActivityVers will be launched to show verse inside

Intent hoofstukIntent = new Intent(BybelActivityHoofstuk.this,BybelActivityVers.class);

//send hoofstuk_id to VersActivity to get verse under that book

String hoofstuk_id_na_vers =((TextView)view.findViewById(R.id.hoofstuk_id)).getText().toString();
hoofstukIntent.putExtra("hoofstuk_id", hoofstuk_id_na_vers);

startActivity(hoofstukIntent);
}
});

我看过其他一些解决方案,但在我的情况下没有任何效果,我添加了这段代码,但是当我使用它时没有任何区别:

android:focusable="false"

android:focusableInTouchMode="false"

android:descendantFocusability="blocksDescendants"

4 个答案:

答案 0 :(得分:1)

Yout列表项目正在关注焦点。设置在 my Listview Item xml 文件 android:clickable="false" 文件中的 LinearLayout 标记下方的行

with
     saxtion_eg ( str_rate ) as (
       select   1.11317    from dual union all
       select 123.08546759 from dual union all
       select   8.49111    from dual union all
       select 582          from dual union all
       select   0.00000001 from dual
     ),
     prep ( str_rate, char_rate, pos ) as (
       select str_rate, to_char(str_rate), instr(to_char(str_rate), '.')
       from   saxtion_eg
     ),
     final ( char_rate, dec_digits, max_dec_digits ) as (
       select char_rate,
              case pos when 0 then 0 else length(char_rate) - pos end,
              max(case pos when 0 then 0 else length(char_rate) - pos end) over ()
       from   prep
     )
select char_rate as str_rate
from   final
where  dec_digits = max_dec_digits
;

STR_RATE                              
----------------
123.08546759                            
.00000001 

你的意图会在没有注意力的情况下开火并正常工作我已经检查过了......让我知道一旦它为你工作......

答案 1 :(得分:0)

使用AdapterView获取adpater项目视图,您应该使用 parent 来查找TextView

listviewHoofstuk.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //on selecting a hoofstk
                //BybelActivityVers will be launched to show verse inside

                Intent hoofstukIntent = new Intent(BybelActivityHoofstuk.this,BybelActivityVers.class);

                //send hoofstuk_id to VersActivity to get verse under that book
                String hoofstuk_id_na_vers =((TextView)parent.findViewById(R.id.hoofstuk_id)).getText().toString();
                hoofstukIntent.putExtra("hoofstuk_id", hoofstuk_id_na_vers);
                startActivity(hoofstukIntent);
            }
        });

答案 2 :(得分:0)

以下是您的解决方案:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".defineBybeldbAlles">

    <ListView
        android:id="@+id/BybelHoofstukListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#ff303030"
        android:dividerHeight="1dp"
        android:layout_marginTop="5dp"/>
</RelativeLayout>

在listview项目中,您必须进行此更改:

<LinearLayout
        android:id="@+id/customButtonLayout"
        android:layout_height="50dp"
        style="@android:style/Widget.Button"
        android:layout_width="200dp">

        <! hoofstuk_id is hidden and is only for reference >
        <TextView
            android:text="id"
            android:id="@+id/hoofstuk_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            >
        </TextView>

        <TextView
            android:textColor="#000"
            android:text="nommer"
            android:layout_height="wrap_content"
            android:id="@+id/custom_row_hoofstuktext"
            android:layout_width="wrap_content"
            android:layout_marginLeft="10dp">
        </TextView>

    </LinearLayout>

</LinearLayout>

如果不只是对此答案发表评论,这应该可以解决您的问题。之后,您将开始获取listview的OnItemClick()事件。

答案 3 :(得分:0)

我删除了

android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"

然后应用

android:clickable="false"

它没有用,但在摆弄后我发现了

android:clickable="false"
android:focusable="false"

组合完成了诀窍!

这已在 LinearLayout 之后的列表视图项中应用。

我测试过并且可以确认它也适用于Gridview。