`
wang_peng1
  • 浏览: 3944245 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

listView 中存在editView chetxBox等点击事件处理

阅读更多

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>();

}

分享到:
评论

相关推荐

    Android编程实现ListView中item部分区域添加点击事件功能

    本文实例讲述了Android编程实现ListView中item部分区域添加点击事件功能。分享给大家供大家参考,具体如下: 需求如题目:Android listview中item部分区域添加点击事件,在一个界面显示了listview,但显示的内容分为...

    在Android中ListView中添加两个点击事件

    里要分享的是在一个listview中添加两个点击事件的解决办法,效果就像android qq的好友列表中,点击一个好友,会进入聊天界面,点击后面的图标,就会进入好友详细信息界面。这样的好处就是既不去掉ListView的...

    ListView和Button点击事件冲突处理

    而Button作为ListView的一个子视图,其点击事件会被ListView的触摸事件处理机制所覆盖。因此,当用户点击Button时,ListView可能会误认为是滚动操作,从而导致Button的点击事件无法正常执行。 解决这个问题通常有...

    listview item点击事件冲突问题

    如果在item布局中包含有可点击的子View(如Button、ImageView等),那么这些子View会截取并处理点击事件,导致ListView的onItemClickListener无法接收到点击事件。解决这个问题的方法是在子View的XML布局中添加`...

    ListView中的点击事件注意及分页的功能

    在实际应用中,我们经常需要处理ListView中的点击事件,特别是在有CheckBox等交互元素的情况下。本教程将深入讲解ListView中点击事件的处理以及实现分页加载数据的技术要点。 首先,我们要理解ListView中的点击事件...

    listview的事件处理

    本篇文章将深入探讨ListView的事件处理,特别是如何处理ListView与Button共存时焦点问题,以及在Adapter中实现接口来响应按钮点击,改变ListView中的数据。 首先,我们来解决“listview与button共存抢夺焦点问题”...

    在ListView中嵌套Switch开关,点击ListView条目来切换Switch的状态

    5. **点击事件处理**: - 要实现在ListView中点击条目时切换Switch,我们需要在Adapter的`getView()`方法中为每个列表项设置点击事件监听器。通常使用`setOnClickListener()`,并在此回调中获取对应位置的Switch,...

    Listview中的Button实现点击事件

    实现ListView中Button的点击事件,主要涉及到事件监听和数据绑定的概念。下面我们将详细探讨这两种常见的实现方式:接口回调和抽象类。 首先,我们来谈谈接口回调的方式。这种方式适用于父组件(如Activity或...

    listView上增加button点击事件

    标题"listView上增加button点击事件"所涉及的核心知识点是如何在ListView的item中嵌入Button,并同时处理这两个元素的独立点击事件,避免事件冲突。 首先,我们需要理解ListView的工作原理。ListView通过Adapter来...

    ListView和BaseAdapter搭配Button点击事件获取不同Item

    因此,我们需要确保在Item的点击事件处理中正确识别出触发事件的Button所属的Item。 1. **BaseAdapter**:这是ListView的数据源,负责将数据转化为列表项视图。你需要创建一个继承自BaseAdapter的自定义Adapter,...

    ListView中嵌入Button ,ListView的item与Button的click事件分别被响应,同时还实现了类似Digg的item点击效果

    在item的点击事件处理中,我们可以改变item的视觉状态,模拟Digg效果。这通常通过修改item的背景颜色、文字颜色或使用动画来实现。例如,我们可以使用`ColorDrawable`动态改变背景颜色: ```java view....

    Listview区分内容和CheckBox的点击事件(AS工程)

    这可以通过在TextView的onClick事件处理函数中调用`event.stopPropagation()`或者`event.cancel()`来实现,这样点击事件就不会传递到父View(即包含CheckBox的View)。 最后,这个工程可能还包括了对CheckBox选中...

    android 自定义listview无法响应点击事件OnItemClickListener

    - 如果在非主线程中更新ListView,可能会影响到点击事件的处理。所有的UI操作必须在主线程中执行。 以上是关于“android 自定义listview无法响应点击事件OnItemClickListener”这一问题的详细分析和解决策略。在...

    解析json实现listview,listView优化、子控件点击事件等实现

    文件"ListView"可能是示例代码,包含了一个简单的ListView应用实例,包括JSON解析、Adapter实现、ListView优化、图片加载和子控件点击事件处理等功能。通过查看和学习这个代码,你可以更好地理解和掌握上述知识点。...

    ListView刷新和监听滚动事件选项事件的例子

    当用户点击ListView中的某一项时,对应的回调方法会被调用,我们可以在这里处理点击事件,比如跳转到新的页面或者执行其他操作。 以下是一个简单的例子: ```java ListView listView = findViewById(R.id.list_...

    android捕获ListView中每个item点击事件

    ### Android捕获ListView中每个item点击事件 在Android开发中,`ListView`是十分常见的UI组件之一,用于显示可滚动的列表数据。当我们在`ListView`中放置一系列的数据项(通常称为item)时,通常还需要为这些item...

    Android中ListView实现子控件点击事件后ListView点击事件失效解决办法

    Android中ListView实现子控件点击事件后ListView点击事件失效解决办法,详细说明见相关博客: 博文地址:http://blog.csdn.net/leijf1239848066/article/details/9526483

    事件处理ListView

    在这个主题中,我们将深入探讨"事件处理ListView"的相关知识点。 首先,`ListView`是Android SDK中的一个视图组件,它允许用户在一个垂直滚动列表中显示多个项目。它通常与适配器(Adapter)配合使用,适配器负责将...

    Android Activity跳转和listview的使用

    在这个主题中,我们将深入探讨如何通过Intent和Bundle实现Activity间的参数传递和跳转,以及如何在ListView中设置Item的点击事件处理。 首先,Intent是Android系统中用于启动其他组件(如Activity、Service等)的...

Global site tag (gtag.js) - Google Analytics