Set bold text in navigation drawer just after creating it (before any selection)

自闭症网瘾萝莉.ら 提交于 2020-01-02 17:48:30

问题


I have a navigation drawer like those from Google apps(Play Music, Play Movies etc..)

Everything works just fine; my only problem is: I can't figure out how to set the text to BOLD, just after creating the drawer and before any selection or click is done!
I have no problem setting bold after the onItemClick is performed, but how to do it just after creating the drawer?
I've tried to get the View reference from the list inside the drawer, but it doesn't work.

Can someone help me?

that's my navigation drawer code

        mNavDrawerStrings = getResources().getStringArray(R.array.action_bar_list);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_navigation_drawer);

    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.navigation_drawer_list_item, mNavDrawerStrings));

    mDrawerList.setItemChecked(0, true);
    mDrawerList.setSelection(0);

    NavigationDrawerItemClickListener navDrawerClickListener
        = new NavigationDrawerItemClickListener(this, mDrawerList, mDrawerLayout); 

    mDrawerList.setOnItemClickListener(navDrawerClickListener);

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

    mDrawerLayout.setDrawerShadow
        (R.drawable.drawer_shadow, GravityCompat.START);

    mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawerLayout);
    mDrawerLayout.setDrawerListener(mDrawerToggle);

回答1:


You need to preserve the selection so the selected item stays selected even after it goes outside the screen. For changing text color and item background you could use Color / Drawable State List Resource http://developer.android.com/guide/topics/resources/color-list-resource.html

However if you want to do more than that you would need to provide a custom adapter. Something like this:

public class MyArrayAdapter extends ArrayAdapter<String>{

    private int selectedItem;

    public MyArrayAdapter(Context context, int resource, String[] objects) {
        super(context, resource, objects);
    }

    public void selectItem(int selectedItem){
        this.selectedItem = selectedItem;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = super.getView(position, convertView, parent);
        ((TextView)convertView).setTypeface(null, position == selectedItem ? Typeface.BOLD : Typeface.NORMAL);

        return convertView;
    }
}

and in onItemClick():

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) {
    ((MyArrayAdapter)adapterView.getAdapter()).selectItem(position);
}



回答2:


Very simple you can do this on onItemSelected method of the ListView. Just use this piece of code to make the text bold.

public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
View rowView = view
TextView tv = (TextView) rowView.findViewById(R.id.label);
tv.setTypeface(null, Typeface.BOLD);
}});


来源:https://stackoverflow.com/questions/21333955/set-bold-text-in-navigation-drawer-just-after-creating-it-before-any-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!