android


RecylerView Button does not trigger


I have a button in my recycler card which is needed to show a popupMenu
However, whenever user clicks on this button, popupMenu does not appear. Even I put a break point inside the menuOptionsButton.Click action, it does not hit.
ImageButton in XML
<ImageButton
android:id="#+id/MoreSettings"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:src="#drawable/ic_more_black_24dp" />
RecyclerViewAdapter.cs
public class RecyclerAdapter : MvxRecyclerAdapter
{
private readonly FragmentActivity _activity;
public RecyclerAdapter(IMvxAndroidBindingContext bindingContext, FragmentActivity activity)
: base(bindingContext)
{
_activity = activity;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
base.OnBindViewHolder(holder, position);
var view = holder.ItemView;
var menuOptionsButton = view.FindViewById<ImageButton>(Resource.Id.MoreSettings);
menuOptionsButton.Click += (s, e) =>
{
var popup = new PopupMenu(_activity, menuOptionsButton );
popup.MenuInflater.Inflate(Resource.Menu.menu,popup.Menu);
popup.Show();
};
}
}
If I do not put popupMenu inside the click action, then each item in the recyclerview shows a popupMenu, but I want to have an action click to show popUpMenu.
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
base.OnBindViewHolder(holder, position);
var view = holder.ItemView;
var menuOptionsButton = view.FindViewById<ImageButton>(Resource.Id.MoreSettings);
var popup = new PopupMenu(_activity, menuOptionsButton );
popup.MenuInflater.Inflate(Resource.Menu.menu,popup.Menu);
popup.Show();
}
UPDATE :
Activity.cs
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.AView, null);
HasOptionsMenu = true;
toolbar = ((MainView)Activity).SupportActionBar;
var recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.ARecyclerView);
if (recyclerView != null)
{
recyclerView.HasFixedSize = true;
recyclerView.Adapter = new RecyclerAdapter((IMvxAndroidBindingContext)BindingContext, Activity);
var layoutManager = new LinearLayoutManager(Activity);
recyclerView.SetLayoutManager(layoutManager);
}
return view;
}
In your RecyclerView.Adapter code, you should be careful when adding a Click delegate inside the OnBindViewHolder method. If you add as a C# anonymous function, then every time that the View shows a new delegate will be added, not set.
First, you should place this line inside the ViewHolder constructor method:
this.menuOptionsButton = view.FindViewById<ImageButton>(Resource.Id.MoreSettings);
Then,
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
base.OnBindViewHolder(holder, position);
var optionsButton = ((YourHolder)holder).menuOptionsButton;
// This avoids adding a Click event more than once
optionsButton.Click -= this.PopupShow;
optionsButton.Click += this.PopupShow;
}
private void PopupShow(object sender, EventArgs e)
{
Toast.MakeText(this.context, "Button clicked", ToastLength.Short).Show();
var popup = new PopupMenu(_activity, menuOptionsButton );
popup.MenuInflater.Inflate(Resource.Menu.menu,popup.Menu);
popup.Show();
}
You can see the Stack Overflow Documentation about Xamarin.Android RecyclerView:
RecyclerView

Related Links

ListView maxWidth is not working - Always 100% width
How to open Facebook profile in Android app with numerical user-id
How to create a NavigationView programmatically [duplicate]
Does anyone know how it works the adaptive states from the exoplayer?
Why is the first option always selected in multiple HTML select in Ionic/Cordova Android app?
Shared library not loaded on Android
What exactly happens when you remove a task from recents?
LIBGDX - adding Kidoz SDK crashes Android app
Context issue when using Anko DSL within customized adapter for ListView
How i can transfer data over voice using ultrasound in android?
image uploading issue using httpurlconnection class
SignalR Android Hubconnection can't start if already connected
ActionBarDrawerToggle won't appear in fragment
In android, how to apply scale animation so that the scaled image appears on top of everything else?
Android: How to prevent video into full screen in WebView?
Google Play/App Store - Is there a way we can put message on the app before we take it down/ unpublish it?

Categories

HOME
awk
time-complexity
linkedin
webdriver
gembox-spreadsheet
jetty
pearson-correlation
github-enterprise
resultset
eip
flatpak
asp.net-mvc-viewmodel
charles
sar
php-mysqlidb
object-storage
scriptella
gmock
tiki-wiki
asymptotic-complexity
angular-translate
soot
wcag
tpm
subclass
finagle
nsurl
getpixel
roblox
raft
xdocreport
modernizr
connection-pool
trigonometry
branching-and-merging
bindingnavigator
placeholder
knime
show-hide
optimizely
convex-optimization
page-replacement
stdmap
libjpeg
juice-ui
change-tracking
taocp
ziparchive
iotivity
dotnetnuke-7
macaulay2
nservicebus5
stream-processing
cycle2
libvlc
character-replacement
java-money
iokit
cakephp-2.7
teaspoon
jama
ss7
codeplex
ios8-extension
mutual-authentication
renderer
getopt
solace-mq
ie8-compatibility-mode
inputmismatchexception
isqlquery
facebook-timeline
diem
appjs
promotion-code
cdonts
vs-android
non-clustered-index
binomial-heap
interprocess
silverlight-oob
wordprocessingml
interop-domino

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App