Android的 - 动态滑动菜单 [英] Android - Dynamic Slide Menu

查看:150
本文介绍了Android的 - 动态滑动菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用教程(来源$ C ​​$ C的这里),我是能够创造利用单一的XML布局菜单和主要内容都在Android上滑动菜单

问题是我需要做的菜单和内容的动态。我可以创建一个动态的观点,但我不知道如何用一个按钮\\轻扫切换到第二个动态视图或创建两种观点并有一个滑出时一键pressing。内容的必须的是动态的,菜单将在从互联网文件为主。我看过了无数的教程,但我还没有遇不到如何融合在一起所有的人都为单一的连贯的项目。

因此​​,这将意味着要么使视图菜单,查看上下文从上面链接(而不是从XML加载它),或简单地创建,我可以根据按键开关左右移动两种观点的来源$ C ​​$ C动态。

下面的非动态版:

MainActivity.java

 包com.example.slider;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.View;进口com.example.slider.view.viewgroup.FlyOutContainer;
公共类MainActivity延伸活动{FlyOutContainer根;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    this.root =(FlyOutContainer)this.getLayoutInflater()膨胀(R.layout.activity_sample,NULL);    this.setContentView(根);    }
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    。getMenuInflater()膨胀(R.menu.main,菜单);    返回true;
}公共无效toggleMenu(视图v){
    this.root.toggleMenu();
}
}

下面是FlyOutContainer.java

 包com.example.slider.view.viewgroup;进口android.annotation.Sup pressLint;
进口android.content.Context;
进口android.util.AttributeSet;
进口android.view.View;
进口android.widget.Button;
进口android.widget.CheckBox;
进口android.widget.EditText;
进口android.widget.LinearLayout;
进口android.widget.ScrollView;
进口android.widget.TextView;@燮pressLint(NewApi)
公共类FlyOutContainer扩展的LinearLayout {
私人视图菜单;
私人查看上下文受保护的静态最终诠释menuMargin = 125;公共枚举MenuState {
    封闭式,开放
};保护INT currentContentOffset = 0;
保护MenuState menuCurrentState = MenuState.Closed;公共FlyOutContainer(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
    // TODO自动生成构造函数存根}公共FlyOutContainer(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    // TODO自动生成构造函数存根
}公共FlyOutContainer(上下文的背景下){
    超级(上下文);
    // TODO自动生成构造函数存根
}保护无效onAttachedToWindow(){
    super.onAttachedToWindow();    this.menu = this.getChildAt(0);    this.context = this.getChildAt(1);    this.menu.setVisibility(View.GONE);
}保护无效onLayout(布尔变化,诠释离开,诠释顶部,右诠释,诠释底部){
    如果(改变)
        this.calculateChildDimensions();        this.menu.layout(左,上,右 - menuMargin,底部);        this.context.layout(左+ this.currentContentOffset,上,右
                + currentContentOffset,底部);}公共无效toggleMenu(){
    开关(this.menuCurrentState){
    结案:
        this.menu.setVisibility(View.VISIBLE);
        this.currentContentOffset = this.getMenuWidth();
        this.context.offsetLeftAndRight(currentContentOffset);
        this.menuCurrentState = MenuState.Open;
        打破;
    开放的情况下:
        this.context.offsetLeftAndRight(-currentContentOffset);
        this.currentContentOffset = 0;
        this.menuCurrentState = MenuState.Closed;
        this.menu.setVisibility(View.GONE);
        打破;
    }
    this.invalidate();
}私人诠释getMenuWidth(){
    返回this.menu.getLayoutParams()宽。
}
私人无效calculateChildDimensions(){
    。this.context.getLayoutParams()高度= this.getHeight();
    。this.context.getLayoutParams()宽度= this.getWidth();    。this.menu.getLayoutParams()高度= this.getHeight();
    。this.menu.getLayoutParams()宽度= this.getWidth() - menuMargin;
}
}


解决方案

有已经可以使用一些好的库。无需推倒重来。

或者使用官方之一:

Using a tutorial (source code here), I was able to create a slide menu in android using a single xml layout for both the menu and the main content.

The problem is I need to make the menu and the content dynamic. I can create a dynamic view, but I don't know how to switch to a second dynamic view using a button\swipe or to create both views and have one slide out upon pressing of a button. The content must be dynamic, the menu will be based upon files from the internet. I've looked up numerous tutorials, but I haven't quite found how to meld all of them together into single coherent project.

So that would mean either making the View menu and View context dynamic from the source code linked above (rather than loading it from the xml), or simply creating two views that I can move around based on button switching.

Here's the non-dynamic version:

MainActivity.java

package com.example.slider;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

import com.example.slider.view.viewgroup.FlyOutContainer;


public class MainActivity extends Activity{

FlyOutContainer root;

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

    this.root = (FlyOutContainer) this.getLayoutInflater().inflate(R.layout.activity_sample, null);

    this.setContentView(root);

    }
@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.main, menu);

    return true;
}

public void toggleMenu(View v){
    this.root.toggleMenu();
}


}

Here's FlyOutContainer.java

package com.example.slider.view.viewgroup;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

@SuppressLint("NewApi")
public class FlyOutContainer extends LinearLayout {


private View menu;
private View context;

protected static final int menuMargin = 125;

public enum MenuState{
    Closed, Open
};

protected int currentContentOffset = 0;
protected MenuState menuCurrentState = MenuState.Closed;

public FlyOutContainer(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub

}

public FlyOutContainer(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public FlyOutContainer(Context context) {
    super(context);
    // TODO Auto-generated constructor stub 
}

protected void onAttachedToWindow(){
    super.onAttachedToWindow();





    this.menu = this.getChildAt(0);

    this.context = this.getChildAt(1);



    this.menu.setVisibility(View.GONE);     
}

protected void onLayout(boolean changed, int left, int top, int right, int bottom){
    if(changed)
        this.calculateChildDimensions();

        this.menu.layout(left, top, right - menuMargin, bottom);

        this.context.layout(left + this.currentContentOffset, top, right
                + currentContentOffset, bottom);

}

public void toggleMenu(){
    switch(this.menuCurrentState){
    case Closed:
        this.menu.setVisibility(View.VISIBLE);
        this.currentContentOffset = this.getMenuWidth();
        this.context.offsetLeftAndRight(currentContentOffset);
        this.menuCurrentState = MenuState.Open;
        break;
    case Open:
        this.context.offsetLeftAndRight(-currentContentOffset);
        this.currentContentOffset = 0;
        this.menuCurrentState = MenuState.Closed;
        this.menu.setVisibility(View.GONE);
        break;
    }
    this.invalidate();
}

private int getMenuWidth(){
    return this.menu.getLayoutParams().width;
}


private void calculateChildDimensions(){
    this.context.getLayoutParams().height = this.getHeight();
    this.context.getLayoutParams().width = this.getWidth();

    this.menu.getLayoutParams().height = this.getHeight();
    this.menu.getLayoutParams().width = this.getWidth() - menuMargin;
}


}

解决方案

There are already some good libraries that you can use. No need to reinvent the wheel.

Or use the official one:

这篇关于Android的 - 动态滑动菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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