android spinner in recyclerView does not work properly
up vote
1
down vote
favorite
I am trying to use Spinner ( 3 dots with dropdown menu) in my recyclerView, but when I load this recyclerView function onItemSelected is called automatically and later when I press on item it is not called anymore. Here is my code in adapter:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.my_listing_item_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
((SaleAdapter.SalesViewHolder) holder).overflow.setAdapter(adapter);
((SaleAdapter.SalesViewHolder) holder).overflow.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(context, String.valueOf(position) + " item is selected", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public static class SalesViewHolder extends RecyclerView.ViewHolder {
public LinearLayout overflowLayout;
public Spinner overflow;
public SalesViewHolder(View itemView, Context context) {
super(itemView);
overflowLayout = (LinearLayout) itemView.findViewById(R.id.list_item_overflow_layout);
overflow = (Spinner) itemView.findViewById(R.id.list_item_overflow);
}
}
Here is code of View:
<LinearLayout
android:id="@+id/list_item_overflow_layout"
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:padding="15dp"
android:background="@drawable/ic_more_vert_24dp_grey"
>
<Spinner
android:id="@+id/list_item_overflow"
android:layout_width="24dp"
android:layout_height="24dp"
/>
</LinearLayout>
Does anyone know what is the problem here with my code? Should I export setOnItemSelectedListener somewhere else?
android android-recyclerview spinner adapter
|
show 3 more comments
up vote
1
down vote
favorite
I am trying to use Spinner ( 3 dots with dropdown menu) in my recyclerView, but when I load this recyclerView function onItemSelected is called automatically and later when I press on item it is not called anymore. Here is my code in adapter:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.my_listing_item_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
((SaleAdapter.SalesViewHolder) holder).overflow.setAdapter(adapter);
((SaleAdapter.SalesViewHolder) holder).overflow.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(context, String.valueOf(position) + " item is selected", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public static class SalesViewHolder extends RecyclerView.ViewHolder {
public LinearLayout overflowLayout;
public Spinner overflow;
public SalesViewHolder(View itemView, Context context) {
super(itemView);
overflowLayout = (LinearLayout) itemView.findViewById(R.id.list_item_overflow_layout);
overflow = (Spinner) itemView.findViewById(R.id.list_item_overflow);
}
}
Here is code of View:
<LinearLayout
android:id="@+id/list_item_overflow_layout"
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:padding="15dp"
android:background="@drawable/ic_more_vert_24dp_grey"
>
<Spinner
android:id="@+id/list_item_overflow"
android:layout_width="24dp"
android:layout_height="24dp"
/>
</LinearLayout>
Does anyone know what is the problem here with my code? Should I export setOnItemSelectedListener somewhere else?
android android-recyclerview spinner adapter
So each item in the recyclerview includes a spinner which wants to mimic the menu dropdown?
– Skemelio
Nov 7 at 14:19
Tomas, yes, you are right
– Tomas Maksimavicius
Nov 7 at 14:43
I believe it's a better idea to implement a pop up menu for every recyclerview item instead of a spinner.
– Skemelio
Nov 7 at 14:48
As the docs say "Providing a drop-down similar to Spinner that does not retain a persistent selection."
– Skemelio
Nov 7 at 14:49
Tomas, but popup menu requires clear coordinates for each item, it is not possible in recycler view. Maybe do you know any examples of popup menu which does not require?
– Tomas Maksimavicius
Nov 7 at 14:52
|
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to use Spinner ( 3 dots with dropdown menu) in my recyclerView, but when I load this recyclerView function onItemSelected is called automatically and later when I press on item it is not called anymore. Here is my code in adapter:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.my_listing_item_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
((SaleAdapter.SalesViewHolder) holder).overflow.setAdapter(adapter);
((SaleAdapter.SalesViewHolder) holder).overflow.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(context, String.valueOf(position) + " item is selected", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public static class SalesViewHolder extends RecyclerView.ViewHolder {
public LinearLayout overflowLayout;
public Spinner overflow;
public SalesViewHolder(View itemView, Context context) {
super(itemView);
overflowLayout = (LinearLayout) itemView.findViewById(R.id.list_item_overflow_layout);
overflow = (Spinner) itemView.findViewById(R.id.list_item_overflow);
}
}
Here is code of View:
<LinearLayout
android:id="@+id/list_item_overflow_layout"
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:padding="15dp"
android:background="@drawable/ic_more_vert_24dp_grey"
>
<Spinner
android:id="@+id/list_item_overflow"
android:layout_width="24dp"
android:layout_height="24dp"
/>
</LinearLayout>
Does anyone know what is the problem here with my code? Should I export setOnItemSelectedListener somewhere else?
android android-recyclerview spinner adapter
I am trying to use Spinner ( 3 dots with dropdown menu) in my recyclerView, but when I load this recyclerView function onItemSelected is called automatically and later when I press on item it is not called anymore. Here is my code in adapter:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.my_listing_item_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
((SaleAdapter.SalesViewHolder) holder).overflow.setAdapter(adapter);
((SaleAdapter.SalesViewHolder) holder).overflow.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(context, String.valueOf(position) + " item is selected", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public static class SalesViewHolder extends RecyclerView.ViewHolder {
public LinearLayout overflowLayout;
public Spinner overflow;
public SalesViewHolder(View itemView, Context context) {
super(itemView);
overflowLayout = (LinearLayout) itemView.findViewById(R.id.list_item_overflow_layout);
overflow = (Spinner) itemView.findViewById(R.id.list_item_overflow);
}
}
Here is code of View:
<LinearLayout
android:id="@+id/list_item_overflow_layout"
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:padding="15dp"
android:background="@drawable/ic_more_vert_24dp_grey"
>
<Spinner
android:id="@+id/list_item_overflow"
android:layout_width="24dp"
android:layout_height="24dp"
/>
</LinearLayout>
Does anyone know what is the problem here with my code? Should I export setOnItemSelectedListener somewhere else?
android android-recyclerview spinner adapter
android android-recyclerview spinner adapter
asked Nov 7 at 13:43
Tomas Maksimavicius
11210
11210
So each item in the recyclerview includes a spinner which wants to mimic the menu dropdown?
– Skemelio
Nov 7 at 14:19
Tomas, yes, you are right
– Tomas Maksimavicius
Nov 7 at 14:43
I believe it's a better idea to implement a pop up menu for every recyclerview item instead of a spinner.
– Skemelio
Nov 7 at 14:48
As the docs say "Providing a drop-down similar to Spinner that does not retain a persistent selection."
– Skemelio
Nov 7 at 14:49
Tomas, but popup menu requires clear coordinates for each item, it is not possible in recycler view. Maybe do you know any examples of popup menu which does not require?
– Tomas Maksimavicius
Nov 7 at 14:52
|
show 3 more comments
So each item in the recyclerview includes a spinner which wants to mimic the menu dropdown?
– Skemelio
Nov 7 at 14:19
Tomas, yes, you are right
– Tomas Maksimavicius
Nov 7 at 14:43
I believe it's a better idea to implement a pop up menu for every recyclerview item instead of a spinner.
– Skemelio
Nov 7 at 14:48
As the docs say "Providing a drop-down similar to Spinner that does not retain a persistent selection."
– Skemelio
Nov 7 at 14:49
Tomas, but popup menu requires clear coordinates for each item, it is not possible in recycler view. Maybe do you know any examples of popup menu which does not require?
– Tomas Maksimavicius
Nov 7 at 14:52
So each item in the recyclerview includes a spinner which wants to mimic the menu dropdown?
– Skemelio
Nov 7 at 14:19
So each item in the recyclerview includes a spinner which wants to mimic the menu dropdown?
– Skemelio
Nov 7 at 14:19
Tomas, yes, you are right
– Tomas Maksimavicius
Nov 7 at 14:43
Tomas, yes, you are right
– Tomas Maksimavicius
Nov 7 at 14:43
I believe it's a better idea to implement a pop up menu for every recyclerview item instead of a spinner.
– Skemelio
Nov 7 at 14:48
I believe it's a better idea to implement a pop up menu for every recyclerview item instead of a spinner.
– Skemelio
Nov 7 at 14:48
As the docs say "Providing a drop-down similar to Spinner that does not retain a persistent selection."
– Skemelio
Nov 7 at 14:49
As the docs say "Providing a drop-down similar to Spinner that does not retain a persistent selection."
– Skemelio
Nov 7 at 14:49
Tomas, but popup menu requires clear coordinates for each item, it is not possible in recycler view. Maybe do you know any examples of popup menu which does not require?
– Tomas Maksimavicius
Nov 7 at 14:52
Tomas, but popup menu requires clear coordinates for each item, it is not possible in recycler view. Maybe do you know any examples of popup menu which does not require?
– Tomas Maksimavicius
Nov 7 at 14:52
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Better use a PopUpMenu for each item in the list. Have an ImageView
instead of the Spinner
which will display a "3 dot" icon. When someone clicks the dots the PopUpMenu will... pop up!
In steps:
1)Replace the Spinner
in the xml layout with an ImageView
which will display the three dots icon.
2) Define a listener class inside your RecyclerView.Adapter<SalesViewHolder>
and save an instance there.
3) Inside SalesViewHolder
set an OnClickListener
to responds to clicks.
4) Pop up from the Activity.
Your new adapter should look something like this
class SalesAdapter extends RecyclerView.Adapter<SalesViewHolder>{
public interface OnMenuItemClickListener{
void onMenuItemClicked(ImageView view);
}
private OnMenuItemClickListener listener;
public void setOnMenuItemClickListener(OnMenuItemClickListener listener){
this.listener = listener;
}
public static class SalesViewHolder extends RecyclerView.ViewHolder {
public LinearLayout overflowLayout;
public ImageView menu;
public SalesViewHolder(View itemView, Context context) {
super(itemView);
overflowLayout = (LinearLayout) itemView.findViewById(R.id.list_item_overflow_layout);
menu= (ImageView) itemView.findViewById(R.id.imageview_id);
// Make sure to enable lambdas
menu.setOnClickListener( (view) -> {
if( listener != null ){
listener.onMenuItemClicked(view);
}
}
}
}
}
Your activity must implement the OnMenuItemClickListener and may look something like this
class YourActivity extends Activity implements SalesAdapter.OnMenuItemClickListener{
// Don't forget to set the listener to the adapter, let's suppose that happens in onCreate()
@Override
public void onCreate(Bundle arg){
SalesAdapter adapter = new SalesAdapter();
adapter.setOnMenuItemClickListener(this);
}
@Override
public void onMenuItemClicked(ImageView menu){
showPopUp(menu);
}
private void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.actions, popup.getMenu());
popup.show();
}
}
Don't forget to supply menu actions etc... as mentioned here.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Better use a PopUpMenu for each item in the list. Have an ImageView
instead of the Spinner
which will display a "3 dot" icon. When someone clicks the dots the PopUpMenu will... pop up!
In steps:
1)Replace the Spinner
in the xml layout with an ImageView
which will display the three dots icon.
2) Define a listener class inside your RecyclerView.Adapter<SalesViewHolder>
and save an instance there.
3) Inside SalesViewHolder
set an OnClickListener
to responds to clicks.
4) Pop up from the Activity.
Your new adapter should look something like this
class SalesAdapter extends RecyclerView.Adapter<SalesViewHolder>{
public interface OnMenuItemClickListener{
void onMenuItemClicked(ImageView view);
}
private OnMenuItemClickListener listener;
public void setOnMenuItemClickListener(OnMenuItemClickListener listener){
this.listener = listener;
}
public static class SalesViewHolder extends RecyclerView.ViewHolder {
public LinearLayout overflowLayout;
public ImageView menu;
public SalesViewHolder(View itemView, Context context) {
super(itemView);
overflowLayout = (LinearLayout) itemView.findViewById(R.id.list_item_overflow_layout);
menu= (ImageView) itemView.findViewById(R.id.imageview_id);
// Make sure to enable lambdas
menu.setOnClickListener( (view) -> {
if( listener != null ){
listener.onMenuItemClicked(view);
}
}
}
}
}
Your activity must implement the OnMenuItemClickListener and may look something like this
class YourActivity extends Activity implements SalesAdapter.OnMenuItemClickListener{
// Don't forget to set the listener to the adapter, let's suppose that happens in onCreate()
@Override
public void onCreate(Bundle arg){
SalesAdapter adapter = new SalesAdapter();
adapter.setOnMenuItemClickListener(this);
}
@Override
public void onMenuItemClicked(ImageView menu){
showPopUp(menu);
}
private void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.actions, popup.getMenu());
popup.show();
}
}
Don't forget to supply menu actions etc... as mentioned here.
add a comment |
up vote
1
down vote
accepted
Better use a PopUpMenu for each item in the list. Have an ImageView
instead of the Spinner
which will display a "3 dot" icon. When someone clicks the dots the PopUpMenu will... pop up!
In steps:
1)Replace the Spinner
in the xml layout with an ImageView
which will display the three dots icon.
2) Define a listener class inside your RecyclerView.Adapter<SalesViewHolder>
and save an instance there.
3) Inside SalesViewHolder
set an OnClickListener
to responds to clicks.
4) Pop up from the Activity.
Your new adapter should look something like this
class SalesAdapter extends RecyclerView.Adapter<SalesViewHolder>{
public interface OnMenuItemClickListener{
void onMenuItemClicked(ImageView view);
}
private OnMenuItemClickListener listener;
public void setOnMenuItemClickListener(OnMenuItemClickListener listener){
this.listener = listener;
}
public static class SalesViewHolder extends RecyclerView.ViewHolder {
public LinearLayout overflowLayout;
public ImageView menu;
public SalesViewHolder(View itemView, Context context) {
super(itemView);
overflowLayout = (LinearLayout) itemView.findViewById(R.id.list_item_overflow_layout);
menu= (ImageView) itemView.findViewById(R.id.imageview_id);
// Make sure to enable lambdas
menu.setOnClickListener( (view) -> {
if( listener != null ){
listener.onMenuItemClicked(view);
}
}
}
}
}
Your activity must implement the OnMenuItemClickListener and may look something like this
class YourActivity extends Activity implements SalesAdapter.OnMenuItemClickListener{
// Don't forget to set the listener to the adapter, let's suppose that happens in onCreate()
@Override
public void onCreate(Bundle arg){
SalesAdapter adapter = new SalesAdapter();
adapter.setOnMenuItemClickListener(this);
}
@Override
public void onMenuItemClicked(ImageView menu){
showPopUp(menu);
}
private void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.actions, popup.getMenu());
popup.show();
}
}
Don't forget to supply menu actions etc... as mentioned here.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Better use a PopUpMenu for each item in the list. Have an ImageView
instead of the Spinner
which will display a "3 dot" icon. When someone clicks the dots the PopUpMenu will... pop up!
In steps:
1)Replace the Spinner
in the xml layout with an ImageView
which will display the three dots icon.
2) Define a listener class inside your RecyclerView.Adapter<SalesViewHolder>
and save an instance there.
3) Inside SalesViewHolder
set an OnClickListener
to responds to clicks.
4) Pop up from the Activity.
Your new adapter should look something like this
class SalesAdapter extends RecyclerView.Adapter<SalesViewHolder>{
public interface OnMenuItemClickListener{
void onMenuItemClicked(ImageView view);
}
private OnMenuItemClickListener listener;
public void setOnMenuItemClickListener(OnMenuItemClickListener listener){
this.listener = listener;
}
public static class SalesViewHolder extends RecyclerView.ViewHolder {
public LinearLayout overflowLayout;
public ImageView menu;
public SalesViewHolder(View itemView, Context context) {
super(itemView);
overflowLayout = (LinearLayout) itemView.findViewById(R.id.list_item_overflow_layout);
menu= (ImageView) itemView.findViewById(R.id.imageview_id);
// Make sure to enable lambdas
menu.setOnClickListener( (view) -> {
if( listener != null ){
listener.onMenuItemClicked(view);
}
}
}
}
}
Your activity must implement the OnMenuItemClickListener and may look something like this
class YourActivity extends Activity implements SalesAdapter.OnMenuItemClickListener{
// Don't forget to set the listener to the adapter, let's suppose that happens in onCreate()
@Override
public void onCreate(Bundle arg){
SalesAdapter adapter = new SalesAdapter();
adapter.setOnMenuItemClickListener(this);
}
@Override
public void onMenuItemClicked(ImageView menu){
showPopUp(menu);
}
private void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.actions, popup.getMenu());
popup.show();
}
}
Don't forget to supply menu actions etc... as mentioned here.
Better use a PopUpMenu for each item in the list. Have an ImageView
instead of the Spinner
which will display a "3 dot" icon. When someone clicks the dots the PopUpMenu will... pop up!
In steps:
1)Replace the Spinner
in the xml layout with an ImageView
which will display the three dots icon.
2) Define a listener class inside your RecyclerView.Adapter<SalesViewHolder>
and save an instance there.
3) Inside SalesViewHolder
set an OnClickListener
to responds to clicks.
4) Pop up from the Activity.
Your new adapter should look something like this
class SalesAdapter extends RecyclerView.Adapter<SalesViewHolder>{
public interface OnMenuItemClickListener{
void onMenuItemClicked(ImageView view);
}
private OnMenuItemClickListener listener;
public void setOnMenuItemClickListener(OnMenuItemClickListener listener){
this.listener = listener;
}
public static class SalesViewHolder extends RecyclerView.ViewHolder {
public LinearLayout overflowLayout;
public ImageView menu;
public SalesViewHolder(View itemView, Context context) {
super(itemView);
overflowLayout = (LinearLayout) itemView.findViewById(R.id.list_item_overflow_layout);
menu= (ImageView) itemView.findViewById(R.id.imageview_id);
// Make sure to enable lambdas
menu.setOnClickListener( (view) -> {
if( listener != null ){
listener.onMenuItemClicked(view);
}
}
}
}
}
Your activity must implement the OnMenuItemClickListener and may look something like this
class YourActivity extends Activity implements SalesAdapter.OnMenuItemClickListener{
// Don't forget to set the listener to the adapter, let's suppose that happens in onCreate()
@Override
public void onCreate(Bundle arg){
SalesAdapter adapter = new SalesAdapter();
adapter.setOnMenuItemClickListener(this);
}
@Override
public void onMenuItemClicked(ImageView menu){
showPopUp(menu);
}
private void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.actions, popup.getMenu());
popup.show();
}
}
Don't forget to supply menu actions etc... as mentioned here.
answered Nov 7 at 16:30
Skemelio
36612
36612
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190683%2fandroid-spinner-in-recyclerview-does-not-work-properly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So each item in the recyclerview includes a spinner which wants to mimic the menu dropdown?
– Skemelio
Nov 7 at 14:19
Tomas, yes, you are right
– Tomas Maksimavicius
Nov 7 at 14:43
I believe it's a better idea to implement a pop up menu for every recyclerview item instead of a spinner.
– Skemelio
Nov 7 at 14:48
As the docs say "Providing a drop-down similar to Spinner that does not retain a persistent selection."
– Skemelio
Nov 7 at 14:49
Tomas, but popup menu requires clear coordinates for each item, it is not possible in recycler view. Maybe do you know any examples of popup menu which does not require?
– Tomas Maksimavicius
Nov 7 at 14:52