【Android】listview选中行字体变大

目标:listview中item使用textview,当item选中时,字体为25px;当item未选中时,字体21px

device

之前想了很久,以为同listview选中行字体颜色一样,使用xml文件中使用selector就可以改变了,但是一直上网查找资料,都没有找到selector中改变字体大小的命令。后来网友提醒我,可以在adapter中的getview中修改。现将重要的代码放上:

?
1
2
//全局变量,记录选中的item 
public static int select_item = - 1 ;

先使用全局变量记录选中的item,然后在listview的OnItemSelectedListener中实时更新选中的item,并且使用adapter.notifyDataSetChanged()刷新listview数据。

?
1
2
3
4
5
6
7
8
9
10
11
listview_listMenu.setOnItemSelectedListener( new OnItemSelectedListener(){
     @Override
     public void onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3){
     select_item = arg2; //当前选择的节目item
  
     listAdapter.notifyDataSetChanged(); //通知adapter刷新数据
     }
  
     public void onNothingSelected(AdapterView<?> arg0) {
     }
});

接着在adapter的getview中修改字体大小。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Override
public View getView( int position, View convertView, ViewGroup parent) 
{
     convertView = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.menulistitem, null );
     TextView listItem = (TextView)convertView.findViewById(R.id.name_menu);
     listItem.setText(list.get(position).get( "name_menu" ).toString());
      
     this .select_item = LiveChannelsActivity.select_item;
  
     try {
         if ( this .select_item == position){
             listItem.setTextSize( 25 );   //选中的Item字体:25px
         }
         else
             listItem.setTextSize( 21 );   //未选中的Item字体:21px
     } catch (Exception ex){
         ex.printStackTrace();
     }
 
      
     return convertView;
}

这样就可以了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值