我有一个ListView lv
。我在OnItemClickListener
上设置了OnItemLongClickListener
和lv
。但是当调用OnItemLongClick
时,也会调用OnItemClick
。因此,我需要在不调用OnItemLongClick
的情况下致电OnItemClick
。我怎么能这样做?
请原谅我的英语。
UPD 以下是一些代码
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView p1, View p2, int p3, long p4)
{
//blah-blah-blah
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView p1, View p2, int p3, long p4) {
//blah-blah-blah
return true;
}
});
答案 0 :(得分:1)
似乎您@Override
setOnItemLongClickListener
示例:
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0,View arg1,int arg2, long arg3){
ListView lv = (ListView) findViewById(R.id.listView1);
final String Name = lv.getAdapter().getItem(arg2).toString();
// Make sth on click
}});
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
// Make sth on longclick
return true;
}
});
答案 1 :(得分:0)
这是我的代码,它也适用于长按事件。项目单击事件在长按时不会触发。
package com.example.spinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView)findViewById(R.id.list);
String fuel[] = new String[] { "Select Any", "Petrol", "Diesel",
"CNG Company Fitted", "CNG External Fitted" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fuel);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
}
});
list.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}