【问题标题】:How to add a dropdown to tab in viewpager sliding tabs?如何在 viewpager 滑动选项卡中向选项卡添加下拉列表?
【发布时间】:2016-06-27 15:27:35
【问题描述】:

我的应用中有 8 个选项卡,我希望更轻松地访问最后 4 个选项卡,否则必须滚动到最后才能到达。

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

如何做到这一点?没有关于如何执行此操作的文档?只提供图片。

Here's the link to material design guidelines for tabs

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

【问题讨论】:

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


    【解决方案1】:

    我建议您使用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>
    

    【讨论】:

    • 如何将弹出菜单中的选定选项带到最后一个选项卡并将其显示为选定选项卡?
    • 我的示例中的锚点为空。将其更改为您想要的选项卡!那么你应该拥有它:)
    • 这不能回答我的问题,我想知道如何使用下拉菜单设置选项卡,我知道如何制作弹出菜单,但如果您建议在选项卡中使用弹出菜单,请解释如何使用与标签相关的代码。
    • 即添加弹出菜单。您只需将锚点设置为您想要的标签即可。
    • 我不认为我们可以在选项卡内添加弹出菜单..如果您知道怎么做,请更新答案..如何在选项卡内添加弹出菜单..跨度>
    【解决方案2】:

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

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

    Here's the link to the question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 2023-01-15
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多