Android - 列表视图,然后单击侦听器



我有一个列表视图lv .我设置了OnItemClickListenerOnItemLongClickListener 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;
                            }
            });

似乎你失踪的@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;
            }
        });  

这是我的代码,它也适用于长按事件。长按项目时单击事件不触发。

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;
    }
}

相关内容

  • 没有找到相关文章

最新更新