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);
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;
}
}
public interface ItemClickListener {
void onItemLongClicked(View view);
void onItemClicked(View view);
}
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();
}
}));
}
}
<?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>