【问题标题】:OnItemClickListener Not working in fragment (Multicolumn ListView)OnItemClickListener 在片段中不起作用(多列 ListView)
【发布时间】:2014-01-20 23:52:35
【问题描述】:

我需要你的帮助!

我有一个包含 2 个片段的主要活动(一个用于标题,一个用于多列列表),其中一个片段是 ListFragments 包含一个多列 ListView,它被装箱在可滚动视图中。此 ListFragment 覆盖 onItemClickListener 方法。我可以滚动视图和所有内容,但是当我单击列表视图时,不会调用侦听器!

我尝试了许多不同的解决方案,例如尝试阻止视图的可聚焦性,但到目前为止没有任何效果。也许你们可以帮帮我。

包含两个片段的父Activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1"
    android:descendantFocusability="blocksDescendants"
    tools:context=".MainWindow" 
     >

    <fragment
        android:id="@+id/headerfragment"
        android:name="lindcom.mobile.estock.fragments.ListHeaderFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
         />

    <fragment
        android:id="@+id/unitlistfragment"
        android:name="lindcom.mobile.estock.fragments.UnitsListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants"
         />

</LinearLayout>

这是我用于多列列表的布局:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1" >

        <TextView
            android:id="@+id/unitsCol1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.40"
            android:ellipsize="marquee"
            android:maxLines="1"
            android:scrollHorizontally="true"
            android:singleLine="true"
            />

        <TextView
            android:id="@+id/unitsCol2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.40"
            android:ellipsize="marquee"
            android:maxLines="1"
            android:scrollHorizontally="true"
            android:singleLine="true" 
            />


        <TextView
            android:id="@+id/unitsCol3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:ellipsize="marquee"
            android:maxLines="1"
            android:scrollHorizontally="true"
            android:singleLine="true" 
            />
    </LinearLayout>

</ScrollView>

在我获取数据后,上面的布局被填充到一个异步任务中:

ListAdapter adapter = new SimpleAdapter(
                    fragment.getActivity().getBaseContext(), this.unitList,
                    R.layout.unit_list_item, new String[] { TAG_OWNER, TAG_NAME,
                            TAG_CONTENT }, new int[] { R.id.unitsCol1, R.id.unitsCol2,
                            R.id.unitsCol3 });


            fragment.setListAdapter(adapter); // Set the adapter to the listFragment

然后对于点击监听器,我只是在片段中覆盖它:

public class UnitsListFragment extends ListFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Fetch stuff here

    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        String type = l.getItemAtPosition(position).getClass().toString();

        Log.d("Item type was", type);
            // THIS IS NEVER INVOKED :(


    };
}

我尝试了很多不同的方法,但也许你们可以看到一个简单的解决方法,这是我可能忽略的。

任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 你说你在一个ScrollView中有一个ListView?是这样吗?如果是这样,那很少是愉快的合作关系,这可能是原因。

标签: android android-listview onclicklistener android-listfragment


【解决方案1】:

在布局中删除您的 scollview 或将其定义为不可聚焦然后重试

【讨论】:

  • 哇,非常感谢!真的就是这么简单。我不明白这是怎么回事,因为我在 OP 中看到的 scrollView 中定义了 android:focusable="false" android:focusableInTouchMode="false" android:descendantFocusability="blocksDescendants" 。我简直不敢相信它是如此简单,事实证明,无论如何我都可以不用滚动视图,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多