Android - Dynamic Slide Menu

蓝咒 提交于 2019-12-11 15:33:22

问题


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;
}


}

回答1:


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

  • SlidingMenu#
  • Android-Menudrawer

Or use the official one:

  • NavigationDrawer
  • How to create a NavigationDrawer


来源:https://stackoverflow.com/questions/20549599/android-dynamic-slide-menu

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