- 浏览: 3942862 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
hinuliba:
...
字体背景颜色的高度修改 -
KANGOD:
最后的 -createDialog() 私有方法是怎么回事,没 ...
简单的实现listView中item多个控件以及点击事件 -
sswangqiao:
呵呵,呵呵
onActivityResult传值的使用 -
yumeiqiao:
感觉你所的不清楚 lstView.setOnTouchLi ...
listview中viewflipper的问题 -
lizhou828:
果然是大神啊!!!
Animation动画效果的实现
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;
/**
* This list adapter is derived from the "Efficient List Adapter"-Example of
* API-Demos. It uses holder object to access the list items efficiently.
* Additionally, click listeners are provided, which can be connected to the
* arbitrary view items, e.g. customized checkboxes, or other clickable
* Image/TextViews. Implement subclasses of them and add your listeners to your
* "clickable" views.
*
* @author poss3x
*/
public abstract class ClickableListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List mDataObjects; // our generic object list
private 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 object
public Object data;
}
/**
* The click listener base class.
*/
public static abstract class OnClickListener implements
View.OnClickListener {
private ViewHolder mViewHolder;
/**
* @param holder The holder of the clickable item
*/
public OnClickListener(ViewHolder holder) {
mViewHolder = holder;
}
// delegates the click event
public 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 implements
View.OnLongClickListener {
private ViewHolder mViewHolder;
/**
* @param holder The holder of the clickable item
*/
public OnLongClickListener(ViewHolder holder) {
mViewHolder = holder;
}
// delegates the click event
public 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 implementation
holder = createHolder(view);
// we set the holder as tag
view.setTag(holder);
} else {
// get holder back...much faster than inflate
holder = (ViewHolder) view.getTag();
}
// we must update the object's reference
holder.data = getItem(position);
// call the user's implementation
bindHolder(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"?>
<LinearLayout
xmlns: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.
* @author poss3x
*/
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>();
}
发表评论
-
URI 转path
2019-06-26 10:41 1322转自知乎Matisse package com.zhihu ... -
权限申请
2017-09-22 13:25 1263public class PermissionActivit ... -
onPreviewFrame 相机输出格式转换yuv420p保存成图片
2015-11-25 15:59 7569在最近项目中,因为特殊需要,底层相机往外输出了i420 也 ... -
new Android's Runtime Permission
2015-11-03 21:05 1240targetSdkVersion 23 开始 使用运行时权 ... -
自定义listview 边缘效果
2015-02-28 10:58 1738static void ChangeEdgeEffect( ... -
发射打开wifi
2015-01-07 10:25 1432WifiManager wifiManager = (Wif ... -
RecyclerView
2014-11-05 13:08 1254http://www.grokkingandroid.com ... -
获取点击区域
2014-04-28 09:39 1581@Override public void getHitR ... -
speex 和libogg 编译
2014-04-03 16:17 6403下载: http://www.speex.org/down ... -
rsync 同步
2014-03-28 17:06 1837两台android 设备 进行rsy ... -
流转字符串
2014-03-11 09:49 1522public static String convertSt ... -
java simplexml 序列化
2014-03-06 13:22 5962<?xml version="1.0&quo ... -
获取其他程序的特定资源
2014-03-05 09:33 1690try { PackageManager man ... -
检测来电属于哪个sim卡
2014-02-07 10:41 1723public class IncomingCallInter ... -
使用 NDK r9 编译ffmpeg
2014-01-16 13:32 168431. 环境 ubuntu 我的是13.10 ndk r9 ... -
android h264含so
2014-01-13 11:24 1546http://download.csdn.net/downlo ... -
xml转义字符
2013-12-18 09:29 1593" " ' & ... -
字体背景颜色的高度修改
2013-12-11 10:31 4215当使用android:lineSpacingExtra= ... -
屏保的实现
2013-12-07 10:27 2797最近需要做一个屏保,开始以为很简单,因为系统本身就带了屏保功 ... -
PreferenceActivity下嵌套PreferenceScreen在其它布局中
2013-11-21 16:32 9160今天在修改系统代码的时候,系统代码用了PreferenceA ...
相关推荐
本文实例讲述了Android编程实现ListView中item部分区域添加点击事件功能。分享给大家供大家参考,具体如下: 需求如题目:Android listview中item部分区域添加点击事件,在一个界面显示了listview,但显示的内容分为...
里要分享的是在一个listview中添加两个点击事件的解决办法,效果就像android qq的好友列表中,点击一个好友,会进入聊天界面,点击后面的图标,就会进入好友详细信息界面。这样的好处就是既不去掉ListView的...
而Button作为ListView的一个子视图,其点击事件会被ListView的触摸事件处理机制所覆盖。因此,当用户点击Button时,ListView可能会误认为是滚动操作,从而导致Button的点击事件无法正常执行。 解决这个问题通常有...
如果在item布局中包含有可点击的子View(如Button、ImageView等),那么这些子View会截取并处理点击事件,导致ListView的onItemClickListener无法接收到点击事件。解决这个问题的方法是在子View的XML布局中添加`...
在实际应用中,我们经常需要处理ListView中的点击事件,特别是在有CheckBox等交互元素的情况下。本教程将深入讲解ListView中点击事件的处理以及实现分页加载数据的技术要点。 首先,我们要理解ListView中的点击事件...
本篇文章将深入探讨ListView的事件处理,特别是如何处理ListView与Button共存时焦点问题,以及在Adapter中实现接口来响应按钮点击,改变ListView中的数据。 首先,我们来解决“listview与button共存抢夺焦点问题”...
5. **点击事件处理**: - 要实现在ListView中点击条目时切换Switch,我们需要在Adapter的`getView()`方法中为每个列表项设置点击事件监听器。通常使用`setOnClickListener()`,并在此回调中获取对应位置的Switch,...
实现ListView中Button的点击事件,主要涉及到事件监听和数据绑定的概念。下面我们将详细探讨这两种常见的实现方式:接口回调和抽象类。 首先,我们来谈谈接口回调的方式。这种方式适用于父组件(如Activity或...
标题"listView上增加button点击事件"所涉及的核心知识点是如何在ListView的item中嵌入Button,并同时处理这两个元素的独立点击事件,避免事件冲突。 首先,我们需要理解ListView的工作原理。ListView通过Adapter来...
因此,我们需要确保在Item的点击事件处理中正确识别出触发事件的Button所属的Item。 1. **BaseAdapter**:这是ListView的数据源,负责将数据转化为列表项视图。你需要创建一个继承自BaseAdapter的自定义Adapter,...
在item的点击事件处理中,我们可以改变item的视觉状态,模拟Digg效果。这通常通过修改item的背景颜色、文字颜色或使用动画来实现。例如,我们可以使用`ColorDrawable`动态改变背景颜色: ```java view....
这可以通过在TextView的onClick事件处理函数中调用`event.stopPropagation()`或者`event.cancel()`来实现,这样点击事件就不会传递到父View(即包含CheckBox的View)。 最后,这个工程可能还包括了对CheckBox选中...
- 如果在非主线程中更新ListView,可能会影响到点击事件的处理。所有的UI操作必须在主线程中执行。 以上是关于“android 自定义listview无法响应点击事件OnItemClickListener”这一问题的详细分析和解决策略。在...
文件"ListView"可能是示例代码,包含了一个简单的ListView应用实例,包括JSON解析、Adapter实现、ListView优化、图片加载和子控件点击事件处理等功能。通过查看和学习这个代码,你可以更好地理解和掌握上述知识点。...
当用户点击ListView中的某一项时,对应的回调方法会被调用,我们可以在这里处理点击事件,比如跳转到新的页面或者执行其他操作。 以下是一个简单的例子: ```java ListView listView = findViewById(R.id.list_...
### Android捕获ListView中每个item点击事件 在Android开发中,`ListView`是十分常见的UI组件之一,用于显示可滚动的列表数据。当我们在`ListView`中放置一系列的数据项(通常称为item)时,通常还需要为这些item...
Android中ListView实现子控件点击事件后ListView点击事件失效解决办法,详细说明见相关博客: 博文地址:http://blog.csdn.net/leijf1239848066/article/details/9526483
在这个主题中,我们将深入探讨如何通过Intent和Bundle实现Activity间的参数传递和跳转,以及如何在ListView中设置Item的点击事件处理。 首先,Intent是Android系统中用于启动其他组件(如Activity、Service等)的...
在这个主题中,我们将深入探讨"事件处理ListView"的相关知识点。 首先,`ListView`是Android SDK中的一个视图组件,它允许用户在一个垂直滚动列表中显示多个项目。它通常与适配器(Adapter)配合使用,适配器负责将...