如何在viewpager滑动标签中为标签添加下拉列表?

时间:2016-06-21 16:36:38

标签: android android-viewpager material-design android-tabs android-tablayout

我的应用程序中有8个标签,我希望能够更方便地访问最后4个标签,否则必须滚动到最后才能到达。

根据材料设计指南,我们可以在标签中使用下拉菜单,通过提供选项"更多"作为最后一个选项卡,如果用户选择了一个下拉项,该项将出现在倒数第二个选项卡上,并将突出显示为所选选项卡。

如何实现这一目标?没有关于如何做到这一点的文件?只提供图像。

Here's the link to material design guidelines for tabs

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

我会回答我自己的问题,如果他们决定走这条路,可能会节省一些时间。这就是我发现的: -

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

Here's the link to the question