如何在单击按钮时创建滑动菜单

时间:2013-11-09 10:25:13

标签: android slidingmenu

我想在点击按钮时创建一个滑动菜单。我使用了Sliding Menu库和Sherlock库。滑动菜单在滑动时效果很好,但我的要求是当我点击按钮时我想滑动菜单滑过。请帮忙。 提前致谢

MainPage代码

package com.ketan.slidingexample;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.slidingmenu.lib.SlidingMenu;

public class MainActivity extends SherlockFragmentActivity implements Menufragment.MenuClickInterFace{
    SlidingMenu menu;
    TextView text;
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text=(TextView)findViewById(R.id.text);
        btn=(Button) findViewById(R.id.button1);

        ActionBar ab = getSupportActionBar();
        ab.setHomeButtonEnabled(true);
        ab.setDisplayHomeAsUpEnabled(true);
        menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        menu.setShadowDrawable(R.drawable.shadow);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.menu);
        menu.setSlidingEnabled(true);

    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        menu.toggle();
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onListitemClick(String item) {
        // TODO Auto-generated method stub
        text.setText(item);
    }
}

菜单页码

package com.ketan.slidingexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;

public class Menufragment extends SherlockFragment {
    ListView list;
    MenuClickInterFace mClick;

    interface MenuClickInterFace {
        void onListitemClick(String item);
    }

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        mClick = (MenuClickInterFace) activity;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        list = (ListView) getView().findViewById(R.id.list);
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                String i=(String) arg0.getItemAtPosition(arg2);
                mClick.onListitemClick(i);
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.menulist, container, false);
        return v;
    }

}

2 个答案:

答案 0 :(得分:0)

按钮上的点击监听器和刚刚放置的点击监听器 肘节();

答案 1 :(得分:0)

编辑button1的xml并添加属性android:onClick="buttonClick"。然后在你的Activity中,而不是片段,添加

public void buttonClick(View v){ menu.showMenu(true); }

这应该可以解决问题。

由于