- 论坛徽章:
- 0
|
ListView 选中字体变大
目标:listview中item使用textview,当item选中时,字体为25px;当item未选中时,字体21px
之前想了很久,以为同listview选中行字体颜色一样,使用xml文件中使用selector就可以改变了,但是一直上网查找资料,都没有找到selector中改变字体大小的命令。后来网友提醒我,可以在adapter中的getview中修改。现将重要的代码放上:
view sourceprint?
1 //全局变量,记录选中的item
2 public static int select_item = -1;
先使用全局变量记录选中的item,然后在listview的OnItemSelectedListener中实时更新选中的item,并且使用adapter.notifyDataSetChanged()刷新listview数据。- view sourceprint?
- 01 listview_listMenu.setOnItemSelectedListener(new OnItemSelectedListener(){
- 02 @Override
- 03 public void onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3){
- 04 select_item = arg2; //当前选择的节目item
- 05
- 06 listAdapter.notifyDataSetChanged(); //通知adapter刷新数据
- 07 }
- 08
- 09 public void onNothingSelected(AdapterView<?> arg0) {
- 10 }
- 11 });
复制代码 接着在adapter的getview中修改字体大小。- view sourceprint?
- 01 @Override
- 02 public View getView(int position, View convertView, ViewGroup parent)
- 03 {
- 04 convertView = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.menulistitem, null);
- 05 TextView listItem = (TextView)convertView.findViewById(R.id.name_menu);
- 06 listItem.setText(list.get(position).get("name_menu").toString());
- 07
- 08 this.select_item = LiveChannelsActivity.select_item;
- 09
- 10 try{
- 11 if(this.select_item == position){
- 12 listItem.setTextSize(25); //选中的Item字体:25px
- 13 }
- 14 else
- 15 listItem.setTextSize(21); //未选中的Item字体:21px
- 16 }catch(Exception ex){
- 17 ex.printStackTrace();
- 18 }
- 19
- 20
- 21 return convertView;
- 22 }
复制代码 这样就可以了。
http://archive.cnblogs.com/a/2080347/ |
|