问题描述:

I have a list item of strings, and would like to assiate each item in the NavigationDrawer with an icon.

Below is the list item is called:

OptionMenu = new String[] { "Discover activities", "Matches", "City selection", "Preferences", "Contact Dooba" };

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

mDrawerRelativeLayout = (RelativeLayout)

findViewById(R.id.left_drawer);

mDrawerList = (ListView) findViewById(R.id.list_view_drawer);

mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,

mOptionMenu));

mDrawerList.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {

Fragment fragment = null;

switch (position) {

case 0:

fragment = new FirstFragment();

break;

case 1:

fragment = new SecondFragment();

break;

case 2:

fragment = new ThirdFragment();

break;

}

FragmentManager fragmentManager = getSupportFragmentManager();

fragmentManager.beginTransaction()

.replace(R.id.content_frame, fragment).commit();

mDrawerList.setItemChecked(position, true);

mTitleSection = mOptionMenu[position];

getSupportActionBar().setTitle(mTitleSection);

mDrawerLayout.closeDrawer(mDrawerRelativeLayout);

}

});

mDrawerList.setItemChecked(0, true);

mTitleSection = getTitle();

mTitleApp = getTitle();

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,

R.drawable.logo8, R.string.drawer_open,

R.string.drawer_close) {

public void onDrawerClosed(View view) {

getSupportActionBar().setTitle(mTitleSection);

ActivityCompat.invalidateOptionsMenu(MainActivity.this);

}

public void onDrawerOpened(View drawerView) {

getSupportActionBar().setTitle(mTitleSection);

ActivityCompat.invalidateOptionsMenu(MainActivity.this);

}

};

mDrawerLayout.setDrawerListener(mDrawerToggle);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

getSupportActionBar().setHomeButtonEnabled(true);

getSupportActionBar().setDisplayUseLogoEnabled(true);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main_activity_actions, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

if (mDrawerToggle.onOptionsItemSelected(item)) {

return true;

}

switch (item.getItemId()) {

case R.id.action_settings:

Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();

;

break;

default:

return super.onOptionsItemSelected(item);

}

return true;

}

@Override

protected void onPostCreate(Bundle savedInstanceState) {

super.onPostCreate(savedInstanceState);

mDrawerToggle.syncState();

}

@Override

public void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

mDrawerToggle.onConfigurationChanged(newConfig);

}

I am also wondering how I could add a number next to an item list string. For example, Matches (number 22)

Any assistance would be greatly appreciated.

网友答案:

Write custom adapter and inflate list with the layout having imageview and textview.

<?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="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_gravity="center"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="TextView" />

</LinearLayout>


public class CustomAdapter extends BaseAdapter{ 
    String [] result;
    Context context;
 int [] imageId;
      private static LayoutInflater inflater=null;
    public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
        // TODO Auto-generated constructor stub
        result=prgmNameList;
        context=mainActivity;
        imageId=prgmImages;
         inflater = ( LayoutInflater )context.
                 getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

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

    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;        
             rowView = inflater.inflate(R.layout.program_list, null);
             holder.tv=(TextView) rowView.findViewById(R.id.textView1);
             holder.img=(ImageView) rowView.findViewById(R.id.imageView1);       
         holder.tv.setText(result[position]);
         holder.img.setImageResource(imageId[position]);         
         rowView.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
            }
        });  
        return rowView;
    }

}
网友答案:

Using custom adapter and layout is a good solution, but if all you want is to add an icon, you can use setCompoundDrawablesWithIntrinsicBounds wich does add up to 4 drawables to a TextView.

You only have to override the getView method of the ArrayAdapter you already use :

    String[] mOptionMenu = new String[]{"Discover activities", "Matches", "City selection", "Preferences", "Contact Dooba"};

    int[] mOptionMenuDrawables = new int[]{ ... same number of drawables resources... };

    ...

    mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mOptionMenu) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView v = (TextView) super.getView(position, convertView, parent);
            v.setCompoundDrawablesWithIntrinsicBounds(mOptionMenuDrawables[position], 0, 0, 0);
            return v;
        }
    });
相关阅读:
Top