开发者

How can I use spinner setOnItemLongClickListener

I am开发者_StackOverflow trying to make the Spinner behave different way when the user clicked on an item for a long time. I have spinner with some project and I want two things.

  1. When the user simple click on an item I want to normal select it.
  2. When the user have long clicked on an item I want to show dialog, with options like "Edit item", "Delete item".

The first step works well (ofcourse), but when I am trying to do the second task I can not make spinner to generate longClicked event.

Here is my code:

    this.projectSpinner = (Spinner) this.findViewById(R.id.SpinnerProjects);
    this.projectSpinner.setLongClickable(true);

    this.projectSpinner.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
        public boolean onItemLongClick(AdapterView<?> arg0, 
                                       View arg1, 
                                       int arg2, 
                                       long arg3) {
            Toast.makeText(
                 AndroidTimeTrackerMainActivity.this, 
                 "Long click", 
                 Toast.LENGTH_SHORT).show(); // This toast doesn't show up.
            return false;
        }

    });


The Spinner currently does not support OnItemLongClickListener.


You can add an OnLongClickListener to the Spinner though. It wont be fired when an item on the menu is long clicked but it will be fired when the user long clicks the spinner itself.

this.projectSpinner.setOnLongClickListener(new OnLongClickListener() 
{
    @Override
    public boolean onLongClick(View v) 
    {
        System.out.println(chuteSpinner.getSelectedItem().toString() + " is long clicked");
        return true;
    }
});


1) Create a custom spinner class by extending spinner,

public class CCSpinner extends Spinner {
    public CCSpinner(Context context) {
        super(context);
    }

    public CCSpinner(Context context, int mode) {
        super(context, mode);
    }

    public CCSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CCSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CCSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) {
        super(context, attrs, defStyleAttr, mode);
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }
} 

Use the above spinner in your xml view.

2) Define an interface to handle clicks,

 public interface ClickListener {
        void onItemLongClicked(View view);
        void onItemClicked(View view);
    }

3) In your spinner adapter class do these stuffs in getView and getDropDownView

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //ususal stuffs
        convertView.setTag(R.string.click_tag, position);
        convertView.setClickable(false);
        convertView.setLongClickable(false);



        return convertView;
    }


    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        //ususal stuffs
        convertView.setTag(R.string.click_tag, position);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) {
                    clickListener.onItemClicked(v);
                }
            }
        });


        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (clickListener != null) {
                    clickListener.onItemLongClicked(v);
                }
                return true;
            }
        });

        return convertView;
    }

4) While instantiating the adapter, pass the ClcikListener interface we created.

5) and in the implementing class do the following,

@Override
    public void onItemLongClicked(final View view) {
        mPSpinner.onDetachedFromWindow();
        final int pos = (int) view.getTag(R.string.click);
        mPSpinner.setSelection(pos);
    }


    @Override
    public void onItemClicked(final View view) {
        mPSpinner.onDetachedFromWindow();
        int pos = (int) view.getTag(R.string.click);
        mPSpinner.setSelection(pos);
    }


simply use an touchlistener with handler. for getting selected item

spinner.getSelectedItemPosition
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜