有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java setOnItemClickListener()不处理片段的listView

我的ListView元素上的句柄点击有一些问题,该元素包含自定义的碎片数组适配器。任何地方都没有复选框,片段中唯一的按钮是ImageButton,它已经有了安卓:focusable=“false”。在ListView的布局中,我有一些框架布局、一些按钮和下面的EditText XML代码。任何解决方案,比如安卓:focusableInTouchMode=“false”,安卓:focusableInTouchMode=“false”,列表。setItemsCanFocus(错误)。我搜索了很多,但没有任何帮助

下面是上面带有Gmap和listView的listView的主容器:

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

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

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

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

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

     </FrameLayout>
 </LinearLayout>

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

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

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

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

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

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

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

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

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

         </FrameLayout>
     </LinearLayout>

     <FrameLayout
         安卓:layout_width="fill_parent"
         安卓:layout_height="wrap_content">

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

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

下面是列表视图的片段:

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

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

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

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

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

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

下面是来自视图容器类的一些代码

 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) 个答案

  1. # 1 楼答案

    将此行添加到ListItem的父布局android:descendantFocusability="blocksDescendants"