- 浏览: 71922 次
- 性别:
- 来自: 广州
最新评论
-
鱼在陆地上跑:
yanjunhui2011 写道http://download ...
Android自适应屏幕方向、大小和分辨率 -
yanjunhui2011:
http://download.csdn.net/detail ...
Android自适应屏幕方向、大小和分辨率 -
鱼在陆地上跑:
sydydream 写道<supports-screen ...
Android自适应屏幕方向、大小和分辨率 -
sydydream:
<supports-screens and ...
Android自适应屏幕方向、大小和分辨率 -
lenomon:
这里有篇实现无下划线的,Android使用TextView实现 ...
通过SpannableString来设置超链接、颜色、字体等属性(转)
转自:http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html
有时候,列表不光会用来做显示用,我们同样可以在在上面添加按钮。添加按钮首先要写一个有按钮的xml文件,然后自然会想到用上面的方法定义一个适配器,然后将数据映射到布局文件上。但是事实并非这样,因为按钮是无法映射的,即使你成功的用布局文件显示出了按钮也无法添加按钮的响应,这时就要研究一下ListView是如何现实的了,而且必须要重写一个类继承BaseAdapter。下面的示例将显示一个按钮和一个图片,两行字如果单击按钮将删除此按钮的所在行。并告诉你ListView究竟是如何工作的。
效果如下:
实现如下:
列表项的布局文件vlist2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px" /> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="22px" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="13px" /> </LinearLayout> <Button android:id="@+id/view_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/s_view_btn" android:layout_gravity="bottom|right" /> </LinearLayout>
程序代码:
/** * @author allin * */ public class MyListView4 extends ListActivity { private List<Map<String, Object>> mData; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mData = getData(); MyAdapter adapter = new MyAdapter(this); setListAdapter(adapter); } private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "G1"); map.put("info", "google 1"); map.put("img", R.drawable.i1); list.add(map); map = new HashMap<String, Object>(); map.put("title", "G2"); map.put("info", "google 2"); map.put("img", R.drawable.i2); list.add(map); map = new HashMap<String, Object>(); map.put("title", "G3"); map.put("info", "google 3"); map.put("img", R.drawable.i3); list.add(map); return list; } // ListView 中某项被选中后的逻辑 @Override protected void onListItemClick(ListView l, View v, int position, long id) { Log.v("MyListView4-click", (String)mData.get(position).get("title")); } /** * listview中点击按键弹出对话框 */ public void showInfo(){ new AlertDialog.Builder(this) .setTitle("我的listview") .setMessage("介绍...") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .show(); } public final class ViewHolder{ public ImageView img; public TextView title; public TextView info; public Button viewBtn; } public class MyAdapter extends BaseAdapter{ private LayoutInflater mInflater; public MyAdapter(Context context){ this.mInflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return mData.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder=new ViewHolder(); convertView = mInflater.inflate(R.layout.vlist2, null); holder.img = (ImageView)convertView.findViewById(R.id.img); holder.title = (TextView)convertView.findViewById(R.id.title); holder.info = (TextView)convertView.findViewById(R.id.info); holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn); convertView.setTag(holder); }else { holder = (ViewHolder)convertView.getTag(); } holder.img.setBackgroundResource((Integer)mData.get(position).get("img")); holder.title.setText((String)mData.get(position).get("title")); holder.info.setText((String)mData.get(position).get("info")); holder.viewBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showInfo(); } }); return convertView; } } }
发表评论
-
Activity的启动与通信
2012-02-01 17:14 1450Activity是Android组件中最基本也是最常见的组件, ... -
【转】android通过USB使用真机调试程序
2012-02-01 11:04 1086转自:http://www.cnblogs.com/azai/ ... -
【整理】通过ViewPager实现Launcher中的屏幕切换效果
2012-01-30 17:06 2651整理自:http://www.cnmsdn.com/html/ ... -
【转】实现自定义布局的Notification
2012-01-30 15:57 1535转自:http://blog.csdn.net/chenlon ... -
Eclipse 语法自动提示
2012-01-30 09:39 13261. 从Window -> preferences -& ... -
【转】Notification 详解
2012-01-29 16:08 1186转自:http://www.microdu.com/threa ... -
【转】Toast大全(五种情形)建立属于你自己的Toast
2012-01-29 13:51 1007转自:http://www.cnblogs.com/salam ... -
【转】Toast简易消息提示框的使用
2012-01-29 10:46 1198转自:http://www.2cto.com/kf ... -
实现Launcher的抽屉效果
2012-01-19 10:18 1186android 的 launcher 有一个抽屉效果,可以有拉 ... -
【整理】Android animation - 基础
2012-01-18 11:30 826整理自:http://blog.csdn.net/lzx_bu ... -
【转】编写高效的android代码
2012-01-17 10:00 861转自:http://www.2cto.com/kf/20111 ... -
【转】Android 分页控件制成底部菜单
2012-01-16 17:47 1859转自:http://blog.csdn.net/knowhea ... -
【转】android使用分页标签
2012-01-16 15:46 1206转自:http://marshal.easymorse.com ... -
【转】Eclipse调试常用技巧
2012-01-12 11:03 751转:http://chenhaodejia.iteye ... -
【转】Content Provider 基础 之URI
2012-01-12 10:42 1125转自:http://chenhaodejia.it ... -
【转】ImageButton点击背景切换事件
2012-01-12 10:31 2173转自:http://chenhaodejia.iteye.co ... -
【转】ContentProvider基础
2012-01-12 09:26 865转自:http://wq294948004.iteye.com ... -
使用bindService启动服务
2012-01-10 10:32 4874一般情况下我们使用startService(I ... -
Android SDCard操作(文件读写,容量计算)
2011-04-08 00:55 709Android SDCard操作(文件读写,容量计算) ... -
【转】Service创建有两种方法: startService或者bindService
2011-06-18 15:18 1106服务不能自己运行,需要通过调用Context.startSer ...
相关推荐
我需要的ListView是如图一样的实现SD卡资源文件浏览列表,每个列表项由一个ImageView、TextView、CheckBox组成,并且要求当整个列表中有一个或一个以上的Checkbox被选中时,右上角的搜索按钮就显示出来,否则就隐藏...
在开发中,我们经常使用到ListView这个控件。Android的API也提供了许多创建ListView适配器的快捷方式。例如ArrayAdapter、SimpleAdapter和SimpleCursorAdapter等。但你是否发现,如果采用这些系统自带的适配器,对于...
本文讲实现一个自定义列表的Android程序,程序将实现一个使用自定义的适配器(Adapter)绑定 数据,通过contextView.setTag绑定数据有按钮的ListView。 系统显示列表(ListView)时,首先会实例化一个适配器,本文...
自定义gridview和listview实现单选功能实现,通过adapter来控制。
在平常的开发过程中,我们的ListView可能不只是简单的显示下文本或者按钮,更多的是显示复杂的布局,这样的话,我们就得自己写布局和自定义adapter了,一般是继承于BaseAdapter,示例代码见下方。写ListView的点击...
一个ListView通常有两个职责。 (1)将数据填充到布局。 (2)处理用户的选择点击等操作。 第一点很好理解,ListView就是实现这个功能的。第二点也不难做到,在后面的学习中读者会发现,这非常简单。 一个...
(3)掌握自定义控件的2种使用方法。 (4)掌握ListView的简单用法。 (5)掌握自定义ListView控件的使用。 实验软、硬件环境 硬件:PC电脑一台; 配置:winxp或win7系统,内存大于4G,硬盘250G及以上 JDK1.7 、...
(3)掌握自定义控件的2种使用方法。 (4)掌握ListView的简单用法。 (5)掌握自定义ListView控件的使用。 实验软、硬件环境 硬件:PC电脑一台; 配置:winxp或win7系统,内存大于4G,硬盘250G及以上 JDK1.7 、...
自定义Adapter来建立复杂的列表项 99 动态添加、删除ListView列表项 102 改变ListView列表项选中状态的背景颜色 102 可展开的列表组件 102 数据的存取 103 SharePreferences 6.1 103 文件的存储6.2 103 SQLite数据库...
5.11 制作有图标的文件资源管理器——自定义Adapter对象 5.12 还原手机默认桌面——重写clearWallpaper方法 5.13 置换手机背景图——Gallery与setWallpaper整合实现 5.14 撷取手机现存桌面——getWallpaper与...
5.11 制作有图标的文件资源管理器——自定义Adapter对象 5.12 还原手机默认桌面——重写clearWallpaper方法 5.13 置换手机背景图——Gallery与setWallpaper整合实现 5.14 撷取手机现存桌面——getWallpaper与...
5.11 制作有图标的文件资源管理器——自定义Adapter对象 5.12 还原手机默认桌面——重写clearWallpaper方法 5.13 置换手机背景图——Gallery与setWallpaper整合实现 5.14 撷取手机现存桌面——getWallpaper与...
24.4.5 使用语言方法 24.5 参考资料 24.6 小结 第25章 触摸屏 25.1 MotionEvent 25.1.1 MotionEvent 对象 25.1.2 回收MotionEvent 25.1.3 使用VelocityTracker 25.1.4 探索拖放操作 25.2...
24.4.5 使用语言方法 24.5 参考资料 24.6 小结 第25章 触摸屏 25.1 MotionEvent 25.1.1 MotionEvent 对象 25.1.2 回收MotionEvent 25.1.3 使用VelocityTracker 25.1.4 探索拖放操作 25.2...
5.11 制作有图标的文件资源管理器——自定义Adapter对象 5.12 还原手机默认桌面——重写clearWallpaper方法 5.13 置换手机背景图——Gallery与setWallpaper整合实现 5.14 撷取手机现存桌面——getWallpaper与...
5.11 制作有图标的文件资源管理器——自定义Adapter对象 5.12 还原手机默认桌面——重写clearWallpaper方法 5.13 置换手机背景图——Gallery与setWallpaper整合实现 5.14 撷取手机现存桌面——getWallpaper与...
5.11 制作有图标的文件资源管理器——自定义Adapter对象 5.12 还原手机默认桌面——重写clearWallpaper方法 5.13 置换手机背景图——Gallery与setWallpaper整合实现 5.14 撷取手机现存桌面——getWallpaper与...