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

来源:互联网 发布:js frameelement 编辑:程序博客网 时间:2024/05/05 03:34

用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. 
 
publicint getCount() { 
 
// Get the data item associated with the specified position in the data set. 
publicObject getItem(intposition) { 
 
// Get the row id associated with the specified position in the list. 
publiclong getItemId(intposition) { 
   
// Get a View that displays the data at the specified position in the data set. 
  publicView getView(intposition, View convertView, ViewGroup parent) { 
   
}


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


其实就是使用了


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



并在 getView中进行判断:

[Java] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
publicView getView(intposition, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    MyImageButton imgBtn; 
    imgBtn = newMyImageButton(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(newOnItemSelectedListener() { 
    publicvoid onItemSelected(AdapterView<?> arg0, View arg1, 
    intarg2, longarg3) { 
    Log.d("zonesView","onItemSelected"); 
    ImageAdapter zonesImageAdapter = (ImageAdapter) zonesView.getAdapter(); 
    zonesImageAdapter.notifyDataSetChanged(arg2); 
    }   
   publicvoid onNothingSelected(AdapterView<?> arg0) { 
        // TODO Auto-generated method stub 
            
    }          
});


这样一来,当GrideView的Item被选中的时候就会放大,而其他未被选中的就会变小。
0 0
原创粉丝点击