监听listview item兼容于checkbox/textview/imageview
2019独角兽企业重金招聘Python工程师标准>>>
package com.o1.android.view;import java.util.ArrayList;
import java.util.List;import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;/*** 监听listview item兼容于checkbox/textview/imageview * */
public abstract class ClickableListAdapter extends BaseAdapter {private LayoutInflater mInflater;private List mDataObjects; // our generic object listprivate int mViewId;/*** This is the holder will provide fast access to arbitrary objects and* views. Use a subclass to adapt it for your personal needs.*/public static class ViewHolder {// back reference to our list objectpublic Object data;}/*** The click listener base class.*/public static abstract class OnClickListener implementsView.OnClickListener {private ViewHolder mViewHolder;/*** @param holder The holder of the clickable item*/public OnClickListener(ViewHolder holder) {mViewHolder = holder;}// delegates the click eventpublic void onClick(View v) {onClick(v, mViewHolder);}/*** Implement your click behavior here* @param v The clicked view.*/public abstract void onClick(View v, ViewHolder viewHolder);};/*** The long click listener base class.*/public static abstract class OnLongClickListener implementsView.OnLongClickListener {private ViewHolder mViewHolder;/*** @param holder The holder of the clickable item*/public OnLongClickListener(ViewHolder holder) {mViewHolder = holder;}// delegates the click eventpublic boolean onLongClick(View v) {onLongClick(v, mViewHolder);return true;}/*** Implement your click behavior here* @param v The clicked view.*/public abstract void onLongClick(View v, ViewHolder viewHolder);};/*** @param context The current context* @param viewid The resource id of your list view item* @param objects The object list, or null, if you like to indicate an empty* list*/public ClickableListAdapter(Context context, int viewid, List objects) {// Cache the LayoutInflate to avoid asking for a new one each time.mInflater = LayoutInflater.from(context);mDataObjects = objects;mViewId = viewid;if (objects == null) {mDataObjects = new ArrayList<Object>();}}/*** The number of objects in the list.*/public int getCount() {return mDataObjects.size();}/*** @return We simply return the object at position of our object list Note,* the holder object uses a back reference to its related data* object. So, the user usually should use {@link ViewHolder#data}* for faster access.*/public Object getItem(int position) {return mDataObjects.get(position);}/*** We use the array index as a unique id. That is, position equals id.* * @return The id of the object*/public long getItemId(int position) {return position;}/*** Make a view to hold each row. This method is instantiated for each list* object. Using the Holder Pattern, avoids the unnecessary* findViewById()-calls.*/public View getView(int position, View view, ViewGroup parent) {// A ViewHolder keeps references to children views to avoid uneccessary// calls// to findViewById() on each row.ViewHolder holder;// When view is not null, we can reuse it directly, there is no need// to reinflate it. We only inflate a new View when the view supplied// by ListView is null.if (view == null) {view = mInflater.inflate(mViewId, null);// call the user's implementationholder = createHolder(view);// we set the holder as tagview.setTag(holder);} else {// get holder back...much faster than inflateholder = (ViewHolder) view.getTag();}// we must update the object's referenceholder.data = getItem(position);// call the user's implementationbindHolder(holder);return view;}/*** Creates your custom holder, that carries reference for e.g. ImageView* and/or TextView. If necessary connect your clickable View object with the* PrivateOnClickListener, or PrivateOnLongClickListener* * @param vThe view for the new holder object*/protected abstract ViewHolder createHolder(View v);/*** Binds the data from user's object to the holder* @param h The holder that shall represent the data object.*/protected abstract void bindHolder(ViewHolder h);
}// -------------------------------------------------------------
// E X A M P L E
// -------------------------------------------------------------// LAYOUT FILE
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center_vertical"><TextView android:text="Text" android:id="@+id/listitem_text"android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
<ImageView android:id="@+id/listitem_icon"android:src="@drawable/globe2_32x32"android:layout_width="wrap_content" android:layout_height="wrap_content"android:maxWidth="32px"android:maxHeight="32px">
</ImageView>
</LinearLayout>
//-------------------------------------------------------------- // ACTIVITY //-------------------------------------------------------------- package com.o1.android.view; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.o1.android.view.ClickableListAdapter; import com.o1.android.view.ClickableListAdapter.ViewHolder; /** * An example how to implement the ClickableListAdapter for a list view layout containing * a TextView and an ImageView. */ public class ClickableListItemActivity extends ListActivity { /** * Our data class. This data will be bound to * MyViewHolder, which in turn is used for the * ListView. */ static class MyData { public MyData(String t, boolean e) { text = t; enable = e; } String text; boolean enable; } /** * Our very own holder referencing the view elements * of our ListView layout */ static class MyViewHolder extends ViewHolder { public MyViewHolder(TextView t, ImageView i) { text = t; icon = i; } TextView text; ImageView icon; } /** * The implementation of ClickableListAdapter */ private class MyClickableListAdapter extends ClickableListAdapter { public MyClickableListAdapter(Context context, int viewid, List<MyData> objects) { super(context, viewid, objects); // nothing to do } protected void bindHolder(ViewHolder h) { // Binding the holder keeps our data up to date. // In contrast to createHolder this method is called for all items // So, be aware when doing a lot of heavy stuff here. // we simply transfer our object's data to the list item representatives MyViewHolder mvh = (MyViewHolder) h; MyData mo = (MyData)mvh.data; mvh.icon.setImageBitmap( mo.enable ? ClickableListItemActivity.this.mIconEnabled : ClickableListItemActivity.this.mIconDisabled); mvh.text.setText(mo.text); } @Override protected ViewHolder createHolder(View v) { // createHolder will be called only as long, as the ListView is not filled // entirely. That is, where we gain our performance: // We use the relatively costly findViewById() methods and // bind the view's reference to the holder objects. TextView text = (TextView) v.findViewById(R.id.listitem_text); ImageView icon = (ImageView) v.findViewById(R.id.listitem_icon); ViewHolder mvh = new MyViewHolder(text, icon); // Additionally, we make some icons clickable // Mind, that item becomes clickable, when adding a click listener (see API) // so, it is not necessary to use the android:clickable attribute in XML icon.setOnClickListener(new ClickableListAdapter.OnClickListener(mvh) { public void onClick(View v, ViewHolder viewHolder) { // we toggle the enabled state and also switch the icon MyViewHolder mvh = (MyViewHolder) viewHolder; MyData mo = (MyData) mvh.data; mo.enable = !mo.enable; // toggle ImageView icon = (ImageView) v; icon.setImageBitmap( mo.enable ? ClickableListItemActivity.this.mIconEnabled : ClickableListItemActivity.this.mIconDisabled); } }); // for text we implement a long click listener text.setOnLongClickListener(new ClickableListAdapter.OnLongClickListener(mvh) { @Override public void onLongClick(View v, ViewHolder viewHolder) { MyViewHolder mvh = (MyViewHolder) viewHolder; MyData mo = (MyData)mvh.data; // we toggle an '*' in our text element String s = mo.text; if (s.charAt(0) == '*') { mo.text = s.substring(1); } else { mo.text = '*' + s; } mvh.text.setText(mo.text); } }); return mvh; // finally, we return our new holder } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // preloading our icons mIconEnabled = BitmapFactory.decodeResource(this.getResources(), R.drawable.globe2_32x32); mIconDisabled = BitmapFactory.decodeResource(this.getResources(), R.drawable.globe2_32x32_trans); // fill list with some items... // to demonstrate the performance we create a bunch of data objects for (int i = 0; i < 250; ++i) { mObjectList.add(new MyData("Some Text " + i, true)); } //here we set our adapter setListAdapter(new MyClickableListAdapter(this, R.layout.clickablelistitemview, mObjectList)); } // --------------- field section ----------------- private Bitmap mIconEnabled; private Bitmap mIconDisabled; private List<MyData> mObjectList = new ArrayList<MyData>(); } // see http://androidsnippets.com/clickable-listview-items
转载于:https://my.oschina.net/oppo4545/blog/195211
监听listview item兼容于checkbox/textview/imageview相关推荐
- 监听ListView滚动到最底部
监听ListView滚动到最底部 监听ListView的滚动可以用两个东西: ListView.onScrollStateChanged (本文讲解这个listener的使用) 在OnGestureL ...
- Android项目:使用pulltorefresh开源项目扩展为下拉刷新上拉加载更多的处理方法,监听listview滚动方向...
很多android应用的下拉刷新都是使用的pulltorefresh这个开源项目,但是它的扩展性在下拉刷新同时又上拉加载更多时会有一定的局限性.查了很多地方,发现这个开源项目并不能很好的同时支持下拉刷 ...
- android列表项点击事件,Android 开发 tips(2):监听 Listview 列表项点击事件
Android 开发 tips(2):监听 Listview 列表项点击事件 (这篇和上篇本来是应该一起写的,但是太过冗长,附链接:[SimpleAdapter 在 Listview 中的应用] ht ...
- Android 关于在Activity中监听ListView
Android 开发时,最常用的控件之一就是ListView了,而使用ListView的同时,必然需要对它设置监听器,常用的监听器有这么几个: 1. OnItemClickListener // 监听 ...
- Android监听作用,Android开发之CheckBox的简单使用与监听功能示例
本文实例讲述了Android开发之CheckBox的简单使用与监听功能.分享给大家供大家参考,具体如下: activity_main.xml android:layout_width="ma ...
- Fragment 可见性监听方案 - 完美兼容多种 case
前言 本篇文章主要提供一种监听 Fragment 可见性监听的方案,完美多种 case,有兴趣的可以看看.废话不多说,开始进入正文. 在开发当中, fragment 经常使用到.在很多应用场景中,我们 ...
- android listview分区域监听,listview的监听事件
package JAVASwing;import java.awt.Container;import java.awt.FlowLayout;import java.awt.event.ActionE ...
- Android ListView item里控件的监听
在进行android开发的时候,我们会经常遇到,需要监听listview的item的控件(如button)的问题,比如点击item的图片跳转详情页,音乐列表里的item都有一个播放按钮,点击这个播放按 ...
- layui checkbox 监听
普通单选框 <input lay-filter="switchTest" type="checkbox" name="type[]" ...
- Android Glide加载图片、网络监听、设置资源监听
Glide加载图片.加载进度监听 前言 正文 一.项目配置 二.显示网络图片 三.添加设置资源监听 四.添加设置资源监听 五.添加加载进度条 六.封装工具类 七.源码 总结 前言 在日常开发中使用 ...
最新文章
- Nginx源代码安装
- C# #if, #else和#endif预处理指令
- 初始springCloud
- php 随机输出html,PHP随机插入关键字到有HTML的内容该怎么实现
- mapper mysl实现批量插入 更新
- Linux一些基本概念
- 安装nltk,textacy库
- editormd 支持拖放上传图片和视频
- 欧姆龙r88d系列服务器说明书,欧姆龙R88D-KN10H-ECT-Z用户手册 - 广州凌控
- keepalived的双机热备(主从模式)-主机宕机备机无法替代踩坑
- c语言趣味菜单实验报告,DSP实验报告+心得体会
- HDFS中NameNode和Secondary NameNode
- 经验 | 在麻省理工人工智能实验室如何做研究?
- MySQL安装后默认密码的问题
- HTTP协议详解你确定不看吗
- 可刷新的 PDB(PDB Refresh)
- 林轩田机器学习基石笔记(第23-24节)——上限函数Bounding Function
- https 请求的端口是443 注意
- Camtasia实用技巧之时间轴
- 统计找出一千万以内,一共有多少质数
热门文章
- nginx upstream配置_效率倍增!网易杭研Nginx自动扩缩容实践
- java继承 值_java继承
- xampp mysql关机意外_xampp运行MySQL shutdown unexpectedly解决方法
- BZOJ 1066 蜥蜴 最大流
- 理解Spring MVC Model Attribute 和 Session Attribute
- 11-实战模拟DRBD项目案例环境准备
- 信息安全问题频发:四成人讨厌大数据 六成人称微信谣言最多
- 20天精通 Windows 8:系列课程资料集
- Office 365系列(6)------Stage Migrate 搬迁方式至O365上来方法及步骤总结
- RAID 的各种方案