1
1
1q12019-10-26 15:15:25
Java
1q1, 2019-10-26 15:15:25

How to set setOnItemLongClickListener for spinera items?

I want to put a context menu like "delete, edit" on spinner items on long press.
But, as it turned out, without additional squats, this possibility cannot be realized. Found a similar issue on overflow: stackoverflow.com/questions/2730610/how-can-i-use-spinner-setonitemlongclicklistener
The penultimate answer suggests a working solution, but I seem to be too dumb to figure it out without more help.
1) Create a custom spinner class by extending spinner
I understand how, but I don't understand why
2) Define an interface to handle clicks,
Ok, defined
3) In your spinner adapter class do these stuffs in getView and getDropDownView
I use this adapter:

ArrayAdapter<Category> adapter;
adapter = new ArrayAdapter<Category>(this, android.R.layout.simple_spinner_item,  lCategory);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

I don't understand how to do this.
The following points 4 and 5 also cause difficulties.
Can someone explain in more detail, or make a demo?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
terminator-light, 2019-10-26
@1q1

It inherited from Spinner because the onDetachedFromWindow() method is protected, which means it is not accessible from the outside, so he made it public. If you do not call this method, the dropdown will not close after clicking on the item. If you don't want to inherit, you can use reflection like here https://stackoverflow.com/a/35759475
The above code works:

SpinnerAdapter
public class SpinnerAdapter extends BaseAdapter {

    private List<String> items;
    private LayoutInflater inflater;
    private ItemClickListener clickListener;

    public SpinnerAdapter(Context context, List<String> items, ItemClickListener clickListener){
        inflater = LayoutInflater.from(context);
        this.items = items;
        this.clickListener = clickListener;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.spinner_item, parent, false);
        }
        ((TextView)convertView).setText(items.get(position));
        convertView.setTag(position);
        convertView.setClickable(false);
        convertView.setLongClickable(false);
        
        return convertView;
    }


    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        convertView = getView(position, convertView, parent);
        convertView.setTag(position);

        convertView.setOnClickListener(v -> {
            if (clickListener != null) {
                clickListener.onItemClicked(v);
            }
        });

        convertView.setOnLongClickListener(v -> {
            if (clickListener != null) {
                clickListener.onItemLongClicked(v);
            }
            return true;
        });

        return convertView;
    }
}

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

MainActivity
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        List<String> strings = new ArrayList<String>(){{
            add("John");
            add("James");
            add("Jake");
            add("Jane");
        }};

        CustomSpinner spinner = findViewById(R.id.spinner);

        spinner.setAdapter(new SpinnerAdapter(this, strings, new ItemClickListener() {
            @Override
            public void onItemLongClicked(View view) {
                spinner.onDetachedFromWindow();
                final int pos = (int) view.getTag();
                spinner.setSelection(pos);
                Toast.makeText(MainActivity.this, "regular click: "+strings.get(pos), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemClicked(View view) {
                spinner.onDetachedFromWindow();
                final int pos = (int) view.getTag();
                spinner.setSelection(pos);
                Toast.makeText(MainActivity.this, "long click: "+strings.get(pos), Toast.LENGTH_SHORT).show();
            }

        }));
    }
}

spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_height="wrap_content">
</TextView>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question