如何正确实现SlideMenu和ActionbarSherlock

时间:2013-04-22 10:51:52

标签: android actionbarsherlock

美好的一天,我正在尝试在我的应用程序中使用ListView布局和ActionBarSherlock实现jfeinstein幻灯片菜单,出于某种原因,我似乎无法在SlideMenu上正确实现。根据我的理解,当使用带有ActionBarSherlock的SlideMenu库时,您必须在计划使用的SlideMenu活动中扩展到SherlockActivity。

这是我的实现:我将在此处仅发布相关代码。

 public class MyActivity extends SlidingFragmentActivity {
ListView slidinglist;
private SlidingAdapter SlidingAdapter;

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

          getSupportActionBar().setDisplayShowTitleEnabled(false);

            //Enable home button
            getSupportActionBar().setHomeButtonEnabled(true);

            //Home as up display
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            /*get array from the slidemenu items in value folder */
            String[] text_items = getResources().getStringArray(R.array.sliding_menus);
            TypedArray imgs = getResources().obtainTypedArray(R.array.sliding_menu_images);

            final Context context=this;
            LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v=inflater.inflate(R.layout.menu_layout, null, true);

            slidinglist = (ListView)v.findViewById(R.id.slidemenu_listView);
            SlidingAdapter = new SlidingAdapter(this, R.layout.slide_menu_item, text_items, imgs);
            slidinglist.setAdapter(SlidingAdapter);

        SlidingMenu menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setFadeEnabled(true);
        menu.setFadeDegree(0.35f);
        menu.setBehindOffset(100);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        //menu.setMenu(R.layout.menu_layout);
        menu.setMenu(v);


    }

menu_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/slidemenu_listView"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >
    </ListView>


</LinearLayout>

适配器类:

public class SlidingAdapter extends ArrayAdapter<String> {
         Context mContext;
         private int ViewResourceId; 
         private String[] mstring;
         TypedArray icons;

        public SlidingAdapter(Context context, int resourceId, String[] items, TypedArray images ){
            super(context, resourceId, items);

            mContext = context;
            ViewResourceId = resourceId;
            mstring = items;
            icons = images;
        }

        @Override
        public int getCount(){
            Log.d(TAG, "lenght of string is" + mstring.length);
            return mstring.length;
        }

        @Override
        public String getItem(int position){
            return mstring[position];
        }

        @Override
        public long getItemId(int position){
            return position;
        }

        @Override
        public View getView (int position, View convertView, ViewGroup parent){
            ViewHolder holder;

            if(convertView == null){
                LayoutInflater inflater = LayoutInflater.from(mContext);
                convertView = inflater.inflate(ViewResourceId, null);
                holder = new ViewHolder();


              holder.imageview = (ImageView)convertView.findViewById(R.id.slidemenu_image_item);
              holder.text_title = (TextView)convertView.findViewById(R.id.slidemenu_textview_id);
              holder.arrowImageView = (ImageView)convertView.findViewById(R.id.slidemenu_arrow_id);

              convertView.setTag(holder);

            }
            else{
                holder = (ViewHolder)convertView.getTag();
            }

            //set Array text values defined in xml to textview
            String title = mstring[position];
            holder.text_title.setText(title);

            //set Array icons values defined in xml to textview
            holder.imageview.setImageDrawable(icons.getDrawable(position));

            return convertView;
        }


    }

     class ViewHolder {
        TextView text_title;
        ImageView imageview;
        ImageView arrowImageView;
    }

我在幻灯片菜单中看到一个空白屏幕。我能做错什么?

2 个答案:

答案 0 :(得分:1)

试试这段代码。它对我有用。

public class MainFragmentActivity extends SlidingFragmentActivity {

    private ActionBar ab;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fragment);
        setBehindContentView(R.layout.side_menu);

        Display display = getWindowManager().getDefaultDisplay();
        final int width = display.getWidth();

        ListView mMenuLateral = (ListView) findViewById(R.id.menu_lateral);
        mMenuLateral.setAdapter(new MenuLateralAdapter(getApplicationContext(), vibraCallback));
        mMenuLateral.setDivider(getResources().getDrawable(R.color.divider));
        mMenuLateral.setSelector(android.R.color.transparent);
        mMenuLateral.setOnItemClickListener(new OnItemClickListener() {
             ...
        }

        ab = getSupportActionBar();
        ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM|ActionBar.DISPLAY_SHOW_HOME); 
        ab.setCustomView(actionBarCustomView);
        ab.setIcon(R.drawable.action_bar_icon_main);
        ab.setHomeButtonEnabled(true);

        menu = getSlidingMenu();
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        menu.setShadowWidth(35);
        menu.setShadowDrawable(R.drawable.shadow);
        menu.setBehindWidth(width * 3 / 4);
    }
}

希望有所帮助,欢呼!

答案 1 :(得分:0)

您好需要将您的活动扩展到Sherlock然后实施SlidingMenu

试试这个:

https://stackoverflow.com/a/14126158/1909953