How do I trap an event when a scroll bar appears? - events

I have a GWT ScrollPanel whose overflow-y is set to auto. Is there an event that is fired when the panel's scroll bar appears or disappears?

Unfortunately there is no event fired when scroll bars are added to or removed from an element. It's not hard, however, to roll your own:
public class MyScrollPanel extends ScrollPanel {
boolean horizontal = false;
boolean vertical = false;
public native int getScrollHeight() /*-{
return this.#com.mypackage.MyScrollPanel::getElement()().scrollHeight;
}-*/;
public native int getScrollWidth() /*-{
return this.#com.mypackage.MyScrollPanel::getElement()().scrollWidth;
}-*/;
private boolean hasHorizontalScrollbar() {
return getElement().getClientWidth() < getScrollWidth();
}
private boolean hasVerticalScrollbar() {
return getElement().getClientHeight() < getScrollHeight();
}
public void onLoad() {
new Timer() {
#Override
public void run() {
boolean scrollersChanged = false;
if (hasHorizontalScrollbar() != horizontal) {
horizontal = !horizontal;
scrollersChanged = true;
}
if (hasVerticalScrollbar() != vertical) {
vertical = !vertical;
scrollersChanged = true;
}
if (scrollersChanged) {
// Fire event or call onResize()
}
}
}.scheduleRepeating(500);
}
}

Related

Code Error. The modifier public is not valid. Why?

I am making a 2D game in Unity and am trying to make my moveable character stop every time dialogue appears on screen.
I am using the Fungus extension for my dialogue as I'm a newbie to coding. Every thing I try however I run in to problems.
My current issue is that the modifier 'public' is not valid for this item.
Anyone know how this can be fixed? I have attached the code below. I assume the issue is with the public void CantMove() and public void CanMove() lines.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public Rigidbody2D theRB;
public float jumpForce;
private bool isGrounded;
public Transform groundCheckPoint;
public LayerMask whatIsGround;
private bool canDoubleJump;
private bool canMove = true;
private Animator anim;
private SpriteRenderer theSR;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
theSR = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
if(!canMove)
{
theRB.velocity = new Vector2(0, 0);
}
else
{
theRB.velocity = new Vector2(moveSpeed * Input.GetAxis("Horizontal"), theRB.velocity.y);
}
public void CantMove()
{
canMove = false;
}
public void CanMove()
{
canMove = true;
}
//theRB.velocity = new Vector2(moveSpeed * Input.GetAxis("Horizontal"), theRB.velocity.y);
isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, .2f, whatIsGround);
if(isGrounded)
{
canDoubleJump = true;
}
if(Input.GetButtonDown("Jump"))
{
if (isGrounded)
{
theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
}
else
{
if(canDoubleJump)
{
theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
canDoubleJump = false;
}
}
}
if(theRB.velocity.x > 0)
{
theSR.flipX = true;
} else if(theRB.velocity.x < 0)
{
theSR.flipX = false;
}
anim.SetFloat("moveSpeed", Mathf.Abs( theRB.velocity.x));
anim.SetBool("isGrounded", isGrounded);
}
}
'''
Your problem is that your two functions defined for CanMove and CantMove are declared inside of the Update function body... which makes them locally scoped functions which means they can never have public access and can only be called from within the Update function itself.
Move these two functions outside of the Update function body like this...
void Update() {
...
}
public void CantMove() {
canMove = false;
}
public void CanMove() {
canMove = true;
}

How to prevent parent scroll view from scrolling from gesture capturing child

I'm creating a custom control (custom skia canvas) which I would like to embed within a scroll view, at the moment, because the custom control (child element) is capturing gesture input, this means that the parent scroll view will sometimes scroll and sometimes will not on iOS.
So far we've created a custom renderer for the scroll view which we can enable/disable a property on called 'ScrollEnabled', this works perfectly fine on Android and allows us to interact with the child element when we tap and hold on the child, however this solution is not satisfactory for iOS as we aren't getting a cancel trigger from the child element to tell the ScrollEnabled on the parent to be set back to True (therefore allowing the scrollview to scroll again).
ChartView:
public static readonly BindableProperty AllowStackScrollProperty = BindableProperty.Create(nameof(AllowStackScroll), typeof(bool), typeof(TPChartView), false);
public bool AllowStackScroll
{
get { return (bool)GetValue(AllowStackScrollProperty); }
set { SetValue(AllowStackScrollProperty, value); }
}
{
_currentTouchAction = e.ActionType;
if (e.ActionType == SKTouchAction.Cancelled) return;
((MasterDetailPage)Application.Current.MainPage).IsGestureEnabled = false;
if (e.ActionType == SKTouchAction.Released || e.ActionType == SKTouchAction.Exited)
{
AllowStackScroll = true;
this.Chart.OnTouchEnd();
this.InvalidateSurface();
e.Handled = true;
((MasterDetailPage)Application.Current.MainPage).IsGestureEnabled = true; //Enable the menu gestures again
return;
}
TPVector point = new TPVector(e.Location.X, e.Location.Y);
this.Chart.OnTouchBegan(point);
this.InvalidateSurface();
e.Handled = true;
}
ScrollView renderer (iOS)
public class TPScrollViewIOSRenderer : ScrollViewRenderer
{
public new static void Init()
{
}
public bool ScrollDisabled { get; set; } = false;
private string _currentTouchAction { get; set; }
public TPScrollViewIOSRenderer()
{
if (((TPScrollView)Element) == null) return;
((TPScrollView)Element).PropertyChanged += Element_PropertyChanged;
ScrollEnabled = ((TPScrollView)Element).ScrollingEnabled;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
public override void TouchesBegan(Foundation.NSSet touches, UIEvent evt)
{
if (((TPScrollView)Element) == null) return;
UITouch touch = touches.AnyObject as UITouch;
_currentTouchAction = "press";
LongPress(50, resp =>
{
if (resp)
{
Device.BeginInvokeOnMainThread(() =>
{
ScrollEnabled = false;
});
}
else
{
Device.BeginInvokeOnMainThread(() =>
{
ScrollEnabled = true;
});
}
});
base.TouchesBegan(touches, evt);
}
public override void TouchesMoved(Foundation.NSSet touches, UIKit.UIEvent evt)
{
if (((TPScrollView)Element) == null) return;
_currentTouchAction = "move";
base.TouchesMoved(touches, evt);
}
public override void TouchesEnded(Foundation.NSSet touches, UIKit.UIEvent evt)
{
if (((TPScrollView)Element) == null) return;
_currentTouchAction = "end";
Device.BeginInvokeOnMainThread(() =>
{
ScrollEnabled = true;
});
base.TouchesEnded(touches, evt);
}
public override void TouchesCancelled(Foundation.NSSet touches, UIEvent evt)
{
if (((TPScrollView)Element) == null) return;
_currentTouchAction = "canceled";
Device.BeginInvokeOnMainThread(() =>
{
ScrollEnabled = true;
});
base.TouchesCancelled(touches, evt);
}
public void LongPress(int ExecutionTime, Action<bool> OnComplete)
{
var timer = new System.Threading.Timer(
e =>
{
if (_currentTouchAction == "press" || _currentTouchAction == "move")
{
OnComplete.Invoke(true);
return;
}
OnComplete.Invoke(false);
},
null,
TimeSpan.FromMilliseconds(ExecutionTime),
TimeSpan.FromMilliseconds(-1));
}
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.OldElement != null) Dispose();
ScrollEnabled = ((TPScrollView)Element).ScrollingEnabled;
}
private void Element_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "ScrollingEnabled");
{ }
}
}
We just want behaviour on iOS where the internal element only captures gesture on tap and hold and when released, the parent ScrollView takes precedence again and allows scrolling.

Scroll Index method not firing consistently

In a Xamarin.Forms and Xamarin.Android project I create a Custom Render and Adapter for a ListView.
The adapter implements BaseAdapter and ISectionIndexer. The custom render of this control is using FastScroll feature, in Android when you tap this scroll a bubble with a index letter appears. This works fine, but my idea is to have a way to catch the selected index after releasing scroll and that scroll "bubble" disappears.
I thought with the following class (in the GetSectionForPosition method) could achieve that:
public class ListViewconIndexAdapter : BaseAdapter<string>, ISectionIndexer
{
string[] items;
Activity context;
string[] sections;
Java.Lang.Object[] sectionsObjects;
Dictionary<string, int> alphaIndex;
public ListViewconIndexAdapter(Activity context, string[] items) : base()
{
this.context = context;
this.items = items;
alphaIndex = new Dictionary<string, int>();
for (int i = 0; i < items.Length; i++)
{
var key = items[i][0].ToString();
if (!alphaIndex.ContainsKey(key))
alphaIndex.Add(key, i);
}
sections = new string[alphaIndex.Keys.Count];
alphaIndex.Keys.CopyTo(sections, 0);
sectionsObjects = new Java.Lang.Object[sections.Length];
for (int i = 0; i < sections.Length; i++)
{
sectionsObjects[i] = new Java.Lang.String(sections[i]);
}
}
public override Java.Lang.Object GetItem(int position)
{
return position;
}
public override long GetItemId(int position)
{
return position;
}
public override string this[int position]
{
get { return items[position]; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = items[position];
return view;
}
//Fill in cound here, currently 0
public override int Count
{
get { return items.Length; }
}
// -- ISectionIndexer --
public int GetPositionForSection(int section)
{
return alphaIndex[sections[section]];
}
public int GetSectionForPosition(int position)
{ // this method isn't called in this example, but code is provided for completeness
int prevSection = 0;
for (int i = 0; i < sections.Length; i++)
{
if (GetPositionForSection(i) > position)
{
break;
}
prevSection = i;
}
Console.WriteLine(prevSection);
Console.WriteLine(sections[prevSection]);
//Toast.MakeText(context, sections[prevSection], ToastLength.Short).Show();
Xamarin.Forms.MessagingCenter.Send<object,string>(this, "CambioSeccion", sections[prevSection]);
return prevSection;
}
}
I put those Console.writeline for checking the index letter and that Message send is a way to send it back to PCL/NET Standard code (to show an DisplayAlert or something).
But the problem is that method firing is not consistent, for example, sometimes you fast scroll down to 'C' but Console doesn't print anything after releasing it there, but after touching it again where you leave it, it fires up. But sometimes it works like i want, it prints after release the scroll at selected index.
ListView has two different scroll listeners, AbsListView.IOnScrollListener and AbsListView.IOnScrollChangeListener (this one was added in API 23) and a touch listener (AbsListView.IOnTouchListener)
I think based upon your use-case, you are looking for the OnScrollStateChanged and when it goes into idle state and you are not touching the listview, do something (or vice versa).
Example (adjust to your needs of course):
public class MyScrollListener : Java.Lang.Object, AbsListView.IOnTouchListener, AbsListView.IOnScrollListener, AbsListView.IOnScrollChangeListener //(API23)
{
bool touching;
bool scrolling;
public void OnScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
}
public void OnScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
{
}
public void OnScrollStateChanged(AbsListView view, [GeneratedEnum] ScrollState scrollState)
{
switch(scrollState)
{
case ScrollState.Idle:
if (!touching)
{
scrolling = false;
GetSelection();
}
break;
default:
scrolling = true;
break;
}
}
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Up:
touching = false;
if (!scrolling)
GetSelection();
break;
default:
touching = true;
break;
}
return true;
}
void GetSelection()
{
// touch and srolling is done, do something
}
}
Usage:
var scrollListener = new MyScrollListener();
listView.SetOnTouchListener(scrollListener);
listView.SetOnScrollListener(scrollListener);
listView.SetOnScrollChangeListener(scrollListener); // API23

How to implement TabLayout.IOnTabSelectedListener.OnTabUnselected with TabbedPage.ToolbarPlacement="Bottom" - Xamarin Forms?

I just recently used android:TabbedPage.ToolbarPlacement="Bottom". I used to have the following code:
void TabLayout.IOnTabSelectedListener.OnTabUnselected(TabLayout.Tab tab)
{
var playPage = Element.CurrentPage as NavigationPage;
if (!(playPage.RootPage is PhrasesFrame))
return;
var tabLayout = (TabLayout)ViewGroup.GetChildAt(1);
var playTab = tabLayout.GetTabAt(4);
tab.SetText("Play");
tab.SetIcon(Resource.Drawable.ionicons_2_0_1_play_outline_25);
App.pauseCard = true;
}
Anyone knows how can I implement this with ToolbarPlacement="Bottom" ? I have implemented both BottomNavigationView.IOnNavigationItemSelectedListener, BottomNavigationView.IOnNavigationItemReselectedListener but can't find any reference for UnselectedTab if there is any.
Edit:
Previous custom renderer using the default tab position and implementing TabLayout:
namespace Japanese.Droid
{
public class MyTabbedPageRenderer: TabbedPageRenderer, TabLayout.IOnTabSelectedListener
{
ViewPager viewPager;
TabLayout tabLayout;
bool setup;
public MyTabbedPageRenderer(Context context): base(context){ }
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// More codes here
}
void TabLayout.IOnTabSelectedListener.OnTabReselected(TabLayout.Tab tab)
{
UpdateTab(tab);
}
void TabLayout.IOnTabSelectedListener.OnTabSelected(TabLayout.Tab tab)
{
UpdateTab(tab);
}
void TabLayout.IOnTabSelectedListener.OnTabUnselected(TabLayout.Tab tab)
{
var playPage = Element.CurrentPage as NavigationPage;
if (!(playPage.RootPage is PhrasesFrame))
return;
var tabLayout = (TabLayout)ViewGroup.GetChildAt(1);
var playTab = tabLayout.GetTabAt(4);
tab.SetText("Play");
tab.SetIcon(Resource.Drawable.ionicons_2_0_1_play_outline_25);
App.pauseCard = true;
}
void UpdateTab(TabLayout.Tab tab)
{
// To have the logic only on he tab on position 1
if (tab == null || tab.Position != 4)
{
return;
}
if (tab.Text == "Play")
{
tab.SetText("Pause");
tab.SetIcon(Resource.Drawable.ionicons_2_0_1_pause_outline_22);
App.pauseCard = false;
}
else
{
tab.SetText("Play");
tab.SetIcon(Resource.Drawable.ionicons_2_0_1_play_outline_25);
App.pauseCard = true;
}
}
}
}
Current custom renderer using the ToolbarPlacement="Bottom":
namespace Japanese.Droid
{
public class BottomTabPageRenderer : TabbedPageRenderer, BottomNavigationView.IOnNavigationItemSelectedListener, BottomNavigationView.IOnNavigationItemReselectedListener
{
public BottomTabPageRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
{
base.OnElementChanged(e);
// More codes here
}
bool BottomNavigationView.IOnNavigationItemSelectedListener.OnNavigationItemSelected(IMenuItem item)
{
base.OnNavigationItemSelected(item);
UpdateTab(item)
}
void BottomNavigationView.IOnNavigationItemReselectedListener.OnNavigationItemReselected(IMenuItem item)
{
UpdateTab(item);
}
void UpdateTab(IMenuItem item)
{
var playTabId = 4;
var title = item.TitleFormatted.ToString();
if (item == null || item.ItemId != playTabId)
{
return;
}
if (item.ItemId == playTabId)
{
if (title == "Play")
{
item.SetTitle("Pause");
item.SetIcon(Resource.Drawable.ionicons_2_0_1_pause_outline_22);
App.pauseCard = false;
}
else
{
item.SetTitle("Play");
item.SetIcon(Resource.Drawable.ionicons_2_0_1_play_outline_25);
App.pauseCard = true;
}
}
}
}
}
So now my problem is I don't have any idea how will I implement the TabLayout.IOnTabSelectedListener.OnTabUnselected in the new custom renderer.
There is no official stuff for OnTabReselected event for TabbedPage's bottom navigation or
BottomNavigationView because It doesn't use TabLayout.Tab for a start. Many overridden methods of TabbedPageRenderer not being called like SetTabIcon. If you are using IOnTabSelectedListener interface(As your first part of code) you have three methods to use.
void OnTabReselected(Tab tab);
void OnTabSelected(Tab tab);
void OnTabUnselected(Tab tab);
But when it comes to BottomNavigationView interface you have only two methods
void OnNavigationItemReselected
bool OnNavigationItemSelected
So we don't have built in OnTabUnselected method. Here you need to write custom code to make unseleted event.
I have tried this code without using custom renderer using 4 tabs pages & the xaml of tabbed written in MailPage.xaml file. First declare List<string> in App.xaml.cs file to store Title of all tabs
public static List<string> Titles {get;set;}
Add tabs pages title in above list from MainPage.xaml.cs file's OnAppearing method
protected override void OnAppearing()
{
for (int i = 0; i < this.Children.Count; i++)
{
App.Titles.Add(this.Children[i].Title);
}
}
Now go to your MyTabbedPage class in which is available in shared project.
public class MyTabbedPage : Xamarin.Forms.TabbedPage
{
string selectedTab = string.Empty;
string unSelectedTab = string.Empty;
bool isValid;
public MyTabbedPage()
{
On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
this.CurrentPageChanged += delegate
{
unSelectedTab = selectedTab;
selectedTab = CurrentPage.Title;
if (App.Titles != null)
isValid = true;
else
App.Titles = new List<string>();
if (isValid)
{
MoveTitles(selectedTab);
//Pass 0 index for tab selected & 1 for tab unselected
var unSelecteTabTitle = App.Titles[1];
//TabEvents(1); here you know which tab unseleted call any method
}
};
}
//This method is for to moving selected title on top of App.Titles list & unseleted tab title automatic shifts at index 1
void MoveTitles(string selected)
{
var holdTitles = App.Titles;
if (holdTitles.Count > 0)
{
int indexSel = holdTitles.FindIndex(x => x.StartsWith(selected));
holdTitles.RemoveAt(indexSel);
holdTitles.Insert(0, selected);
}
App.Titles = holdTitles;
}
}
Or you can make swith case like this
void TabEvents(int index)
{
switch (index)
{
case 0:
//Tab selected
break;
case 1:
//Tab unselected
break;
}
}
Few things I should mention that MainPage.xaml.cs file inheriting MyTabbedPage
public partial class MainPage : MyTabbedPage
Structure of MainPage.xaml file
<?xml version="1.0" encoding="utf-8" ?>
<local:MyTabbedPage
<TabbedPage.Children>
<NavigationPage Title="Browse">
</NavigationPage>
</TabbedPage.Children>
</local:MyTabbedPage>
Answer seems long but hope it help you.
As per G.Hakim's suggestion, I was able to do what I wanted to do by capturing the tab item I wanted to work on and do the necessary actions in BottomNavigationView.IOnNavigationItemSelectedListener.OnNavigationItemSelected.
namespace Japanese.Droid
{
public class BottomTabPageRenderer : TabbedPageRenderer, BottomNavigationView.IOnNavigationItemSelectedListener, BottomNavigationView.IOnNavigationItemReselectedListener
{
// same as above
bool BottomNavigationView.IOnNavigationItemSelectedListener.OnNavigationItemSelected(IMenuItem item)
{
base.OnNavigationItemSelected(item);
if(item.ItemId == 4 && item.TitleFormatted.ToString() == "Play")
{
item.SetTitle("Pause");
item.SetIcon(Resource.Drawable.ionicons_2_0_1_pause_outline_22);
App.pauseCard = false;
playTab = item;
}
if(item.ItemId !=4 && playTab.TitleFormatted.ToString() == "Pause")
{
playTab.SetTitle("Play");
playTab.SetIcon(Resource.Drawable.ionicons_2_0_1_play_outline_25);
App.pauseCard = true;
}
return true;
}
// same as above
}
}

ObservableCollection only updating on Map Zoom and Move events not adding to Collection!

I have an issue with a collection that I have bound. I have a manual refresh button that pulls some moving pushpins from the server. The server is moving the pins itself. After processing I delete the existing collection and re add it to the Observable Collection. This code works and I have verififed that the contents have been update however the pins only "update" (move on the map) if a Zoom or move of the map has happened!
My class is as follows...
public class MapData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaisedPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private GeoCoordinate mapCenter = new GeoCoordinate(50, -1);
public GeoCoordinate MapCenter
{
get { return this.mapCenter; }
set
{
if (this.mapCenter == value) return;
this.mapCenter = value;
this.RaisedPropertyChanged("MapCenter");
}
}
private double zoom = 7.0;
public double Zoom
{
get { return this.zoom; }
set
{
if (this.zoom == value) return;
this.zoom = value;
this.RaisedPropertyChanged("Zoom");
}
}
public ObservableCollection<Plane> pins = new ObservableCollection<Plane>() {
};
public ObservableCollection<Plane> Pins
{
get { return pins; }
}
public void RemovePoints()
{
for (int i = 0; i < pins.Count; i++)
{
pins.RemoveAt(i);
}
pins.Clear();
this.RaisedPropertyChanged("Location");
}
public void AddPoints(List<Plane> Planelist)
{
for (int i = 0; i < Planelist.Count; i++)
{
pins.Add(Planelist[i]);
}
}
private Plane selectedPin;
public Plane SelectedPin
{
get {
return this.selectedPin;
}
set
{
if (this.selectedPin == value) return;
this.selectedPin = value;
this.RaisedPropertyChanged("SelectedPin");
}
}
private LocationCollection routePoints = new LocationCollection();
public LocationCollection RoutePoints
{
get { return routePoints; }
}
}
And it is bound using the following...
<my:MapItemsControl ItemsSource="{Binding Pins}" ItemTemplate="{StaticResource PushedMe}"/>
After speaking with Microsoft it appears that the device will cache URL's and that is actually my issue! Forced no-cache on the server side and issue fixed!

Resources