用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被选中的时候就会放大,而其他未被选中的就会变小。