9

シャーロックのアクションバーでドロワートグルを使用しようとしています。これが私の主な活動です:

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>
4

1 に答える 1

35

私は同じ問題を抱えていましたが、これは私にとってはうまくいきます:

@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 ライブラリ / サポート ライブラリの最新リリースで既に修正されているかどうかはわかりませんが、回避策はうまく機能します。

于 2013-10-07T17:11:39.180 に答える