setOnItemClickListener()无法处理片段的listView

时间:2015-11-30 19:40:37

标签: java android android-layout listview onitemclicklistener

我的ListView元素上的句柄点击有一些问题,其中包含自定义的Fragments ArrayAdapter。在任何地方都没有复选框,只有Fragment中的Button是ImageButton,它已经有了android:focusable =" false"。在ListView的布局中,我有一些FrameLayout,下面是一些Buttons和EditText XML代码。任何解决方案,如android:focusable =" false" ,android:focusableInTouchMode =" false" ,list.setItemsCanFocus(false)。我搜索了很多,但没有任何帮助。

以下是带有Gmap和listView的listView的主要容器:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/map"
     android:name="com.google.android.gms.maps.SupportMapFragment"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     tools:context="com.example.medhigh.mapfragmet.MapsActivity" />

 <LinearLayout
     android:orientation="horizontal"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginLeft="24dp"
     android:layout_marginRight="8dp"
     android:id="@+id/searchLine">

    <FrameLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:padding="5dp">

         <EditText
             android:layout_marginLeft="16dp"
             android:layout_marginRight="42dp"
             android:singleLine="true"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:id="@+id/editText"
             android:layout_weight="1"
             android:textColor="@color/black"
             android:textSize="18sp"
             android:hint="  Start Typing Here"
             android:textColorHighlight="@color/colorPrimaryTest"
             android:textColorHint="#959595" />

         <Button
             android:id="@+id/clearEditText"
             android:layout_width="25dp"
             android:layout_height="25dp"
             android:layout_marginRight="50dp"
             android:layout_gravity="right|center_vertical"
             android:background="@drawable/close"
             android:focusable="false"/>

     </FrameLayout>
 </LinearLayout>

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:name="com.google.android.gms.maps.SupportMapFragment"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     tools:context="com.example.medhigh.mapfragmet.MapsActivity">

     <Button
         style="?android:attr/buttonStyleSmall"
         android:layout_width="48dp"
         android:layout_height="48dp"
         android:id="@+id/buttonSwitchToList"
         android:layout_alignParentBottom="true"
         android:layout_alignParentEnd="true"
         android:layout_marginBottom="98dp"
         android:layout_marginRight="7dp"
         android:background="@drawable/format_list"
         android:focusable="false"/>

     <SeekBar
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/seekBar"
         android:layout_alignParentBottom="true"
         android:layout_centerHorizontal="true"
         android:layout_marginRight="42dp"
         android:layout_marginLeft="42dp"
         android:layout_marginBottom="12dp"
         android:max="300" />

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/seekBarProgress"
         android:textColor="@color/black"
         android:layout_alignParentBottom="true"
         android:layout_centerHorizontal="true" />
 </RelativeLayout>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="0dp"
     tools:context=".search.NameFragment"
     android:background="@color/white"
     android:id="@+id/layoutList">

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

         <FrameLayout
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:padding="5dp">

             <EditText
                 android:layout_marginLeft="16dp"
                 android:layout_marginRight="42dp"
                 android:singleLine="true"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:id="@+id/editTextList"
                 android:layout_weight="1"
                 android:textColor="@color/black"
                 android:textSize="18sp"
                 android:hint="  Start Typing Here"
                 android:textColorHighlight="@color/colorPrimaryTest"
                 android:textColorHint="#959595" />

             <Button
                 android:id="@+id/clearEditTextList"
                 android:layout_width="25dp"
                 android:layout_height="25dp"
                 android:layout_marginRight="10dp"
                 android:background="@drawable/close"
                 android:layout_gravity="end|center_vertical"
                 android:focusable="false"/>

         </FrameLayout>
     </LinearLayout>

     <FrameLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content">

         <ListView
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:id="@+id/listView"
             android:clickable="true" />

         <Button
             android:id="@+id/buttonBackToMap"
             android:layout_width="48dp"
             android:layout_height="48dp"
             android:layout_marginRight="12dp"
             android:layout_marginBottom="24dp"
             android:background="@drawable/map"
             android:layout_gravity="end|bottom"
             android:focusable="false"/>
     </FrameLayout>
 </LinearLayout> </FrameLayout>

这是列表视图的片段:

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

     <ImageView
         android:layout_width="80dp"
         android:layout_height="80dp"
         android:id="@+id/imageView2"
         android:layout_alignParentTop="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentStart="true"
         android:layout_marginLeft="16dp"
         android:layout_marginStart="16dp"
         android:layout_marginTop="16dp"
         android:src="@drawable/news_demo"
         android:scaleType="fitXY" />

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="20dp"
         android:textAppearance="?android:attr/textAppearanceMedium"
         android:text="Location Name"
         android:id="@+id/textViewNewsHeader"
         android:textColor="@color/colorPrimaryTest"
         android:layout_alignTop="@+id/imageView2"
         android:layout_toRightOf="@+id/imageView2"
         android:layout_toEndOf="@+id/imageView2" />

     <TextView
         android:layout_width="wrap_content"
         android:lines="4"
         android:layout_height="wrap_content"
         android:layout_marginLeft="20dp"
         android:layout_marginTop="5dp"
         android:layout_marginRight="60dp"
         android:textAppearance="?android:attr/textAppearanceSmall"
         android:text="Location description"
         android:id="@+id/textViewShortStory"
         android:textColor="@color/black"
         android:layout_below="@+id/textViewNewsHeader"
         android:layout_toRightOf="@+id/imageView2"
         android:layout_toEndOf="@+id/imageView2" />

     <TextView
         android:layout_width="wrap_content"
         android:lines="4"
         android:layout_height="wrap_content"
         android:layout_marginLeft="20dp"
         android:layout_marginRight="60dp"
         android:textAppearance="?android:attr/textAppearanceLarge"
         android:text="5"
         android:id="@+id/clinicNumber"
         android:textColor="@color/colorPrimaryTest"
         android:layout_alignParentBottom="true"
         android:layout_alignEnd="@+id/imageButton"
         android:layout_marginEnd="13dp" />

     <ImageButton
         android:layout_width="42dp"
         android:layout_height="42dp"
         android:id="@+id/imageButton"
         android:src="@drawable/arrow_right_black"
         android:background="@color/white"
         android:scaleType="fitXY"
         android:layout_marginRight="20dp"
         android:layout_centerVertical="true"
         android:layout_alignParentRight="true"
         android:layout_alignParentEnd="true"
         android:focusable="false"
         />
 </RelativeLayout>

以下是查看Container类

的一些代码
 adapter = new LocationHospitalListFragmentAdapter(getContext(), locationList, R.layout.fragment_search_location_hospital_list);
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //show list view layout
        Toast.makeText(getActivity().getApplicationContext(),"onItemClick",Toast.LENGTH_SHORT).show();
        Log.d("333__","on click");
    }
});

1 个答案:

答案 0 :(得分:0)

将此行添加到ListItem android:descendantFocusability="blocksDescendants"

的父布局中