5

所以我ExpandableListView具有被定義如下組行:獲取的PopupMenu的情況下像文本菜單

group_row.xml

<?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" > 

    <TextView 
     android:id="@+id/GroupName" 
     style="@style/ListViewRowStyle" 
     android:paddingLeft="40dp" 
     android:textSize="18sp" > 
    </TextView> 

    <ImageView 
     android:id="@+id/Menu" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_marginTop="10dp" 
     android:contentDescription="@string/default_content_description_text" 
     android:src="@drawable/ic_menu_moreoverflow_normal_holo_light" > 
    </ImageView> 

</RelativeLayout> 

當你點擊TextView將取決於是否展開或摺疊或者不是當前顯示子行。我已將OnClickListener附加到組行中的ImageView。當這個ImageView點擊我發動PopupMenu像下面的圖片:

enter image description here

enter image description here

一旦PopupMenu顯示和動作中的一個被點擊,我想執行的所有動作小組的孩子。問題是我無法確定點擊了哪一行。

我想通了如何將一個動作適用於所有孩子的唯一方法是用ContextMenu像下面的圖片:

enter image description here

我想避免使用ContextMenu,因爲上一組LongClick對於用戶來說行可能並不明顯,它會引起一些行爲在子行上執行。我認爲更明顯的設計是將PopupMenu錨定到ImageView(在我的情況下是菜單圖標),並將該操作應用於該組的子行。如何通過PopupMenu獲得此功能?

回答

4

所以我想通了,爲了有一些背景,其中被點擊菜單圖標,我使用的View類的setTag()getTag()方法和剛纔應用這些方法對ImageView(菜單圖標)。

+0

儘管它已經過,因爲你張貼這一年,我不同的解決了這一點,並使用contextView保持,但如果附着的觀點相匹配的某個ID我想將其轉換彈出。 http://stackoverflow.com/questions/12407722/how-to-cancel-the-creation-of-a-context-menu-after-oncreatecontextmenu-has-bee/24014727#24014727 如果你在這裏檢查我的答案'感興趣。 (讓我知道如果我是一個白癡) –

3

您需要:

  • 一個View哪裏膨脹的彈出菜單(您ImageView
  • 一個PopUpMenu保存在res /菜單,在這種情況下popup_select_deselect.xml
  • 你自己onMenuItemClickListener聲明爲內部類,在這種情況下onMenuItemClickListener_View

代碼:

//TODO initialize rows[] 
for (int i = 0; i < rows.lenght; i++){ 
    //inflate you group_row 
    getLayoutInflater().inflate(R.layout.group_row, (ViewGroup)findViewById(R.id.rows_container)); 

    ImageView v_Overflow = (ImageView)findViewById(R.id.Menu); 

    //Set onClickListener 
    v_Overflow.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        View v_Button = v; 
        PopupMenu pum= new PopupMenu(YourActivity.this, v); 

        //set my own listener giving the View that activates the event onClick (i.e. YOUR ImageView) 
        pum.setOnMenuItemClickListener(new onMenuItemClickListener_View(v)); 
          //inflate your PopUpMenu 
        getMenuInflater().inflate(R.menu.popup_select_deselect, pum.getMenu()); 
        pum.show(); 

       } 
      }); 


    //Update the id of your TextView 
    .setId(i); //the i value will be your UNIQUE id for the ImageView 

} 

上面的代碼只是您自己的OnMenuItemClickListener將執行的靜態聲明。

請注意在以下偵聽器的構造函數中給定的View。在創建此偵聽器的實例時,View Id與XML佈局中聲明的ID相同。在運行時它將被更新,所以當調用方法onMenuItemClick時,TextView ID已經被更改。

下面的代碼:

private class onMenuItemClickListener_View implements OnMenuItemClickListener{ 

    View v_View; 

    public onMenuItemClickListener_View(View v){ 
     v_View=v; 
    } 

    @Override 
    public boolean onMenuItemClick(MenuItem item) { 

     int i = v_View.getId(); 

     switch (item.getItemId()) { 
      case R.id.popupItemSelectAll: 
       Toast.makeText(YourActivity.this, "Popup Select All for View #: " + rows[i], Toast.LENGTH_SHORT).show(); 

       //TODO your code to select all 
       return true; 
      case R.id.popupItemDeselectAll: 
       Toast.makeText(YourActivity.this, "Popup Deselect All for View #: " + rows[i], Toast.LENGTH_SHORT).show(); 

       //TODO your code to deselect all 


      return true; 
       default: 
        return false; 
      } 

     } 
    } 
} 
+0

我一直在尋找的東西。謝謝! – Sufian

相關問題