使用片段的Android功能区菜单

2024-03-31

我使用水平滚动视图对功能区菜单进行了编码。我的代码如下:

public class HorzScrollWithListMenuActivity extends Activity {


        MyHorizontalScrollView scrollView;
        View menu;
        View app;
        ImageView btnSlide;
        boolean menuOut = false;
        Handler handler = new Handler();
        int btnWidth;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState); 

            LayoutInflater inflater = LayoutInflater.from(HorzScrollWithListMenuActivity.this);
            scrollView = (MyHorizontalScrollView) inflater.inflate(R.layout.horz_scroll_with_list_menu, null);
            setContentView(scrollView); 

            menu = inflater.inflate(R.layout.horz_scroll_menu, null);
            app = inflater.inflate(R.layout.horz_scroll_app, null);
            ViewGroup tabBar = (ViewGroup) app.findViewById(R.id.tabBar);

            ListView listView = (ListView) app.findViewById(R.id.list);
            ViewUtils.initListView(this, listView, "Item ", 50, android.R.layout.simple_list_item_1);

            listView = (ListView) menu.findViewById(R.id.list);
            ViewUtils.initListView(this, listView, "Menu ", 30, android.R.layout.simple_list_item_1);

            btnSlide = (ImageView) tabBar.findViewById(R.id.BtnSlide);
            btnSlide.setOnClickListener(new ClickListenerForScrolling(scrollView, menu,app));

            final View[] children = new View[] { menu, app };

            // Scroll to app (view[1]) when layout finished.
            int scrollToViewIdx = 1;
            scrollView.initViews(children, scrollToViewIdx, new SizeCallbackForMenu(btnSlide));
        }

        /**
         * Helper for examples with a HSV that should be scrolled by a menu View's width.
         */
        static class ClickListenerForScrolling implements OnClickListener {
            HorizontalScrollView scrollView;
            View menu;
            View app;
            /**
             * Menu must NOT be out/shown to start with.
             */
            boolean menuOut = false;

            public ClickListenerForScrolling(HorizontalScrollView scrollView, View menu, View app) {
                super();
                this.scrollView = scrollView;
                this.menu = menu;
                this.app=app;
            }

            public void onClick(View v) {
                Context context = menu.getContext();
                String msg = "Slide " + new Date();
                Toast.makeText(context, msg, 1000).show();
                System.out.println(msg);


                int menuWidth = menu.getMeasuredWidth();

                // Ensure menu is visible
                menu.setVisibility(View.VISIBLE);

                if (!menuOut) {
                    // Scroll to 0 to reveal menu
                    int left = 0;
                    scrollView.smoothScrollTo(left, 0);

                } else {
                    // Scroll to menuWidth so menu isn't on screen.
                    int left = menuWidth;
                    scrollView.smoothScrollTo(left, 0);

                }
                menuOut = !menuOut;
                if(v.isPressed())
                {
                    v.setClickable(false);

                }


            }
        }

        /**
         * Helper that remembers the width of the 'slide' button, so that the 'slide' button remains in view, even when the menu is
         * showing.
         */
        static class SizeCallbackForMenu implements SizeCallback {
            int btnWidth;
            View btnSlide;

            public SizeCallbackForMenu(View btnSlide) {
                super();
                this.btnSlide = btnSlide;
            }

            public void onGlobalLayout() {
                btnWidth = btnSlide.getMeasuredWidth();
                System.out.println("btnWidth=" + btnWidth);
            }

            public void getViewSize(int idx, int w, int h, int[] dims) {
                dims[0] = w;
                dims[1] = h;
                final int menuIdx = 0;
                if (idx == menuIdx) {
                    dims[0] = w - btnWidth;
                }
            }
        }
    }

MyHorizo​​ntalScrollView.java

public class MyHorizontalScrollView extends HorizontalScrollView {
    public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public MyHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyHorizontalScrollView(Context context) {
        super(context);
        init(context);
    }

    void init(Context context) {
        // remove the fading as the HSV looks better without it
        setHorizontalFadingEdgeEnabled(false);
        setVerticalFadingEdgeEnabled(false);
    }

    /**
     * @param children
     *            The child Views to add to parent.
     * @param scrollToViewIdx
     *            The index of the View to scroll to after initialisation.
     * @param sizeCallback
     *            A SizeCallback to interact with the HSV.
     */
    public void initViews(View[] children, int scrollToViewIdx, SizeCallback sizeCallback) {
        // A ViewGroup MUST be the only child of the HSV
        ViewGroup parent = (ViewGroup) getChildAt(0);

        // Add all the children, but add them invisible so that the layouts are calculated, but you can't see the Views
        for (int i = 0; i < children.length; i++) {
            children[i].setVisibility(View.INVISIBLE);
            parent.addView(children[i]);
        }

        // Add a layout listener to this HSV
        // This listener is responsible for arranging the child views.
        OnGlobalLayoutListener listener = new MyOnGlobalLayoutListener(parent, children, scrollToViewIdx, sizeCallback);
        getViewTreeObserver().addOnGlobalLayoutListener(listener);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Do not allow touch events.
        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Do not allow touch events.
        return false;
    }

    /**
     * An OnGlobalLayoutListener impl that passes on the call to onGlobalLayout to a SizeCallback, before removing all the Views
     * in the HSV and adding them again with calculated widths and heights.
     */
    class MyOnGlobalLayoutListener implements OnGlobalLayoutListener {
        ViewGroup parent;
        View[] children;
        int scrollToViewIdx;
        int scrollToViewPos = 0;
        SizeCallback sizeCallback;

        /**
         * @param parent
         *            The parent to which the child Views should be added.
         * @param children
         *            The child Views to add to parent.
         * @param scrollToViewIdx
         *            The index of the View to scroll to after initialisation.
         * @param sizeCallback
         *            A SizeCallback to interact with the HSV.
         */
        public MyOnGlobalLayoutListener(ViewGroup parent, View[] children, int scrollToViewIdx, SizeCallback sizeCallback) {
            this.parent = parent;
            this.children = children;
            this.scrollToViewIdx = scrollToViewIdx;
            this.sizeCallback = sizeCallback;
        }

        public void onGlobalLayout() {
            // System.out.println("onGlobalLayout");

            final HorizontalScrollView me = MyHorizontalScrollView.this;

            // The listener will remove itself as a layout listener to the HSV
            me.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            // Allow the SizeCallback to 'see' the Views before we remove them and re-add them.
            // This lets the SizeCallback prepare View sizes, ahead of calls to SizeCallback.getViewSize().
            sizeCallback.onGlobalLayout();

            parent.removeViewsInLayout(0, children.length);

            final int w = me.getMeasuredWidth();
            final int h = me.getMeasuredHeight();

            // System.out.println("w=" + w + ", h=" + h);

            // Add each view in turn, and apply the width and height returned by the SizeCallback.
            int[] dims = new int[2];
            scrollToViewPos = 0;
            for (int i = 0; i < children.length; i++) {
                sizeCallback.getViewSize(i, w, h, dims);
                // System.out.println("addView w=" + dims[0] + ", h=" + dims[1]);
                children[i].setVisibility(View.VISIBLE);

                parent.addView(children[i], dims[0], dims[1]);
                if (i < scrollToViewIdx) {
                    scrollToViewPos += dims[0];
                }
            }

            // For some reason we need to post this action, rather than call immediately.
            // If we try immediately, it will not scroll.
            new Handler().post(new Runnable() {
                public void run() {
                    me.scrollBy(scrollToViewPos, 0);
                }
            });
        }
    }

    /**
     * Callback interface to interact with the HSV.
     */
    public interface SizeCallback {
        /**
         * Used to allow clients to measure Views before re-adding them.
         */
        public void onGlobalLayout();

        /**
         * Used by clients to specify the View dimensions.
         * 
         * @param idx
         *            Index of the View.
         * @param w
         *            Width of the parent View.
         * @param h
         *            Height of the parent View.
         * @param dims
         *            dims[0] should be set to View width. dims[1] should be set to View height.
         */
        public void getViewSize(int idx, int w, int h, int[] dims);
    }
}

ViewUtils.java

public class ViewUtils {
    private ViewUtils() {
    }


    public static void initListView(Context context, ListView listView, String prefix, int numItems, int layout) {
        // By using setAdpater method in listview we an add string array in list.
        String[] arr = new String[numItems];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = prefix + (i + 1);
        }
        listView.setAdapter(new ArrayAdapter<String>(context, layout, arr));
        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Context context = view.getContext();
                String msg = "item[" + position + "]=" + parent.getItemAtPosition(position);
                Toast.makeText(context, msg, 1000).show();
                System.out.println(msg);
            }
        });
    }
}

请告诉我如何提供滑动选项。

如何使用片段做相同的项目(功能区菜单)?


ActionBarSherlock 是构建此类应用程序的非常有用的库,

请先检查这个库,
http://actionbarsherlock.com/ http://actionbarsherlock.com/

然后检查这个项目,
https://bitbucket.org/owentech/abstabsviewpager/ https://bitbucket.org/owentech/abstabsviewpager/

我希望它能帮助解决您的问题。

快乐编码...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用片段的Android功能区菜单 的相关文章

  • Android ListView数组索引过滤后越界

    我认为这是专家的问题 我接到电话getView with positon 出界来自ListView数据列表 当我使用适配器过滤器时会发生这种情况 过滤器publishResults 方法使用小于原始列表的过滤列表填充数据 当新的过滤列表时似
  • Android CirclePageIndicator 不工作

    我无法使用 CirclePageIndicator 加载页面 这是 XML
  • Android 中的 JDBC 连接

    有没有人在 android 中尝试过 JDBC 连接 因为在 Android 2 3 中支持 JDBC 我必须在没有 Web 服务的情况下连接 Mysql 我已经提出申请 但它给了我错误 public class MysqlConnect
  • Android如何让进度条(圆形)在按下按钮时覆盖全屏?

    我尝试在单击提交按钮时显示进度栏 数据加载完成后它将隐藏 但是 进度条没有覆盖全屏 相反 它被按钮覆盖 请参考截图 应该更容易理解我的意思 我想要实现的是屏幕截图的底部部分 Main4Activity java public class M
  • Orientation改变时如何处理Activity?

    我正在编写一个活动 它从服务器加载数据并使用 ArrayAdapter 将其显示为列表 为此 我显示了一个进度对话框 即加载 同时它从服务器加载所有数据 然后我在处理程序中关闭该对话框 我的问题是 当我更改方向时 会再次显示进度对话框 这是
  • 在 Android 中绘制一条带有弯曲边缘的线

    I am using canvas drawLine to draw some line in android but the lines are too sharp but i need a curved edges 这里的 1 是我所拥
  • Android:自动从项目包中删除未使用的图像

    我正在开发一个相当大的android项目 并且在drawable文件夹中有很多图像 其中许多图像未在项目中使用 因为它们已被替换 并且占用了宝贵的空间 有什么方法可以自动找到这些图像并删除它们 而不是搜索项目中的每个图像 我使用过的一些工具
  • 此版本不符合 Google Play 64 位要求,添加库后仍然出现错误

    我正在 Play 商店上传一个视频编辑器应用程序 其中包含带有一些本机代码的库 所以我通过将其添加到 gradle 来使其兼容 64 位 ndk abiFilters armeabi v7a arm64 v8a x86 x86 64 添加了
  • Galaxy Nexus 4.1.1 和 ISO14443 读卡器

    是否可以在 Galaxy Nexus Jelly Bean 4 1 1 移动设备和任何常规桌面非接触式读卡器 ISO 14443 A B 之间建立连接 据我所知 android不支持卡模拟模式 所以只能通过p2p模式来完成 p2p 是否基于
  • 如何将txt文件添加到你的android项目中? [复制]

    这个问题在这里已经有答案了 我的Android studio版本是1 5 1 显然这个 never 版本没有 txt 文件的 asset 文件夹 您打算如何将这些文件包含到您的项目中 以及如何进一步使用您内部的应用程序 谢谢你的建议 Pro
  • 推送通知需要很长时间才能到达

    我在适用于 iOS 和 Android 的 Adob e Air 应用程序中遇到推送通知的奇怪问题 我正在使用 Milkman Games 的 Easy Push ANE 以及 One Signal 服务 问题是通知确实会到达 但有时 随机
  • 使用MockWebServer暂停功能测试

    我正在测试使用 MockWebServer 的挂起函数返回结果的 api 但它不适用于 runBlockingTest testCoroutineDispatcher testCorounieScope 除非launch使用builder
  • 膨胀类片段 InflateException 二进制 XML 文件时出错

    我正在使用 Material Design 和 NavigationDrawer 布局等设计我的第一个应用程序 但我遇到了一个问题 该应用程序非常简单 它只显示文本 并且基于 Android Studio 中提供的模板 尝试启动我的应用程序
  • 在Android中绘制圆角矩形

    我已经发现这个问题 https stackoverflow com questions 5618402 how to draw rounded rectangle in android ui解决方案是这段代码
  • 将 espresso 与自定义 EditText 结合使用

    这是我的布局的一部分
  • Android 和 iPhone 应用程序可以使用同一个 Facebook 应用程序 ID 吗?

    我有两个具有相同名称和相同功能的应用程序 一款在安卓市场 一款在应用商店 目前仅通过 iPhone 应用程序 您可以使用我创建的 Facebook 应用程序将您的分数发布到 Facebook 墙上 我的问题是我可以使用相同的 Android
  • Listview里面只有一个Element

    您好 我正在尝试将列表视图放入列表视图中的列表视图中 唯一的问题是只有第一个列表视图正确显示所有元素 此后的每个列表视图仅包含一个元素 UPDATE 创建我自己的不可滚动列表视图解决了这个问题 https stackoverflow com
  • 无法在 BlackBerry Playbook 上设置音量

    我在更改黑莓游戏书的音量时遇到问题 首先 我将 Android 应用程序重新打包到 Palybook 应用程序 我需要使用搜索栏更改黑莓剧本的音量 并在搜索监听器中设置音频管理器音量 这是代码 audioManager AudioManag
  • Android 等距游戏引擎 [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 是否有任何现有的开源或商业解决方案
  • 如何在Android中显示进度对话框?

    我想展示ProgressDialog当我单击 登录 按钮时 需要一些时间才能移动到另一个页面 我怎样才能做到这一点 ProgressDialog pd new ProgressDialog yourActivity this pd setM

随机推荐