Android:LongClickListener无法正常工作

时间:2016-01-20 15:56:03

标签: android

我不知道当我将longClickListener设置为我的视图时,它不是火,这是我的布局:

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

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:id="@+id/map"
        android:layout_below="@+id/toolbar"
        android:layout_height="match_parent">


    </RelativeLayout>
    <TextView
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="16"
        android:id="@+id/zoom_level"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
    <RelativeLayout
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:padding="10dp"
            android:background="@drawable/zoom_in_bg"
            android:layout_above="@+id/zoom_out"
            android:src="@drawable/ic_add_black_24dp"
            android:layout_width="wrap_content"
            android:id="@+id/zoom_in"
            android:layout_marginBottom="1dp"
            android:layout_height="wrap_content"/>

        <ImageView
            android:padding="10dp"
            android:background="@drawable/zoom_out_bg"
            android:id="@+id/zoom_out"
            android:layout_alignParentBottom="true"
            android:src="@drawable/ic_remove_black_24dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RelativeLayout>


</RelativeLayout>

它是片段

 public class MapFragment extends BaseFragment implements View.OnLongClickListener
{

    private View rootView;
    private org.mapsforge.android.maps.MapView mapView ;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        rootView = inflater.inflate(R.layout.fragment_navigation, container, false);
        mapView = new org.mapsforge.android.maps.MapView(getContext());
        mapView.setOnLongClickListener(this);
        RelativeLayout relativeLayout = (RelativeLayout) rootView.findViewById(R.id.map);
        relativeLayout.addView(mapView, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
        return rootView;
    }

    @Override
    public boolean onLongClick(View view)
    {
        KLog.error("Long Click called");

        return true;
    }
}

我尝试了SetClickable(true)setLongClickable(true)setFocusable(true),但我无法解决这个问题。解决方案是什么? 请指教

2 个答案:

答案 0 :(得分:1)

您可以将此代码添加到您的课程中:

default :
    printf("you entered wrong letter.");
    continue;
}
flag=1;
nob--;

或者将mapView.setLongClickable(true); 添加到xml。

答案 1 :(得分:0)

把android:longClickable =&#34; true&#34;在您要添加onLongClickListener的相对布局中:

   <RelativeLayout
    android:layout_width="match_parent"
    android:id="@+id/map"
    android:layout_below="@+id/toolbar"
    android:layout_height="match_parent"
    android:longClickable="true">


   </RelativeLayout>

尝试使用相对布局上的set long click listener而不是mapView:

    RelativeLayout relativeLayout = (RelativeLayout) rootView.findViewById(R.id.map);
    relativeLayout.setOnLongClickListener(this);