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

≡放荡痞女 提交于 2019-12-06 09:54:30

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

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