0

我在我的應用程序中有8個選項卡,我想提供更容易訪問最後4個選項卡,否則必須滾動,直到到達結束。如何添加一個下拉選項卡在viewpager滑動標籤?

根據材料設計指南,我們可以在選項卡中使用下拉菜單,通過給出選項「more」作爲最後一個選項卡,並且如果用戶選擇下拉項目,該項目將出現在倒數第二個選項卡上並將突出顯示作爲選定的選項卡。

如何實現這一目標?有沒有關於如何做到這一點的文件?只提供圖像。

Here's the link to material design guidelines for tabs

Here's an image of tabs with an option "more"

回答

0

我會回答我的問題,它可能會救一個人的時候,如果他們決定去這個road.Here的我發現了什麼: -

我已經張貼在有關TabLayout溢出分頁標籤類似的問題並從@ianhanniballake處獲得答案,他在其中提到這些功能適用於桌面選項卡,而TabLayout不支持。

Here's the link to the question

0

我會建議你使用PopupMenu。它易於使用,看起來不錯。

下面是一個例子如何使用它:

活動

View view = findViewById(R.id.action_settings); 

      LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View popupView = layoutInflater.inflate(R.layout.popup, null); 
      final ListView listView = (ListView) popupView.findViewById(R.id.listView); 

      String[] functions = {getString(R.string.shareScreenshot), getString(R.string.shareDatei), getString(R.string.shareXML)}; 

      ListAdapter adapter = new CustomPopupAdapter(this, functions, listView); 
      listView.setAdapter(adapter); 

      Display display = (this.getWindowManager().getDefaultDisplay()); 
      Point size = new Point(); 
      display.getSize(size); 
      int width = size.x; 
      //int height = size.y; 

      Resources resources = this.getResources(); 
      int navigationBarHeight = 0; 
      int statusbarHeight = 0; 
      int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); 
      if (resourceId > 0) { 
       navigationBarHeight = resources.getDimensionPixelSize(resourceId); 
      } 

      resourceId = resources.getIdentifier("status_bar_height", "dimen", "android"); 
      if (resourceId > 0) { 
       statusbarHeight = resources.getDimensionPixelSize(resourceId); 
      } 

      final PopupWindow popupWindow = new PopupWindow(this); 
      popupWindow.setContentView(popupView); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
       popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.shadow_02327)); 
      } else { 
       popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.shadow_02327)); 
      } 
      popupWindow.setWidth(width); 
      popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 
      popupWindow.setOutsideTouchable(true); 
      popupWindow.setFocusable(true); 
      popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0, navigationBarHeight + statusbarHeight); 

      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

        //Click handle 
       } 
      }); 
     } 

適配器

public class CustomPopupAdapter extends ArrayAdapter { 

      private String[] option; 
      ListView owner; 


     public CustomPopupAdapter(Context context, String[] option, ListView owner) { 
      super(context, R.layout.custom_row_settings, option); 
      this.option = option; 
      this.owner = owner; 
     } 

     @Override 
     public View getView(int pos, View view, ViewGroup parent) { 
      LayoutInflater inflater = LayoutInflater.from(getContext()); 

      View customView = inflater.inflate(R.layout.popup_row_image_text, parent, false); 

      ImageView iv = (ImageView) customView.findViewById(R.id.imageView); 
      TextView tv = (TextView) customView.findViewById(R.id.textView); 
      tv.setText(option[pos]); 


      switch (pos) { 
       case 0: 
        iv.setImageResource(R.drawable.ic_photo_camera_grey_24dp); 
        break; 
       case 1: 
        iv.setImageResource(R.drawable.ic_insert_drive_file_grey_24dp); 
        break; 
       case 2: 
        iv.setImageResource(R.drawable.ic_code_grey_24dp); 
        break; 
       case 3: 
        iv.setImageResource(R.drawable.ic_move_to_inbox_grey_24dp); 
        break; 
      } 

      return customView; 
     } 
    } 

popup.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="5dp" 
    android:background="@color/white"> 

    <ListView 
     android:id="@+id/listView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

    </ListView> 

</RelativeLayout> 
+0

我怎麼可以把從彈出菜單中選擇的選項,最後一個選項卡並顯示爲選定的選項卡? –

+0

我的示例中的錨爲null。將其更改爲您想要的選項卡!那麼你應該有它:) – XxGoliathusxX

+0

這不會回答我的問題,我想知道如何設置一個下拉選項卡,我知道如何使彈出菜單,但如果您建議在選項卡中使用彈出菜單,請解釋如何使用與選項卡相關的代碼。 –

相關問題