2016-01-20 134 views
0

我不知道whay当我设置longClickListener我的一个看法,它不火,这是我的布局:安卓: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)但我无法解决这个问题。解决方法是什么? 请指教

回答

0

认沽机器人:longClickable =“真”在你的相对布局要添加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> 

尝试对相对布局而不是MapView的集长按听众:

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

它不起作用 –

+0

尝试在relativeLayout上设置监听器。看到我更新的答案。 –

+0

我已经尝试过了,但它不起作用 –

1

您可以将此代码添加到您的类:

mapView.setLongClickable(true); 

或者将android:longClickable="true"添加到xml。

+0

我已经做了它,但它不起作用 –

+0

您可以确保您单击是mapView吗?可能无法显示Mapview。 –