如何在 viewpager 滑动选项卡中向选项卡添加下拉列表?

How to add a dropdown to tab in viewpager sliding tabs?

我的应用程序中有 8 个选项卡,我想提供对最后 4 个选项卡的更轻松访问,否则必须滚动到最后才能到达。

根据Material设计准则,我们可以在选项卡中使用下拉菜单,方法是提供一个选项"more"作为最后一个选项卡,如果用户选择了一个下拉项目,该项目将出现在倒数第二个选项卡并将突出显示为所选选项卡。

如何实现?没有关于如何执行此操作的文档?仅提供图片。

Here's the link to material design guidelines for tabs

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

我建议您使用 PopupMenu。它易于使用,看起来很棒。

这是我如何使用它的示例:

Activity

  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>

我会回答我自己的问题,如果他们决定继续这个 road.Here 这可能会节省他们的时间 road.Here 我发现了:-

我在选项卡上发布了一个关于 TabLayout 溢出分页的类似问题,并收到了@ianhanniballake 的回答,他在回答中提到这些功能适用于桌面选项卡,TabLayout 不支持。