ListView OnItemClickListener不会被触发

时间:2014-12-16 13:03:58

标签: java android android-fragments android-listview

我已经设置了一个带导航抽屉的android项目,我正在使用片段

activity_main.xml包含以下内容:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="false"/>

<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
    android:layout_gravity="start" android:name="com.waldispd.homecast.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

片段更改:

StaffelFragment newFragment = new StaffelFragment();
newFragment.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(R.id.fragment_container, newFragment).commit();

fragment_staffel.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<ListView
    android:id="@+id/staffelList"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="0dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="0dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>
</LinearLayout>

问题是,当我想从listview项目中收到项目点击时,它不会被触发。也无法在列表视图中滚动

项目点击监听器:

ListView staffelListView = (ListView) findViewById(R.id.staffelList);
staffelListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
     @Override
     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
     {
         LoadStaffel(position);
     }
 });
 staffelListView.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_listview_item, staffelTitel));

在大多数其他问题上他们说要添加'android:descendantFocusability =“blocksDescendants”',但这并没有解决问题。有人有想法吗?

3 个答案:

答案 0 :(得分:0)

而不是staffelListView.setOnItemClickListener使用staffelListView.setOnClickListener

答案 1 :(得分:0)

尝试在每个子布局中使用它

android:focusable="false"
android:focusableInTouchMode="false"

并删除你正在为此做的其他事情。也来自listview。

或 删除此

android:descendantFocusability="blocksDescendants"

如果您需要android:descendantFocusability,请尝试使用

parent.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        ((ViewGroup) v).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

答案 2 :(得分:0)

试试这个..

staffelListView.setOnItemClickListener(new OnItemClickListener()
{
     @Override
     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
     {
         LoadStaffel(position);
     }
 });
 staffelListView.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_listview_item, staffelTitel));