关于项目点击Android的动作栏自定义下拉列表视图 [英] Android Action bar custom dropdown view on item click

查看:150
本文介绍了关于项目点击Android的动作栏自定义下拉列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了平板电脑的Andr​​oid应用程序。我已经用行动栏创建我的图标。但是,我需要打开点击菜单项之一,当自定义视图。

I'm writing an Android app for tablets. I've gone with the action bar to create my icons. However, I need to open a custom view when one of the menu items is clicked.

我不希望自定义操作吧 - 我需要膨胀的自定义视图中的浏览主题动作栏项目被点击时。该视图将需要出现像下拉但是使用自己的自定义布局,因为它不会被用于导航

I don't want a custom action bar - I need to inflate a custom view when the "Browse Subjects" action bar item is clicked. This view will need to appear like a dropdown but be using my own custom layout as it will not be used for navigation.

下面是我menu.xml文件

Here is my menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_browse"
        android:title="Browse Subjects"
        android:showAsAction="always"
        android:actionLayout="@layout/action_layout_browse"
        android:actionProviderClass="au.com.pearson.f12catalogue.action_providers.BrowseProvider"
            />
    <item android:id="@+id/menu_settings"
        android:title="Settings"
        android:orderInCategory="100"
        android:showAsAction="never" />
</menu>

我以为ActionProviderClass会允许我实例化一个自定义视图的操作栏项目被点击,但我不能找出一个办法,当 - ,也许我会走上错误的道路。

I assumed the ActionProviderClass would allow me to instantiate a custom view when the action bar item is clicked but I can't work out a way - perhaps I'm going down the wrong path.

在这个任何帮助将是 MUCH AP preciated!谢谢!

Any help on this would be MUCH appreciated! Thanks!

更新: 感谢您的联系行动直板造型,但我并不想简单样式下拉。我想夸大的自定义视图。该视图将执行数据库查询等以及。

UPDATE: Thanks for links to action bar styling but I don't want to simply style a dropdown. I want to inflate a custom view. The view will perform DB queries etc aswell.

推荐答案

好吧,我摸索出了自己的解决方案。基本上actionProviderClass用于实例在动作条的而ActionView。在这个类,你可以将一个onClick监听器抬高你的看法。我用这个收听者点击时可以填充下拉列表视图中的主框架。

Ok I worked out a solution myself. Basically the actionProviderClass is used to instantiate an actionView in the actionBar. In this class you can attach an onClick listener to the view you inflate. I used this listener to inflate a dropdown view in the main frame when clicked.

例如

public class BaseProvider extends ActionProvider {

    protected final Context context;
    protected final int layout;
    protected final BaseProvider self;
    protected View view;
    protected int positionLeft = 0;
    protected Dropdown dropdown;

    public BaseProvider(Context context, int layout, Dropdown dropdown) {
        super(context);
        this.layout = layout;
        this.context = context;
        this.self = this;
        this.dropdown = dropdown;
    }

    @Override
    public View onCreateActionView() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

        View view = inflater.inflate(this.layout, null);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                self.onItemClick();
            }
        });
        this.view = view;
        return view;
    }

    public boolean onItemClick(){
        toggleDropdown();
        return true;
    }

    protected void toggleDropdown(){
        this.positionLeft = getRelativeLeft(view);
        DropdownInflater.getInstance().toggleDropdown(this.dropdown,this.positionLeft);
    }

    protected int getRelativeLeft(View view) {
        int[] loc = new int[2];
        view.getLocationOnScreen(loc);
        return loc[0];
    }
}

这篇关于关于项目点击Android的动作栏自定义下拉列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆