onItemLongClick监听器无法正常工作

时间:2017-11-20 09:26:58

标签: android

我的类包含onListItemClickonItemLongClick以及列表视图的适配器类。我的问题是onItemLongClick从未调用过怎么解决这个问题?

public class FilePicker extends ListActivity implements AdapterView.OnItemLongClickListener {
...


@Override
protected void onListItemClick(ListView l, View v,int position, long id) {

    Toast.makeText(getApplicationContext(),"SHORT",Toast.LENGTH_LONG).show();


    super.onListItemClick(l, v, position, id);
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

Toast.makeText(getApplicationContext(),"LOOOONG",Toast.LENGTH_LONG).show();


    return true;
}
 private class FilePickerListAdapter extends ArrayAdapter<File> {

    private List<File> obj;


    public FilePickerListAdapter(Context context, List<File> objects) {

        super(context, R.layout.list_item_browse, android.R.id.text1, objects);
        obj = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View r = null;

        if(convertView == null) {

            LayoutInflater inflater = (LayoutInflater)
                    getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            r = inflater.inflate(R.layout.list_item_browse, parent, false);
        }
        else
            r = convertView;

        File object = obj.get(position);

        ImageView imageView = (ImageView)r.findViewById(R.id.file_picker_image);
        TextView textView = (TextView)r.findViewById(R.id.file_picker_text);
        textView.setSingleLine(true);
        textView.setText(object.getName());

        if(object.isFile())
            imageView.setImageResource(R.drawable.play);

        else
            imageView.setImageResource(R.drawable.folder);


        return r;
    }
}
}

我还在android:longClickable="true"上尝试RelativeLayout(list_item_browse),但它不起作用。

1 个答案:

答案 0 :(得分:0)

您忘记为RelativeLayout注册点击侦听器。

初始化布局后,在getView中添加yourRelativeLayout.setOnLongClickListener(this);

编辑: 如果你想为listview使用longclickListener,那么在你的活动中注册带有listview的监听器并在你的活动中实现该方法

 yourListView.setOnItemLongClickListener(this);