0

如果有一種方法可以讓CollapsingToolbarLayout菜單在一個片段中,而我們不需要在其中定義自定義佈局,那我就非常有趣。 在手機佈局上,我製作了具有菜單的DetailActivity。 enter image description hereCollapsingToolbarLayout片段內的菜單

在平板電腦佈局上,我在右側製作了細節片段。 CollapsingToolbarLayout實際上工作正常,但我無法直接向它添加任何菜單。 enter image description here

我知道Fragment.setHasOptionsMenu(true);,但我不想合併我的詳細菜單與活動菜單。

PS:我在這裏使用的圖像檢索TMDb,它是免費的。

回答

1

我已經潛入CollapsingToolbarLayout並使用Google搜索,但我認爲這是不可能的。因此,爲了解決方法,我使用菜單溢出圖標創建ImageView,並在其下面彈出顯示列表以模擬操作菜單行爲。 這裏是我的CollapsingToolbarLayout看起來像XML

<android.support.design.widget.CollapsingToolbarLayout 
    android:fitsSystemWindows="false" 
    android:id="@+id/collapsing_toolbar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    app:contentInsetStartWithNavigation="0dp" 
    app:contentScrim="?attr/colorPrimary" 
    app:expandedTitleMarginEnd="64dp" 
    app:expandedTitleMarginStart="48dp" 
    app:layout_scrollFlags="exitUntilCollapsed|scroll"> 

    <your.app.AutoFitImageView 
     android:fitsSystemWindows="false" 
     android:id="@+id/iv_backdrop_image" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:scaleType="fitCenter" 
     android:transitionName="image" 
     app:layout_collapseMode="parallax" 
     tools:targetApi="lollipop"/> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_height="?attr/actionBarSize" 
     android:layout_width="match_parent" 
     app:layout_collapseMode="pin" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 

     <ImageView 
      android:id="@+id/button_menu" 
      android:layout_gravity="right" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:minHeight="48dp" 
      android:minWidth="48dp" 
      android:scaleType="centerInside" 
      android:src="@drawable/ic_action_overflow"/> 
    </android.support.v7.widget.Toolbar> 

</android.support.design.widget.CollapsingToolbarLayout> 

然後我設置clickListener到的ImageView與R.id.button_menu id來調用這個方法:

private void showListMenu(View anchor) { 
    final ListPopupWindow popupWindow = new ListPopupWindow(getActivity()); 

    List<HashMap<String, String>> data = new ArrayList<>(); 

    HashMap<String, String> map1 = new HashMap<>(); 
    map1.put("menu", "Share Detail"); 

    HashMap<String, String> map2 = new HashMap<>(); 
    map2.put("menu", "Share First Trailer"); 

    data.add(map1); 
    data.add(map2); 

    final Movie movieData = getMovieData(); 
    HashMap<String, String> map3 = new HashMap<>(); 
    if (DbHelper.movieInDb(movieData)) { 
     map3.put("menu", getString(R.string.unfavorite)); 
    } else { 
     map3.put("menu", getString(R.string.favorite)); 
    } 
    data.add(map3); 

    ListAdapter adapter = new SimpleAdapter(
      getActivity(), 
      data, 
      android.R.layout.simple_list_item_1, // You may want to use your own cool layout 
      new String[]{"menu"}, // These are just the keys that the data uses 
      new int[]{android.R.id.text1}); // The view ids to map the data to 

    popupWindow.setAnchorView(anchor); 
    popupWindow.setAdapter(adapter); 

    DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    popupWindow.setWidth(displaymetrics.widthPixels/3); // note: don't use pixels, use a dimen resource 

    popupWindow.setOnItemClickListener(yourListener); // the callback for when a list item is selected 
    popupWindow.show(); 
} 

的方法將顯示具有相同的樣式溢出操作菜單ListPopUpWindow點擊時。 這裏是最終的佈局看起來像 enter image description here

+0

非常聰明,而且非常有用的解決方案。 –