listview长按事件setOnItemLongClickListener的用法

本文探讨了在安卓开发中ListView的长按事件setOnItemLongClickListener的使用,解释了当返回false时长按后仍会触发点击事件,而返回true则会阻止点击事件的发生。通过实例展示了事件监听器的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

半夜睡不着,决定今天开始记录我的安卓学习过程的体会。

首先要明确,setOnItemLongClickListener响应了LongClick后是否会再相应Click?

如果返回false那么click仍然会被调用。而且是先调用Long click,然后调用click。 

如果返回true那么click就会被吃掉,click就不会再被调用了

这个函数我写的是这样:

       	  listview.setOnItemLongClickListener(new OnItemLongClickListener(){
              public boolean onItemLongClick(AdapterView<?> arg0, View arg1,  
                      int arg2, long arg3) {  
                  <span style="color:#ff0000;">TextView topicid = (TextView)arg1.findViewById(R.id.id);</span> 
                  String topicid_str=topicid.getText().toString();
                  Toast.makeText(ClassNoteActivity.this, topicid_str, Toast.LENGTH_LONG).show();
                  return true;
              }  
       	  });
写这种函数最烦的是获取Item每一项的值,从上边红字可以看到,在这个函数里,我们可以用类似getView中的方法来 直接通过获取控件来得到它的值

其中三个参数的含义可根据API:

public abstract boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id)

AdapterView<?> ,这个属于java基础的内容,叫做泛型。 比如String<?>,List<T>,Map<K,V>,String<E> 
?表示不确定的java类型。 
T  表示java类型。 
K V 分别代表java键值中的Key Value。 
E 代表Element。(粘自百度知道)
比如我的代码中整个adapter的设置是这样:
<span style="color:#333333;">      private void showlist(ArrayList<Topic> list){//显示笔记列表
       	  String[] from = {"title","note","date","id"};//</span><span style="color:#ff0000;">getDate()中map的value,必须一样!</span><span style="color:#333333;">
       	  int[] to = {R.id.title,R.id.note,R.id.date,R.id.id};
       	  Log.d("mylog","in showlist");
       	  SimpleAdapter adapter = new SimpleAdapter(this,
       			  getData(list),R.layout.topiclist,from,to);
       	  
       	  ListView listview = getListView();
       	  listview.setAdapter(adapter);//添加适配器
       	  listview.setOnItemClickListener(new OnItemClickListener(){
       		  public void onItemClick(AdapterView<?> parent,View view,int position,long id){
       			    //Log.i("mylog", "item class : "+ ((ListView) parent).
       			     //getItemAtPosition(position).getClass().getSimpleName());
       		          HashMap<String, String>  map = (HashMap<String, String>) ((ListView) parent).getItemAtPosition(position);</span>
<span style="color:#333333;">                    </span><span style="color:#ff0000;"> //在这里我就可以通过Adapter获取原先所有的映射</span><span style="color:#333333;">
       		         String  topic_id=map.get("id");
       		         Intent notelist = new Intent(ClassNoteActivity.this,NoteListActivity.class);
       		         Bundle notebundle = new Bundle();
       		         notebundle.putString("topic_id", topic_id);
       		         notebundle.putString("userid", userid);
       		         notelist.putExtras(notebundle);
       		         startActivity(notelist);
       		  }
       	  });
       	  listview.setOnItemLongClickListener(new OnItemLongClickListener(){
              public boolean onItemLongClick(AdapterView<?> arg0, View arg1,  
                      int arg2, long arg3) {  
                  TextView topicid = (TextView)arg1.findViewById(R.id.id); 
                  String topicid_str=topicid.getText().toString();
                  Toast.makeText(ClassNoteActivity.this, topicid_str, Toast.LENGTH_LONG).show();
                  return true;
              }  
       	  });
         }</span>
getDate就是将数据源进行键值映射:
<span style="color:#333333;">  	  private List<Map<String, Object>> getData(ArrayList<Topic> list1) {//</span><span style="color:#ff0000;">这个list1是我从数据库已经读到的值</span><span style="color:#333333;">
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        int len = list1.size();      
        for(int i=0;i<len;++i){
        	Map<String, Object> map = new HashMap<String, Object>();
        	map.put("title", list1.get(i).getTitle());        	
        	String note = list1.get(i).getNote();
        	if(note.length()>100) note=note.substring(0, 99)+"……";
        	map.put("note", note);
        	map.put("date", list1.get(i).getDate());
        	map.put("id", list1.get(i).getId());
        	list.add(map);
        }
        String len1=Integer.toString(list.size());
		return list;
	}</span>
View即一块Item视图,就是你定义的那个list的整个xml里的所有东西,position是位置,默认从0开始递增,id是行号,也从0开始。


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值