如何使Android GridView中选中图片时变大,而当焦点离开该图片后缩小

用Android实现的浏览器导航界面,如下图所示:


GridView

如上图所示的众多图片是用GridView实现的。而GridView使用的时候一般都要setAdapter(...)


[Java]  纯文本查看  复制代码
?
1
GrideView.setAdapter(BaseAdatper)


一般都通过继承BaseAdapter来实现自己的Adapter。有一些必须实现的方法:

[Java]  纯文本查看  复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
// How many items are in the data set represented by this Adapter. 
 
public int getCount() { 
 
// Get the data item associated with the specified position in the data set. 
public Object getItem( int position) { 
 
// Get the row id associated with the specified position in the list. 
public long getItemId( int position) { 
   
// Get a View that displays the data at the specified position in the data set. 
   public View getView( int position, View convertView, ViewGroup parent) { 
   
}


    为了使GridView里面的Item被选中的时候能够放大,而离开的时候又变回原来的大小,我也是煞费苦心,找了很多资料,最后在
这个网页上找到了可以借鉴的地方,感谢分享。


其实就是使用了


[Java]  纯文本查看  复制代码
?
1
2
3
4
5
private int selected = - 1
public void notifyDataSetChanged( int id) { 
selected = id; 
super .notifyDataSetChanged(); 
}



并在 getView中进行判断:

[Java]  纯文本查看  复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
public View getView( int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     MyImageButton imgBtn; 
     imgBtn = new MyImageButton( this .context, photoLink[position]); 
     if (selected == position) { 
         // the special one.Scale Large 
         imgBtn.setScaleType(ScaleType.CENTER_CROP); 
     } else
         // the rest.Scale small 
         imgBtn.setScaleType(ScaleType.CENTER_INSIDE); 
    
     return (View)imgBtn; 
   }


当我们的GridView设置了Adapter的时候,我们就可以设置OnItemSelectedListener了

[Java]  纯文本查看  复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
zonesView.setOnItemSelectedListener( new OnItemSelectedListener() { 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
     int arg2, long arg3) { 
     Log.d( "zonesView" , "onItemSelected" ); 
     ImageAdapter zonesImageAdapter = (ImageAdapter) zonesView.getAdapter(); 
     zonesImageAdapter.notifyDataSetChanged(arg2); 
     }   
    public void onNothingSelected(AdapterView<?> arg0) { 
         // TODO Auto-generated method stub 
            
     }          
});


这样一来,当GrideView的Item被选中的时候就会放大,而其他未被选中的就会变小。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值