Android – ActionBarSherlock侧面菜单导航抽屉

Android – ActionBarSherlock侧面菜单导航抽屉,第1张

概述我试图在动作栏中使用抽屉切换.这是我的主要活动: import uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshAttacher;import android.content.res.Configuration;import android.os.Bundle;import android.su 我试图在动作栏中使用抽屉切换.这是我的主要活动:
import uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshAttacher;import androID.content.res.Configuration;import androID.os.Bundle;import androID.support.v4.app.ActionbarDrawerToggle;import androID.support.v4.app.Fragment;import androID.support.v4.app.FragmentManager;import androID.support.v4.Widget.DrawerLayout;import androID.vIEw.VIEw;import androID.Widget.AdapterVIEw;import androID.Widget.AdapterVIEw.OnItemClickListener;import androID.Widget.ArrayAdapter;import androID.Widget.ListVIEw;import com.actionbarsherlock.app.SherlockFragmentActivity;import com.actionbarsherlock.vIEw.MenuItem;import com.mejorandroID.ejemplo3_clase3.fragments.MainFragment;import com.mejorandroID.ejemplo3_clase3.fragments.TermsFragment;public class MainActivity extends SherlockFragmentActivity                          implements OnItemClickListener{    private ListVIEw drawer_List;    private DrawerLayout drawer_layout;    private ActionbarDrawerToggle drawer_toggle;    private PullToRefreshAttacher pullToRefreshAttacher;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        pullToRefreshAttacher = PullToRefreshAttacher.get(this);        drawer_List = (ListVIEw) findVIEwByID(R.ID.left_drawer);        drawer_layout = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);        ArrayAdapter<String> drawer_adapter = new ArrayAdapter<String>(this,R.layout.drawer_List_item,getResources().getStringArray(                        R.array.array_drawer_options));        drawer_List.setAdapter(drawer_adapter);        drawer_List.setonItemClickListener(this);        selectItem(0);        drawer_toggle = new ActionbarDrawerToggle(this,drawer_layout,R.drawable.ic_drawer,R.string.drawer_open,R.string.drawer_close){            @OverrIDe            public voID onDrawerClosed(VIEw drawerVIEw) {                supportInvalIDateOptionsMenu();            }            @OverrIDe            public voID onDrawerOpened(VIEw drawerVIEw) {                supportInvalIDateOptionsMenu();            }        };        drawer_layout.setDrawerListener(drawer_toggle);        getSherlock().getActionbar().setdisplayHomeAsUpEnabled(true);        getSherlock().getActionbar().setHomebuttonEnabled(true);    }    @OverrIDe    protected voID onPostCreate(Bundle savedInstanceState) {        super.onPostCreate(savedInstanceState);        drawer_toggle.syncState();    }    @OverrIDe    public voID onConfigurationChanged(Configuration newConfig) {        super.onConfigurationChanged(newConfig);        drawer_toggle.onConfigurationChanged(newConfig);    }    @OverrIDe    public boolean onoptionsItemSelected(MenuItem item) {        if(item.getItemID() == androID.R.ID.home || drawer_toggle.onoptionsItemSelected(item)){            return true;        }        return super.onoptionsItemSelected(item);    }    @OverrIDe    public boolean onCreateOptionsMenu(com.actionbarsherlock.vIEw.Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getSupportMenuInflater().inflate(R.menu.main,menu);        return true;    }    public PullToRefreshAttacher getAttacher() {        return pullToRefreshAttacher;    }    public voID selectItem(int position){        Fragment f;        if(position==0){            f = new MainFragment();        }else{            f = new TermsFragment();        }        FragmentManager fm = getSupportFragmentManager();        fm.beginTransaction()          .replace(R.ID.main_content,f)          .commit();        drawer_List.setItemChecked(position,true);        setTitle(drawer_List.getItemAtposition(position).toString());        drawer_layout.closeDrawer(drawer_List);    }    @OverrIDe    public voID onItemClick(AdapterVIEw<?> arg0,VIEw arg1,int position,long arg3) {        selectItem(position);    }}

在onoptionsItemSelected的方法中:

@OverrIDe    public boolean onoptionsItemSelected(MenuItem item) {        if(item.getItemID() == androID.R.ID.home || drawer_toggle.onoptionsItemSelected(item)){            return true;        }        return super.onoptionsItemSelected(item);    }

我得到:

如何解决这个问题?

这是我的main.xml:

<androID.support.v4.Widget.DrawerLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:ID="@+ID/drawer_layout"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent">    <FrameLayout         androID:ID="@+ID/main_content"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"/>    <ListVIEw         androID:ID="@+ID/left_drawer"        androID:layout_wIDth="240dp"        androID:layout_height="match_parent"        androID:layout_gravity="start"        androID:choiceMode="singleChoice"        androID:divIDer="@androID:color/@R_403_3318@"        androID:divIDerHeight="0dp"        androID:background="#111"/></androID.support.v4.Widget.DrawerLayout>
@H_404_16@解决方法 我有同样的问题,这对我有用:
@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) {    if (item.getItemID() == androID.R.ID.home) {        if (drawer_layout.isDrawerOpen(drawer_List)) {            drawer_layout.closeDrawer(drawer_List);        } else {            drawer_layout.openDrawer(drawer_List);        }    }    return super.onoptionsItemSelected(item);}

我不知道在最新版本的Sherlock库/支持库中是否已经解决了这个问题,但解决方法做得很好.

@H_404_16@ @H_404_16@ 总结

以上是内存溢出为你收集整理的Android – ActionBarSherlock侧面菜单导航抽屉全部内容,希望文章能够帮你解决Android – ActionBarSherlock侧面菜单导航抽屉所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1131943.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存