Android Action bar custom dropdown view on item click

梦想与她 提交于 2019-11-28 16:45:20

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.

For instance

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];
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!