I've got a PopupDateField with a ValueChangeListener.
Is there a way to differentiate if the Event was fired from an .setValue()-Call or an User-Input?
I want the Event to be excuted only if the user changes the value, not if it was changed by program.
I've made a dirty Workaround by removing the ValueChangListener before setting a new Value and adding it afterwards, but I'm thankfull for better solutions...
private static void setDateFieldValue(final PopupDateField dateField, Date value) {
Collection<Property.ValueChangeListener> listeners = (Collection<Property.ValueChangeListener>)dateField.getListeners(Property.ValueChangeEvent.class);
listeners.forEach(new Consumer<Property.ValueChangeListener>() {
#Override
public void accept(Property.ValueChangeListener listener) {
dateField.removeValueChangeListener(listener);
}
});
dateField.setValue(value);
listeners.forEach(new Consumer<Property.ValueChangeListener>() {
#Override
public void accept(Property.ValueChangeListener listener) {
dateField.addValueChangeListener(listener);
}
});
}
Related
I would like to implement changes in the functionality of GestureDetector.java. The easy way should be just to create a custom GestureDetector that extends from the original one, and to implement the changes:
public class CustomGestureDetector extends GestureDetector {
public CustomGestureDetector(OnGestureListener listener, Handler handler) {
super(listener, handler);
}
public CustomGestureDetector(OnGestureListener listener) {
super(listener);
}
public CustomGestureDetector(Context context, OnGestureListener listener) {
super(context, listener);
}
public CustomGestureDetector(Context context, OnGestureListener listener, Handler handler) {
super(context, listener, handler);
}
public CustomGestureDetector(Context context, OnGestureListener listener, Handler handler, boolean unused) {
super(context, listener, handler, unused);
}
#Override
public void setOnDoubleTapListener(OnDoubleTapListener onDoubleTapListener) {
super.setOnDoubleTapListener(onDoubleTapListener);
}
#Override
public void setContextClickListener(OnContextClickListener onContextClickListener) {
super.setContextClickListener(onContextClickListener);
}
#Override
public void setIsLongpressEnabled(boolean isLongpressEnabled) {
super.setIsLongpressEnabled(isLongpressEnabled);
}
#Override
public boolean isLongpressEnabled() {
return super.isLongpressEnabled();
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(ev);
}
#Override
public boolean onGenericMotionEvent(MotionEvent ev) {
return super.onGenericMotionEvent(ev);
}
}
But I would like to override the function onTouchEvent and implement very specific changes so I cannot just implement them here and then call the super class, nor take the whole code for this function and copy it here because it relies on other variables.
In such case, should I create an alternative GestureDetector and then import the custom one in my project, instead of the regular one?
If it matters, the default behavior of the detector is to disable the scrolling function when long pressed it triggered:
case MotionEvent.ACTION_MOVE:
if (mInLongPress || mInContextClick) {
break;
}
But I would like it to still fire while the user is long pressing, and then scrolling.
Also, another problem is that I cannot just copy the code from GestureDetector.java to my own class in my project, since it actually contains many errors and then it won't compile anymore.
Javafx newbie here.. I need help with the best way to populate listview. Here is my setup..
I am developing UI tool that is supposed to track the number of virtual machines running in my environment. I get a callback whenever a machine comes up or goes down. How do I update listview based on that data. Controller code -
public class MainOverviewController implements Initializable
{
#FXML
private ListView<String> devicesListView; // Points to the listview
#Override
public void initialize(URL location, ResourceBundle resources) {
ObservableList<String> items = FXCollections.observableArrayList("Machines connected");
devicesListView.setItems(items);
...
}
Callback code where I am getting the virtual machine notifications -
class VMChangeListener extends vmlistener
{
...
#Override
public void vmStarted(VM vm)
{
vms.add(vm);
}
#Override
public void vmDisconnected(VM vm)
{
vms.remove(vm);
}
Now my question is, whats the best way to update observablelist, items, from vmStarted and vmDisconnected functions. I could pass the observablelist to the VMChangeListener or use some sort of callbacks? Should I do this in seperate thread?
public class MainOverviewController extends vmListener implements Initializable
{
#FXML
private ListView<VM> devicesListView; // Points to the listview
#Override
public void vmStarted(final VM vm)
{
Platform.runLater(new Runnable()
{
#Override
public void run()
{
devicesListView.getItems().add(vm);
}
});
}
#Override
public void vmDisconnected(final VM vm)
{
Platform.runLater(new Runnable()
{
#Override
public void run()
{
devicesListView.getItems().remove(vm);
}
});
}
...
}
I'm using setOnClickListener for listening on the click event on imageButton in two methods, but it's does not fire in my another method,my first listener firing but my second listener does not fire please see my codes :
Class FirstActivity extends BaseActivity
{
#Override
public void onCreate()
{
super.onCreate();
this.methodA();
this.methodB();
}
public void methodA()
{
ImageButton imageButton = (ImageButton) RContextHelper.getActivity().findViewById(R.id.my_location_button);
imageButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//event firing when image button touched
}
});
}
public void methodB()
{
Test test = new Test(this);
test.methodA();
}
}
class Test
{
Context con;
public Test(Context con)
{
this.con = con;
}
public void methodA()
{
ImageButton imageButton = (ImageButton) getActivity().findViewById(R.id.my_location_button);
imageButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//event does not fire when image button touched
}
});
}
protected ActionBarActivity getActivity()
{
return (ActionBarActivity) con;
}
}
As you can guess from the name setOnClickListener sets the new listener and replaces the old one. That is the case with all the set* listeners in Java. If it was addOnClickListener then you could expect that both listeners should be called.
If you want both of them to be called, you can write a composite on click listener and add both of the listeners to it and set the composite listener to the target.
class CompositeListener implements OnEventListener {
private List<OnEventListener> registeredListeners = new ArrayList<OnEventListener>();
public void registerListener (OnEventListener listener) {
registeredListeners.add(listener);
}
public void onEvent(Event e) {
for(OnEventListener listener:registeredListeners) {
listener.onEvent(e);
}
}
}
And then:
CompositeListener composite = new CompositeListener();
composite.registerListener(listener1);
composite.registerListener(listener2);
imageButton.setOnEventListener(composite);
Source
Very confusing to code with two methodA functions. You never call the second one. At least you are not showing code for that. Moreover - as has been said already - there can only be one listener.
A button triggers an action that should only be invoked once. The button is disabled and hidden in the onClick handler before the action is performed:
someButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
someButton.setEnabled(false);
someButton.setClickable(false);
someButton.setVisibility(View.GONE);
performTaskOnce();
}
});
private void performTaskOnce() {
Log.i("myapp", "Performing task");
//Do something nontrivial that takes a few ms (like changing the view hierarchy)
}
Even though the button is disabled immediately, it is nonetheless possible to trigger multiple "onClick" events by tapping multiple times very quickly. (i.e. performTaskOnce is called multiple times). Is seems that the onClick events are queued before the the button is actually disabled.
I could fix the problem by checking in every single onClick handle whether the corresponding button is already disabled but that seems like a hack. Is there any better way to avoid this issue?
The problem occurs on Android 2.3.6, I cannot reproduce it on Android 4.0.3. But given the rarity of 4.x devices it is not an option to exclude older devices.
You could set a boolean variable to true when the button is clicked and set it to false when you're done processing the click.
This way you can ignore multiple clicks and not having to disable the button possibly avoiding annoying flickering of the button.
boolean processClick=true;
someButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(processClick)
{
someButton.setEnabled(false);
someButton.setClickable(false);
someButton.setVisibility(View.GONE);
performTaskOnce();
}
processClick=false;
}
});
private void performTaskOnce() {
Log.i("myapp", "Performing task");
//Do something nontrivial that takes a few ms (like changing the view hierarchy)
}
In the interest of keeping DRY:
// Implementation
public abstract class OneShotClickListener implements View.OnClickListener {
private boolean hasClicked;
#Override public final void onClick(View v) {
if (!hasClicked) {
onClicked(v);
hasClicked = true;
}
}
public abstract void onClicked(View v);
}
// Usage example
public class MyActivity extends Activity {
private View myView;
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView.setOnClickListener(new OneShotClickListener() {
#Override public void onClicked(View v) {
// do clicky stuff
}
});
}
}
Bit late but this might be of use to someone. In my case I am calling another activity so;
Declare a boolean;
boolean clickable;
In the click listener;
if(clickable){
// Launch other activity
clickable = false;
}
Enable when onResume is called;
#Override
public void onResume() {
Log.e(TAG, "onResume");
super.onResume();
clickable = true;
}
You can use RxView(com.jakewharton.rxbinding2.view.RxView) is an extension around RxJava that created by Jake Wharton.
To integrate it to project you should use implementation 'com.jakewharton.rxbinding3:rxbinding:3.1.0'
Simple Java usage:
RxView.clicks(yourButton)
.sample(500, TimeUnit.MILLISECONDS)
.subscribe { action() }
In Kotlin you can create extension function to handle your clicks:
View.singleClick(action: () -> Any) {
RxView.clicks(this)
.sample(500, TimeUnit.MILLISECONDS)
.subscribe { action() }
}
Sample:
Kotlin
yourButton.singleClick({
//do some stuff here
})
Java
SingleClickListenerKt.singleClick(yourButton, () -> {
doSomeStuff();
return null;
});
Note: you can use any RxJava operators like debounce, map, first, etc if you wish.
declare a varieble
and use it as
boolean boo = false;
someButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(boo==false){
someButton.setEnabled(false);
someButton.setClickable(false);
someButton.setVisibility(View.GONE);
boo = true;
}
}
});
by this you prevent multiple clicks on your button
hope it help
I have a couple of CheckBoxPreferences set up, my preference class extends PreferenceActivity and implements OnSharedPreferenceChangeListener
This is what I'm using to respond to people checking/unchecking the CheckBoxPreferences:
public void onSharedPreferenceChanged(SharedPreferences P, String K) {
if (K.equals(CheckBoxPref_KEY_HERE)) {
MyClass.BooleanVariable = P.getBoolean("CheckBoxPref_KEY_HERE", true);
}
}
As far as I can tell, the onSharedPreferenceChanged method above is never even getting called?
Here is the soln if you want to do something on all the preferences:
Create a class member:
SharedPreferences settings;
in your onCreate method:
settings = getSharedPreferences(<your_pref_name>, 0);
settings.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key) {
// Do whatever
}
});
checkBoxPreference = (CheckBoxPreference) this.findPreference("CheckBoxPref_KEY_HERE");
checkBoxPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// do your work here
return true;
}
});