eatwhatApp开发实战(九)

  之前我们为app在item项上添加了点击出现修改对话框,对店名进行修改的功能,其中我们会发现我们点击item和点击item上的按钮会有点击冲突。这次我们来修正下这个问题,同时介绍item项的长按点击OnItemLongClickListener()。

  解决这个问题只需要修改对应item的xml文件上的两个属性,首先是item的布局上的设置:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants" >

  其次是在点击的button上进行修改:

	<Button
	    android:id="@+id/item_btn"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_alignParentRight="true"
	    android:layout_centerInParent="true"
	    android:focusable="false"
	    android:clickable="true"
	    android:text="删除"/>

  之后就可以对item和button进行不同的点击处理:

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, final int position,long id) {
	//item点击事件 比如修改店名
   }

  button做删除数据的操作:

btn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
    //删除数据的方法
    });

  最后介绍下长按item的OnItemLongClickListener():

shop_lv.setOnItemLongClickListener(new itemLongClick());

  接口实现:

	class itemLongClick implements OnItemLongClickListener{

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			
			Log.e("OnItemLongClickListener","long clikc");
			return true;
		}
	}

  这样就能实现item的长按对应功能。

原文地址:https://www.cnblogs.com/superdo/p/5137815.html