使用tutorial(源代码here),我能够在Android中使用单个xml布局为菜单和主要内容创建幻灯片菜单。
问题是我需要使菜单和内容动态化。我可以创建一个动态视图,但我不知道如何使用按钮\滑动切换到第二个动态视图,或者创建两个视图并在按下按钮时滑出一个视图。内容必须是动态的,菜单将基于来自互联网的文件。我查了很多教程,但我还没有找到如何将它们全部融合成一个连贯的项目。
这意味着要么从上面链接的源代码中创建View菜单和View上下文(而不是从xml加载它),或者只是根据按钮切换创建两个我可以移动的视图。
这是非动态版本:
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();
}
}
这是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;
}
}
答案 0 :(得分:1)
您可以使用一些好的库。无需重新发明轮子。
或者使用官方的: