Android – ActionBarSherlock侧面菜单导航抽屉

前端之家收集整理的这篇文章主要介绍了Android – ActionBarSherlock侧面菜单导航抽屉前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在动作栏中使用抽屉切换.这是我的主要活动:
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/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>

</android.support.v4.widget.DrawerLayout>

解决方法

我有同样的问题,这对我有用:
@Override
public 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库/支持库中是否已经解决了这个问题,但解决方法做得很好.

原文链接:https://www.f2er.com/android/311368.html

猜你在找的Android相关文章