如何在导航抽屉中的标题上添加图像

时间:2014-10-04 17:54:20

标签: android navigation-drawer slidingmenu

我正在使用Gabriele Mariotti导航抽屉库来创建滑动菜单。它工作得很完美,但我正面临添加图像,作为标题。看起来应该是这样的:

enter image description here

我的意思是......不完全相同,但将图像作为标题的想法。谢谢!

MainActivity:

package ro.volunteer;



import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;

public class NewsFeed extends Activity implements OnClickListener {

    private ListView mDrawerList;
    private DrawerLayout mDrawer;
    private CustomActionBarDrawerToggle mDrawerToggle;
    private String[] menuItems;


    MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newsfeed);


        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        // set a custom shadow that overlays the main content when the drawer
        // opens
        mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        _initMenu();
        mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
        mDrawer.setDrawerListener(mDrawerToggle);
    }


    private void _initMenu() {
        NsMenuAdapter mAdapter = new NsMenuAdapter(this);



        menuItems = getResources().getStringArray(
                R.array.ns_menu_items_icon);


        // Add Header
        mAdapter.addHeader(R.string.ns_menu_main_header);

        // Add first block

        menuItems = getResources().getStringArray(
                R.array.ns_menu_items);
        String[] menuItemsIcon = getResources().getStringArray(
                R.array.ns_menu_items_icon);

        int res = 0;
        for (String item : menuItems) {

            int id_title = getResources().getIdentifier(item, "string",
                    this.getPackageName());
            int id_icon = getResources().getIdentifier(menuItemsIcon[res],
                    "drawable", this.getPackageName());

            NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
            if (res==1) mItem.counter=0; //it is just an example...
            if (res==3) mItem.counter=0; //it is just an example...
            mAdapter.addItem(mItem);
            res++;
        }

        mAdapter.addHeader(R.string.ns_menu_main_header2);

        menuItems = getResources().getStringArray(
                R.array.ns_menu_items2);
        String[] menuItemsIcon1 = getResources().getStringArray(
                R.array.ns_menu_items_icon3);

        int res2 = 0;
        for (String item : menuItems) {

            int id_title = getResources().getIdentifier(item, "string",
                    this.getPackageName());
            int id_icon = getResources().getIdentifier(menuItemsIcon1[res2],
                    "drawable", this.getPackageName());

            NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
            if (res2==1) mItem.counter=0; //it is just an example...
            mAdapter.addItem(mItem);
            res2++;
        }

        mDrawerList = (ListView) findViewById(R.id.drawer);
        if (mDrawerList != null)
            mDrawerList.setAdapter(mAdapter);

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    }


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }




    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        /*
         * The action bar home/up should open or close the drawer.
         * ActionBarDrawerToggle will take care of this.
         */
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        // Handle your other action bar items...
        return super.onOptionsItemSelected(item);
    }

    private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle {

        public CustomActionBarDrawerToggle(Activity mActivity,DrawerLayout mDrawerLayout){
            super(
                mActivity,
                mDrawerLayout, 
                R.drawable.ic_drawer,
                R.string.ns_menu_open, 
                R.string.ns_menu_close);
        }

        @Override
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(getString(R.string.ns_menu_close));
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(getString(R.string.ns_menu_open));
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mDrawerList.setItemChecked(position, true);         
            mDrawer.closeDrawer(mDrawerList);
            if (position == 1) {
                Intent intent = new Intent(NewsFeed.this, Profile.class);
                startActivity(intent);
                mDrawer.closeDrawers();
            }


        }



    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
        }

适配器:

 package ro.volunteer;


    import android.content.Context;
    import android.graphics.Color;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

    public class NsMenuAdapter extends ArrayAdapter<NsMenuItemModel> {

        /*
         * public NsMenuAdapter(Context context, int resource, int
         * textViewResourceId, String[] objects) { super(context,
         * R.layout.ns_menu_row, textViewResourceId, objects); }
         */

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

        public void addHeader(int title) {
            add(new NsMenuItemModel(title, -1, true));
        }

        public void addItem(int title, int icon) {
            add(new NsMenuItemModel(title, icon, false));
        }

        public void addItem(NsMenuItemModel itemModel) {
            add(itemModel);

        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            return getItem(position).isHeader ? 0 : 1;
        }

        @Override
        public boolean isEnabled(int position) {
            return !getItem(position).isHeader;
        }

        public static class ViewHolder {
            public final TextView textHolder;
            public final ImageView imageHolder;
            public final TextView textCounterHolder;

            public ViewHolder(TextView text1, ImageView image1,TextView textcounter1) {
                this.textHolder = text1;
                this.imageHolder = image1;
                this.textCounterHolder=textcounter1;
            }
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            NsMenuItemModel item = getItem(position);
            ViewHolder holder = null;
            View view = convertView;

            if (view == null) {
                int layout = R.layout.ns_menu_row_counter;
                if (item.isHeader)
                    layout = R.layout.ns_menu_row_header;

                view = LayoutInflater.from(getContext()).inflate(layout, null);

                TextView text1 = (TextView) view.findViewById(R.id.menurow_title);
                text1.setTextColor(Color.WHITE);

                ImageView image1 = (ImageView) view.findViewById(R.id.menurow_icon);
                TextView textcounter1 = (TextView) view.findViewById(R.id.menurow_counter);

                view.setTag(new ViewHolder(text1, image1,textcounter1));
            }

            if (holder == null && view != null) {
                Object tag = view.getTag();
                if (tag instanceof ViewHolder) {
                    holder = (ViewHolder) tag;
                }
            }


            if(item != null && holder != null)
            {
                if (holder.textHolder != null)
                    holder.textHolder.setText(item.title);

                if (holder.textCounterHolder != null){
                    if (item.counter > 0){
                        holder.textCounterHolder.setVisibility(View.VISIBLE);
                        holder.textCounterHolder.setText(""+item.counter);
                    }else{
                        holder.textCounterHolder.setVisibility(View.GONE);
                    }
                }

                if (holder.imageHolder != null) {
                    if (item.iconRes > 0) {
                        holder.imageHolder.setVisibility(View.VISIBLE);
                        holder.imageHolder.setImageResource(item.iconRes);
                    } else {
                        holder.imageHolder.setVisibility(View.GONE);
                    }
                }
            }

            return view;        
        }

    }



**ItemModel:**


package ro.volunteer;


public class NsMenuItemModel {

    public int title;
    public int iconRes;
    public int counter;
    public boolean isHeader;

    public NsMenuItemModel(int title, int iconRes,boolean header,int counter) {
        this.title = title;
        this.iconRes = iconRes;
        this.isHeader=header;
        this.counter=counter;
    }

    public NsMenuItemModel(int title, int iconRes,boolean header){
        this(title,iconRes,header,0);
    }

    public NsMenuItemModel(int title, int iconRes) {
        this(title,iconRes,false);
    }



}

0 个答案:

没有答案