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

来源:互联网 发布:充值软件多少钱 编辑:程序博客网 时间:2024/05/08 01:34

http://www.apkbus.com/forum.php?mod=viewthread&tid=69669

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


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

GrideView.setAdapter(BaseAdatper) 

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

/ 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被选中的时候能够放大,而离开的时候又变回原来的大小,我也是煞费苦心,找了很多资料,最后在
http://my.oschina.net/u/126188/blog/28990。代码就是在adapter中加入

private int selected = -1; public void notifyDataSetChanged(int id) { selected = id; super.notifyDataSetChanged(); }

并在 getView中进行判断:

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了

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 0
原创粉丝点击