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

Users send push notification, firebase cloud messaging
Error testing meteor application in Android studio/on physical device
Update and refresh ListView item when another Activity finishes
JSon Parse from Wiki
How to reset a click counter?
Android device not showing on adb anymore
How to access Viewholder from Fragment activity in landscape mode
java.exe eror (Xamarin C#)
How to update webview?
How to set string on spinner?
Unregistering a BroadcastReceiver outside of a subscription when activity is destroyed
Android - Add image on top of Showcase
Otto Event Bus object post limit
Is it possible to run sqlite query in the backgroud thread and sending an email at the UI with output of query as a TO field
Adding custom header to WebView by adding headers to WebResourceRequest's headers in shouldInterceptRequest doesn't work
Android Studio hangs when gradle build fails

Categories

HOME
android
django
gridview
polymer
c++11
ns-3
battery
package
ms-access-2016
newrelic
rom
telegram-bot
settings
android-json
missing-data
retrofit2
recordrtc
p2p
mel
automatic-ref-counting
alpacajs
sse
nim
connector
topology
asp.net-core-webapi
googlebot
kendo-mobile
ios10.2
visual-studio-extensions
tosca
zxing
flask-sqlalchemy
google-cloud-print
pic32
pylons
has-and-belongs-to-many
user-defined-functions
kudu
dumpbin
contacts
twos-complement
reverse
imx6
latitude-longitude
openproject
git-rewrite-history
release
health-monitoring
parser-generator
xmldocument
berkeley-db-je
photos
apache-spark-dataset
fclose
vugen
boo
quicktime
payload
cpu-cache
restore
choice
tinkerpop3
xdoclet
observablecollection
shopping
http-status-code-401
cassia
redis-cluster
uiactionsheet
fink
microsoft-reporting
ember-cli-rails
jdk1.7
j-security-check
code-documentation
webmatrix-3
jbake
post-commit-hook
spy++
menubar
xmltodict
eclipse-kepler
c++builder-xe5
inputmismatchexception
pagedown
boost-msm
wcf-configuration
azman
infobright
cgimage
apache-commons-dbutils
mtu
interprocess
burndowncharts
build-environment
silverlight-oob
lalr
sqlobject

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