【问题标题】:Android: OnItemClickListener does not workAndroid:OnItemClickListener 不起作用
【发布时间】:2015-09-05 12:46:09
【问题描述】:

我实现了我的自定义列表,但是当我单击列表中的一个元素时,应该会显示 Toast 消息。看起来setOnItemClickListener 不起作用。 这是我的片段布局的代码,其中有我的 ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

        <ListView
            android:id="@+id/listSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:footerDividersEnabled="false"
            android:headerDividersEnabled="false"
            android:paddingStart="15dp"
            android:paddingEnd="15dp"
            android:focusable="false"/>

</LinearLayout>

这是我的列表的结构(每一行)

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
 >


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

        <ImageView
            android:id="@+id/imageContact"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:padding="5dp"
            android:focusable="false"/>

            <TextView
                android:id="@+id/nameSurnameContact"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:padding="2dp"
                android:textColor="#33CC33"
                android:focusable="false"/>

            <TextView
                android:id="@+id/idContact"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:visibility="invisible"
                android:focusable="false"
                />
    </LinearLayout>
</LinearLayout>

这里有我的片段的方法,它创建布局并填充列表

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {

    View firstAccessView;
    if(savedBundle==null) {
        firstAccessView = inflater.inflate(R.layout.search_fragment_layout, null);


        for(int i=0; i<8; i++){
            Contact c = new Contact(idContact[i], nameSurname[i], facebookId[i], timeStamp[i]);
            this.rows.add(c);

        }

        adapter = new SearchListAdapter(getActivity(), this.rows);
        list = (ListView) firstAccessView.findViewById(R.id.listSearch);
        list.setAdapter(adapter);


        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(getActivity().getApplicationContext(), "item selected", Toast.LENGTH_SHORT).show();

            }
        });
        list.setOnScrollListener(this);

    }else{
        firstAccessView = getView();
    }
    return firstAccessView;
}

xml 文件中的 Fragment 文件中是否有我遗漏的内容?

【问题讨论】:

  • 尝试将android:descendantFocusability="blocksDescendants" 添加到ListView 的父布局中。
  • 这种方式也行不通

标签: android android-fragments onitemclicklistener


【解决方案1】:

ListView 中删除android:focusable="false"

【讨论】:

  • @FF91,在第一个 LinearLayout 标签的列表结构中添加 android:descendantFocusability="blocksDescendants"
  • 抱歉,我应该在哪里添加这一行?
【解决方案2】:

android:focusable="false" 从您的 listview xml 文件中删除此行

【讨论】:

    猜你喜欢
    • 2015-08-05
    • 2012-03-11
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多