如何在slidemenu listview中添加不同的图标

时间:2013-03-20 12:17:22

标签: android android-listview android-arrayadapter

我正在使用来自github的slidemenu示例,它只显示带有文本和标题的幻灯片菜单,但我需要添加不同的图标。我正在尝试使用自定义适配器,但它无法正常工作......

slidemenu.java:

public class SlideMenu implements SlideMenuAnimationContainer.Listener {

    public final static String[] slideMenuOptions = { "Recent Update", "Notification", "Wardrobe" };
    public final static String TAG = "SlideMenu";
    protected static final String TextView = null;
    Intent intent;

    private Context context;
    private SlideMenuAnimationContainer slideMenuAnimationContainer;

    public SlideMenu( Context context, SlideMenuAnimationContainer mainAnimationLayout){

        this.context = context;
        this.slideMenuAnimationContainer = mainAnimationLayout;
    }

    public void init(){

        final Activity activity = (Activity) context;

        //Set Content's show menu button's action
        ImageView showMenuButton = (ImageView) activity.findViewById( R.id.content_button);
        showMenuButton.setOnClickListener( new OnClickListener(){

            @Override
            public void onClick( View v){

                slideMenuAnimationContainer.toggleSlideMenu();
            }
        });


        //Contruct the SlideMenu items here, you can replace this ArrayAdapter with your customr ArrayAdapter
        List<SlideMenuItem> slideMenuList = new ArrayList<SlideMenuItem>();
        slideMenuList.add( new SlideMenuItem( SlideMenuItemType.Header, "Updates"));
        slideMenuList.add( new SlideMenuItem( SlideMenuItemType.Activity, "Recent Update"));
    //  slideMenuList.add( new SlideMenuItem( SlideMenuItemType.Header, "Header 2"));
        slideMenuList.add( new SlideMenuItem( SlideMenuItemType.Activity, "Notification"));
        slideMenuList.add( new SlideMenuItem( SlideMenuItemType.Header, "Category"));
        slideMenuList.add( new SlideMenuItem( SlideMenuItemType.Activity, "Wardrobe"));
        slideMenuList.add( new SlideMenuItem( SlideMenuItemType.Activity, "Gadgets"));

        SlideMenuArrayAdapter slideMenuArrayAdaptar = new SlideMenuArrayAdapter( context, android.R.layout.simple_list_item_1, slideMenuList);

        ListView menuListView = (ListView) activity.findViewById( R.id.slideMenuListView);
        menuListView.setAdapter( slideMenuArrayAdaptar);
        menuListView.setOnItemClickListener( new OnItemClickListener(){

            @Override
            public void onItemClick( AdapterView<?> parent, View view, int position, long id){

                // Close sidebar
                slideMenuAnimationContainer.closeSlideMenuAndActOnClick( parent, view, position, id);
            }
        });

        //Implement your logic here within the execute() function when an item is clicked with in the SlideMenu
        //Called after the SlideMenu collapses.
        slideMenuAnimationContainer.setMenuItemSelectedAction( new MenuItemSelectedAction(){

            @Override
            public void execute( AdapterView<?> parent, View view, int position, long id){

                //Only act when a ListViewItem with type Activity is clicked
                TextView textView = (TextView)view.findViewById( R.id.MenuActivityListViewItem_text);
                if( textView != null && textView.getId() == R.id.MenuActivityListViewItem_text){

                    //Start new activity
                    CharSequence selectedActivityName = textView.getText();


                    if( selectedActivityName.equals( "Recent Update")) {
                        intent = new Intent( activity, Activity1.class);
                    }
                    else if( selectedActivityName.equals( "Notification")) {
                        intent = new Intent( activity, Activity2.class);
                    }
                    else if( selectedActivityName.equals( "Wardrobe")) {
                        intent = new Intent( activity, Activity3.class);
                    }
                    else if( selectedActivityName.equals( "Gadgets")) {
                        intent = new Intent( activity, Gadgets_Activity.class);
                    }
                     intent =intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    activity.startActivity( intent);
                }
            }
        });
    }

    //Callback of SlideMenuAnimationContainer.Listener to monitor status of SlideMenu
    @Override
    public void onSlideMenuOpened(){
        Log.d( TAG, "opened");
    }

    //Callback of SlideMenuAnimationContainer.Listener to monitor status of SlideMenu
    @Override
    public void onSlideMenuClosed(){
        Log.d( TAG, "closed");
    }

    //Callback of SlideMenuAnimationContainer.Listener to monitor status of SlideMenu
    @Override
    public boolean onContentTouchedWhenOpening(){
        //The content area is touched when sidebar opening, close sidebar
        Log.d( TAG, "going to close sidebar");
        slideMenuAnimationContainer.closeSlideMenu();
        return true;
    }
}

和适配器:

public class SlideMenuArrayAdapter extends ArrayAdapter<SlideMenuItem> {

    private List<SlideMenuItem> slideMenuItemList;

    public SlideMenuArrayAdapter( Context context, int textViewResourceId, List<SlideMenuItem> slideMenuItemList) {

        super( context, textViewResourceId, slideMenuItemList);
        this.slideMenuItemList = slideMenuItemList;            
    }

    @Override
    public View getView( int position, View view, ViewGroup parent) {

        SlideMenuItem slideMenuItem = slideMenuItemList.get( position);
        LayoutInflater layoutInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if( slideMenuItem.getSlideMenuItemType() == SlideMenuItemType.Header){

            view = layoutInflater.inflate( R.layout.menu_header_listview_item, null);          

            TextView textView = (TextView) view.findViewById( R.id.MenuListHeaderListViewItem_text);           
            textView.setText( slideMenuItem.getText());

        }
        else if( slideMenuItem.getSlideMenuItemType() == SlideMenuItemType.Activity){
            view = layoutInflater.inflate( R.layout.menu_activity_listview_item, null);            

            TextView textView = (TextView) view.findViewById( R.id.MenuActivityListViewItem_text);                  
            textView.setText( slideMenuItem.getText());
        }
        else{
            view = null;    //Left for operation 
        }

        return view;
    }    
}

1 个答案:

答案 0 :(得分:0)

试试这个

 ListView listView = (ListView) menu.findViewById(R.id.list);

             ArrayList<String[]> datavalues=new ArrayList<String[]>();

             String[] menuhome=new String[2];
             menuhome[0]=""+R.drawable.home_sidebar;
             menuhome[1]="Home";
             datavalues.add(menuhome);

             String[] menuprofile=new String[2];
             menuprofile[0]=""+R.drawable.profile_sidebar;
             menuprofile[1]="Profile";
             datavalues.add(menuprofile);

             String[] menusearch=new String[2];
             menusearch[0]=""+R.drawable.search_sidebar;
             menusearch[1]="Search";
             datavalues.add(menusearch);

             String[] menugallery=new String[2];
             menugallery[0]=""+R.drawable.gallery_sidebar;
             menugallery[1]="Gallery";
             datavalues.add(menugallery);

             String[] menulogout=new String[2];
             menulogout[0]=""+R.drawable.logout_sidebar;
             menulogout[1]="Logout";
             datavalues.add(menulogout);

           SlideMenuAdapter menuadapter=new SlideMenuAdapter(datavalues, this);
           listView.setAdapter(menuadapter);




private class SlideMenuAdapter extends BaseAdapter{
                ArrayList<String[]> data;
                ActivityListing context;
                 public SlideMenuAdapter(ArrayList<String[]> datavalues,
                         ActivityListing contextobj) {
                    // TODO Auto-generated constructor stub
                     data=datavalues;
                     context=contextobj;
                }
                @Override
                public int getCount() {
                    // TODO Auto-generated method stub
                    return data.size();
                }

                @Override
                public Object getItem(int arg0) {
                    // TODO Auto-generated method stub
                    return data.get(arg0);
                }

                @Override
                public long getItemId(int arg0) {
                    // TODO Auto-generated method stub
                    return arg0;
                }

                @Override
                public View getView(int arg0, View arg1, ViewGroup arg2) {
                    // TODO Auto-generated method stub
                     final int pos=arg0;
                        // This a new view we inflate the new layout
                        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        View convertView = inflater.inflate(R.layout.menurow, null, false);
                        int drawable=Integer.parseInt(data.get(arg0)[0].trim());
                        Button btnOption=(Button) convertView.findViewById(R.id.btnoption);
                        btnOption.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(drawable), null, null, null);
                        btnOption.setText(data.get(arg0)[1].trim());
                        btnOption.setTextColor(Color.rgb(143, 7, 0));

                        btnOption.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View arg0) {
                                // TODO Auto-generated method stub
                                callOptions(data.get(pos));
                            }


                        });

                    return convertView;
                }