Android:OnItemClickListener不起作用

时间:2015-09-05 12:46:09

标签: android android-fragments onitemclicklistener

我实现了自定义列表,但是当我单击列表中的元素时,应显示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个文件中遗漏了哪些内容?

2 个答案:

答案 0 :(得分:0)

android:focusable="false"删除ListView

答案 1 :(得分:0)

<强>机器人:可聚焦= “假” 从listview xml文件中删除此行