我有一个导航抽屉。我的问题是:我无法弄清楚如何将文本设置为粗体。它总是第一个被选中的项目(粗体)。
Ps:在我的ListView中,我添加了一个标题。
在我的活动课程中:
// Setting the nav drawer list adapter
NavMainMenuAdapter adapter = new NavMainMenuAdapter(this,
R.layout.list_item_mainmenu,
items);
drawerList.setAdapter(adapter);
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
View rowView = view;
TextView tv = (TextView) rowView.findViewById(R.id.textListItemMainMenuTitle);
//tv.setTypeface(null, Typeface.BOLD);
displayView(view, position, tv.getText());
((NavMainMenuAdapter)((HeaderViewListAdapter)adapterView.getAdapter()).getWrappedAdapter()).selectItem(position);
}
});
我的NavMainMenuAdapter类:
public class NavMainMenuAdapter extends ArrayAdapter<NavMainMenuItem> {
private Context context;
private int mSelectedItem;
public NavMainMenuAdapter(Context context, int resource, ArrayList<NavMainMenuItem> items) {
super(context, resource, items);
}
public void selectItem(int selectedItem){
mSelectedItem = selectedItem;
notifyDataSetChanged();
}
// ....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_mainmenu, parent, false);
}
// Get the data item for this position
NavMainMenuItem item = getItem(position);
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.imageListItemMainMenuPicture);
TextView txtTitle = (TextView) convertView.findViewById(R.id.textListItemMainMenuTitle);
TextView txtCount = (TextView) convertView.findViewById(R.id.textListItemMainMenuCounter);
imgIcon.setImageResource(item.getIcon());
txtTitle.setText(item.getTitle());
// displaying count
// check whether it set visible or not
if(item.getCounterVisibility()){
txtCount.setText(item.getCount());
}else{
// hide the counter view
txtCount.setVisibility(View.GONE);
}
txtTitle.setTypeface(null, position == mSelectedItem ? Typeface.BOLD : Typeface.NORMAL);
return convertView;
}
答案 0 :(得分:0)
在适配器“getView()
方法
txtTitle.setTypeface(null, position == mSelectedItem ? Typeface.BOLD : Typeface.NORMAL);
用
替换上面一行txtTitle.setTypeface(txtTitle.getTypeface (), position == mSelectedItem ? Typeface.BOLD : Typeface.NORMAL);
希望它有效。
答案 1 :(得分:0)
好的,我明白为什么会有这种行为:当我在抽屉菜单中选择一个项目时,我会开始一个新的活动。每个Activity都扩展了一个包含菜单抽屉管理的基本活动。所以我有4个活动,所以我有4个菜单抽屉(等4个适配器);我的变量mSelectedItem在当前的Activity中更新。接下来显示,不会更新mSelected变量(保持为0)。这就是为什么它始终是我菜单中以粗体模式显示的第一项。