Gallery + GridView 实现图片与小圆点同步变化
直接上代码吧:
添加简单的自动播放功能:
private void autoPlay(){ //设置间隔5秒自动播放的功能 new Thread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub while(isAlive_){ currentIndex_ = currentIndex_ % infoIds.length; infoshow_gallery.post(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub infoshow_gallery.setSelection(currentIndex_); } }); //更新时间间隔 5秒 try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } currentIndex_++; } } }).start(); }
主要代码:
infoshow_gallery.setOnItemSelectedListener(new Gallery.OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub //设置当前选中的Index currentIndex_ = position; //改变GridView显示 gridviewAdapter.notifyDataSetInvalidated(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); // Gallery OnItemClick infoshow_gridview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub //设置当前选中的Index currentIndex_ = position; //改变GridView显示 gridviewAdapter.notifyDataSetInvalidated(); //改变Gallery显示 infoshow_gallery.setSelection(currentIndex_); } });
GridView适配器:
public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView img = new ImageView(context_); img.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT)); //红球表示选中状态 if(position == currentIndex_){ img.setImageResource(thumbIds[0]); }else{ img.setImageResource(thumbIds[1]); } img.setScaleType(ScaleType.FIT_CENTER); return img; }
Gallery适配器:
public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView img = new ImageView(context_); //此处每个ImageView都要占全部空间 img.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT)); img.setImageResource(infoIds[position]); img.setScaleType(ScaleType.FIT_XY); return img; }
效果图:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构