문제

I'm developing an Android app with Sliding Menu and ActionBar Sherlock and I'm lost.

I have followed this Spanish tutorial to add them to my project.

This is my code:

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;

public class MainActivity extends SlidingFragmentActivity
{
    /**
     * Used as TAG on Log messages.
     */
    public static final String TAG = "MainActivity";
    private SlidingMenu menu;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Generate keys to encrypt and decrypt user password.
        generateCryptoKeys();

        setBehindContentView(R.layout.menu);

        setSlidingActionBarEnabled(false);        

        menu = new SlidingMenu(this);
        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);

        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

And this is menu fragment:

public class MenuFragment extends ListFragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.menu_list, null);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SampleAdapter adapter = new SampleAdapter(getActivity());
        for (int i = 0; i < 20; i++) {
            adapter.add(new SampleItem("Sample List", android.R.drawable.ic_menu_search));
        }
        setListAdapter(adapter);
    }

    private class SampleItem {
        public String tag;
        public int iconRes;
        public SampleItem(String tag, int iconRes) {
            this.tag = tag; 
            this.iconRes = iconRes;
        }
    }

    public class SampleAdapter extends ArrayAdapter<SampleItem> {

        public SampleAdapter(Context context) {
            super(context, 0);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.menu_row, null);
            }
            ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
            icon.setImageResource(getItem(position).iconRes);
            TextView title = (TextView) convertView.findViewById(R.id.row_title);
            title.setText(getItem(position).tag);

            return convertView;
        }
    }
}

I have a couple of questions:

1. Do I have to use fragments?
2. Why I can't open the menu when I click on home button?
See the following image:

enter image description here

To integrate Action Bar Sherlock with Sliding menu I have done this:

Go into the SlidingActivities that you plan on using make them extend Sherlock___Activity instead of ___Activity.

And, when I to do this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        toggle();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

I get this error:

Cannot override the final method from SherlockFragmentActivity.

도움이 되었습니까?

해결책 2

  1. Do I have to use fragments?

Yes, the library is built for it. In addtion, no two Activity can be in the foreground at the same time.

2 . Why I can't open the menu when I click on home button?

This is because that button isn't intend to open the sliding menu.

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

will enable the app icon as a back button. This goes back from the stack. See also Navigation . Your slindermenu is a fragment thats is put on the side of your Activity, hence they exsit at the same moment, hence not in its not related to any navigation.

To open the menu, you need to call :

 SlidingMenu sm = getSlidingMenu();
     sm.toggle();//or .open();

if you want to override onOptionsItemSelected , you need:

extends SlidingFragmentActivity

Modify the sliding menu library. Have ALL the Fragments extend from the sherlock library related fragments. (btw this is written in the docs under Setup with ActionBarSherlock.).

다른 팁

Use somthing like that :

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        menu.toggle();

        return true;
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top