2016-06-18 69 views
10

如何在MenuItem上設置長按聽衆?如何在MenuItem上(在NavigationView上)設置長按監聽器?

我試過this answer,但該方法對我來說不存在。任何解決方案

代碼:

Menu menu = navigationView.getMenu(); 
MenuItem menuItem = menu.findItem(R.id.menu_item); 

// TODO set a long click listener on the menuItem. 
menuItem.setOnLongClickListener(...); // Method does not exist, any other solutions? 

編輯:我不想設置自定義的ActionView,我想從長遠點擊收聽到整個MenuItem,沒有自定義視圖。

+0

顯示您的代碼,請 – BooDoo

+0

@BooDoo沒有什麼可顯示的,但我添加了一些代碼。 –

+0

http://stackoverflow.com/q/23151117/6152781 – BooDoo

回答

5

的許多方面(假設我們使用的工具欄)一個 - 這個例子應該給你的想法如何實現工具欄上的按鈕,長按:

class MyActivity extends Activity {  

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     /** get menu inflater */ 
     MenuInflater menuInflater = getMenuInflater(); 
     /** Inflate the menu 
     * this adds items to the action bar if it is present. */ 
     menuInflater.inflate(R.menu.menu_home, menu); 
     /** find interesting item */ 
     MenuItem item = menu.findItem(R.id.itemId); 
     /** set action view */ 
     item.setActionView(new ImageButton(this)); // this is a Context.class object 
     /** set listener on action view */ 
     item.getActionView().setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       return false; 
      } 
     }); 

     return super.onCreateOptionsMenu(menu); 
    } 
} 

的方法onCreateOptionsMenu - 或任何其他方法時,你可以獲得菜單項參考(省略步驟1-2)

  1. 由充氣
  2. 獲取菜單項
  3. 創建菜單/例如
  4. 創建例如ImageButton的動作視圖
  5. 行動視圖
  6. 設置長按監聽
  7. 上的菜單項目的動作視圖

上面我設置一個動作視圖,然後我把它找回來,從設置菜單項,並設置聽衆(順序無論它也可以這樣):

ImageButton imageButton = new ImageButton(Context); 
imageButton.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       return false; 
      } 
     }); 
item.setActionView(imageButton); 

ps。您還可以設置圖像視圖中的XML作爲菜單項屬性:

<item 
    ... 
    android:actionViewClass="android.widget.ImageButton" 
/> 

then you can get the action view by cast 

    View menuItemActionView = menu.findItem(R.id.itemId).getActionView(); 
    if(menuItemActionView != null 
      && ImageButton.class.isAssignableFrom(menuItemActionView.getCLass())) { 
     ImageButton imageButton = (ImageButton) menuItemActionView; 
    } 

但你設置長按聽衆的動作僅查看,而不是整個項目。 - SuperThomasLab

- 沒有你在這種情況下,設置單個元素上的動作視圖,您更改的菜單項的默認視圖(到的ImageButton控件) - 動作視圖可以是簡單或複雜的視圖類型

但是,如果你不想改變視圖,但保持默認視圖? - SuperThomasLab

例如(這是通過使用一個佈局樹觀察者 /通過設置佈局改變聽衆的許多單程):

private View.OnLongClickListener onLongClickListener = new View.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { 
      return false; 
     } 
    }; 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     /** get menu inflater */ 
     MenuInflater menuInflater = getMenuInflater(); 
     /** Inflate the menu 
     * this adds items to the action bar if it is present. */ 
     menuInflater.inflate(R.menu.menu_home, menu); 
     /** geta menu item using findItem(int itemid) */ 
     MenuItem item = menu.findItem(R.id.itemLogOut); 
     /** check if we have item */ 
     if(item!=null) { 
      /** try get its action view */ 
      View actionView = item.getActionView(); 
      /** check if action view is already set? */ 
      if(actionView==null) { 
       /** get item id to comparte later in observer listener*/ 
       final int itemId = item.getItemId(); 
       /** if not set on top most window an layout changes listener */ 
       getWindow().getDecorView() 
          .addOnLayoutChangeListener(new View.OnLayoutChangeListener() { 
        @Override 
        public void onLayoutChange(View v, int left, int top, int right, 
         int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 
          /** try get view by id we have stored few line up */ 
          View viewById = v.getRootView().findViewById(itemId); 
          /** check if we have any result */ 
          if(viewById!=null) { 
           /** set our listener */      
           viewById.setOnLongClickListener(onLongClickListener); 
           /** remove layout observer listener */ 
           v.removeOnLayoutChangeListener(this); 
          } 
        } 
       }); 
      } else { 
       /** if set we can add our on long click listener */ 
       actionView.setOnLongClickListener(onLongClickListener); 
      } 
     } 
    } 

我試圖OnLayoutChangeListener ,但它仍然無法改變。 - SuperThomasLab

是它 - 但我知道爲什麼it't是不是在你的情況下工作的原因??? - 在我的例子中,我們檢查,如果視圖項目佈局而不是改變,如果菜單視圖佈局,然後檢查是否菜單包含項目

這裏工作代碼爲你學習:

https://github.com/c3ph3us/LongClickOnMenuItem

在回答其他評論:

@ SuperThomasLab這不是對我來說顯而易見的,爲什麼setOnLongClickListener(...)方法不適用於你。 - 侯賽因Seifi

@HosseinSeifi - 看android.view.MenuItem界面 - 它並沒有提供這樣的方法 - 所以對於優秀的程序員,這應該是顯而易見的:)爲什麼他不能達到實現類方法。

+0

謝謝你的回答。但是,長按聽衆在哪裏?我該如何將它設置爲'MenuItem'? –

+0

@SuperThomasLab - 請參閱我編輯的答案 – ceph3us

+0

但是,您只將長單擊偵聽器設置爲操作視圖,而不是整個項目。 –

0

您可以通過這樣實現:

action_menu.xml 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:support="http://schemas.android.com/apk/res-auto" > 

<item 
    android:id="@+id/item1" 
    support:showAsAction="always"> 
</item> 

custom_action_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="10dp" 
    android:paddingRight="5dp" > 

<ImageButton 
    android:id="@+id/customActionItem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:background="@drawable/abc_item_background_holo_dark" 
    android:src="@drawable/bulb_icon" /> 

</RelativeLayout> 

和菜單吹氣代碼如下:

public boolean onCreateOptionsMenu(Menu menu) { 
    // TODO Auto-generated method stub 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.action_menu, menu);  

    final MenuItem item1= menu.findItem(R.id.item1); 
    MenuItemCompat.setActionView(item1, R.layout.custom_action_view); 
    View vItem1= MenuItemCompat.getActionView(item1); 

    final ImageButton customActionItem= (ImageButton) vItem1.findViewById(R.id.customActionItem); 
    customActionItem.setOnLongClickListener(new OnLongClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      // do something here 
     } 
    }); 

    return super.onCreateOptionsMenu(menu); 
} 
+0

這與其他答案一樣。我不想要一個自定義的ActionView,我想將這個長的點擊監聽器設置爲普通的項目,而不需要自定義視圖。 –

相關問題