从活动android动态更改布局

时间:2014-01-24 11:14:38

标签: android android-actionbar

在我的项目中,我正在动态地从listview布局更改为gridview布局,通过在操作栏按钮单击上调用setcontentview()两次。我还有一个带有按钮和滑动菜单的操作栏。从一个布局换到另一个布局,反之亦然,但是在操作栏滑动菜单停止工作之后。 这是我的代码,我正在改变布局

public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub

    switch (item.getItemId()) {
    case R.id.action_settings:
        if(list)
        {
            list = false ;
            grid = true;
            setContentView(R.layout.gridview);
            gridview = (GridView) findViewById(R.id.gridview);
            arrayAdapter = new EventsListAdapter(getApplicationContext());
            gridview.setAdapter(arrayAdapter);
            actionbar();
        }
        else if(grid)
        {
            grid = false;
            list = true;
            setContentView(R.layout.eventsdetails);
            eventList = (ListView) findViewById(R.id.EventsList);
            arrayAdapter = new EventsListAdapter(getApplicationContext());
            eventList.setAdapter(arrayAdapter);
            actionbar();
        }
        break;
    case android.R.id.home:
    {
        menu.toggle();
    }
    default:
        break;
    }


    return super.onOptionsItemSelected(item);
}
    private void actionbar() {
    // TODO Auto-generated method stub
     ActionBar ab = getSupportActionBar();
        ab.setHomeButtonEnabled(true);
        ab.setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().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);
}

1 个答案:

答案 0 :(得分:0)

尝试getSlidingMenu()而不是创建新的