代码之家  ›  专栏  ›  技术社区  ›  irobotxx

如何正确实现SlideMenu和ActionbarSherlock

  •  0
  • irobotxx  · 技术社区  · 11 年前

    您好,我正在尝试在我的应用程序中使用ListView布局和ActionBarSherlock实现jfeinstein幻灯片菜单,但由于某种原因,我似乎无法在幻灯片菜单上正确实现。根据我的理解,当将SlideMenu库与ActionBarSherlock一起使用时,您必须在计划使用的SlideMenu Activity中扩展到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):

    <?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 回复  |  直到 11 年前
        1
  •  1
  •   Adrián Rodríguez    11 年前

    试试这个代码。它对我有效。

    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);
        }
    }
    

    希望有帮助,欢呼!

        2
  •  0
  •   Community Mr_and_Mrs_D    7 年前

    嗨,你需要将你的活动扩展到夏洛克,然后实现SlidingMenu

    试试这个:

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