ListViewItem无法在Fragment

时间:2017-01-07 11:04:44

标签: android listview android-fragments

我知道这个问题有几个问题,但没有一个在我的案例中工作。我试图在片段中使用自定义适配器制作ListView,并且这些项目不可点击。这是我的OnItemClickListener:

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Log.d("Klick", "Klick");
        HeroItem heroItem = (HeroItem) heroSelect.getItemAtPosition(i);
        int id = heroItem.id;
        FragmentManager fragmentManager = getFragmentManager();
        Fragment fragment = HeroDetailFragment.newInstance(String.valueOf(id));
        fragmentManager.beginTransaction().replace(R.id.content_selecthero, fragment, null).addToBackStack(null).commit();
    }

OnItemClickListener在OnViewCreated中设置如下:

    heroSelect.setClickable(true);
    heroSelect.setAdapter(new HeroSelectAdapter(getContext(),array));
    heroSelect.setOnItemClickListener(this);

以下是项目的XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:descendantFocusability="blocksDescendants">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/itemList"
        android:descendantFocusability="blocksDescendants">

        <ImageView
            android:layout_height="60dp"
            app:srcCompat="@mipmap/ic_launcher"
            android:id="@+id/heroselect_avatar"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="14dp"
            android:layout_marginStart="14dp"
            android:scaleType="fitCenter"
            android:layout_width="60dp"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:focusable="false"/>

        <TextView
            android:text="TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/heroselect_avatar"
            android:layout_toEndOf="@+id/heroselect_avatar"
            android:layout_marginLeft="65dp"
            android:layout_marginStart="65dp"
            android:id="@+id/heroselect_name"
            android:textColor="@color/colorWhite"
            android:textSize="30sp"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:focusable="false"/>
    </RelativeLayout>
</FrameLayout>

片段之一:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.rocketfox.overwatchinfo.HeroSelectFragment"
android:background="@color/colorPrimary"
android:id="@+id/content_selecthero">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list_view_heroes" />

点击某个项目只是没有触发任何东西,经过几个小时的尝试修复它,我想要问你们所有人!在此先感谢:)

1 个答案:

答案 0 :(得分:0)

从代码中删除heroSelect.setClickable(true);