Android L中的Spinner的OnLongClickListener不起作用。有解决方案吗

时间:2014-11-15 03:27:59

标签: android spinner android-5.0-lollipop onlongclicklistener

当用户长按一个微调器时,我出现了一个Alertbox。它适用于Android 4.4及更低版本,但对于Android L(5.0),长按不会注册。有没有其他人看过这个问题或有解决方法?

这是代码

classArrayAdapter =   new ArrayAdapter<String> (getApplicationContext(),  R.layout.class_item,          displayClassNames);
classArrayAdapter.setDropDownViewResource(R.layout.class_dropdown);

classSpinner = new Spinner(this);
classSpinner.setAdapter(classArrayAdapter);
classSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
     // spinner code here - spinner works fine
}

classSpinner.setOnLongClickListener(new OnLongClickListener() 
    {
       @Override
        public boolean onLongClick(View v) 
        {
            if(UI==true)
            {
                studentsPresent();
            }
            else
            {
                studentsPassing();
            }
            return true;
        }
    });

1 个答案:

答案 0 :(得分:2)

您需要在适配器的项目上设置OnLongClickListener。由于我使用的是ArrayAdapter,我必须对其进行子类化并覆盖getView方法,如下所示:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final View view = super.getView(position, convertView, parent);
    if (onLongClickListener != null && convertView == null) { //set listener only for newly created view
        view.setLongClickable(true);
        view.setOnLongClickListener(onLongClickListener);
    }
    return view;
}

onLongClickListener字段在适配器的构造函数中设置。