android, how to avoid the flick when replace fragment with FragmentTransaction after did the popStack first - android-fragments

when adding fragments in the activity, it is done by replace(R.id.fragment_container, theFragment, backStackTag).
the use case is to limit the FragmentTransaction in the backstack, i.e.
[a]->[b]-[c]->[d]->[e], if adding one more [f], it should pop [e] first so the result would be:
[a]->[b]-[c]->[d]->[f]
and the code: does supportFragmentManager.popBackStackImmediate() first to pop the [e], and then do .replace(R.id.fragment_container, theFragment, backStackTag) to show the [f].
but it will show a flick with content of [e] before the [f] is displayed.
is there a way to avoid this flickering?
val theFragment = getFragment()
if (supportFragmentManager.backStackEntryCount > MAX_STACK_DEPTH) {
supportFragmentManager.popBackStackImmediate()
}
supportFragmentManager.beginTransaction()
.addToBackStack(backStackTag)
.replace(R.id.fragment_container, theFragment, backStackTag)
.commit()

adding supportFragmentManager.executePendingTransactions() after the commit() seems does not show the flicking.
not sure if there is better solution tho.

Related

UICollectionView with autosizing cells incorrectly positions supplementary view

I am using a UICollectionView with a Flow Layout and trying to get the collectionView to size the cells appropriately according to AutoLayout constraints.
While the cells work as intended, I am running in to issues with the layout of any supplementary views that I add to the CollectionView.
Specifically, the supplementaryView will be in the wrong position (i.e., the y origin is incorrect) on initial layout, before 'correcting' itself after I scroll.
For reference, here is how I am configuring my cell sizing:
1. Set the collectionViewLayout's estimated item size
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = CGSizeMake(375, 50.0)
layout.minimumInteritemSpacing = 0.0
layout.minimumLineSpacing = 0.0
let view = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
view.backgroundColor = UIColor.whiteColor()
view.alwaysBounceVertical = true
return view
}()
2. Use subclasses of AutoLayoutCollectionViewCell
class AutoLayoutCollectionViewCell: UICollectionViewCell {
override func preferredLayoutAttributesFittingAttributes(layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
layoutIfNeeded()
layoutAttributes.bounds.size.height = systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
return layoutAttributes
}
}
Note that at this point, everything works as intended.
The next step is where we fail.
3. Provide a reference size for a header
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSizeMake(CGRectGetWidth(collectionView.frame), 30.0)
}
My question is: Why does this happen? How can I get this to correct? How am I supposed to handle supplementary views within a collectionView that self-sizes its cells??
I had the same problem, what solved it for me was to subclass UICollectionViewFlowLayout and override the following function:
override func invalidationContext(forPreferredLayoutAttributes preferredAttributes: UICollectionViewLayoutAttributes, withOriginalAttributes originalAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutInvalidationContext {
let context = super.invalidationContext(forPreferredLayoutAttributes: preferredAttributes, withOriginalAttributes: originalAttributes)
context.invalidateSupplementaryElements(ofKind: UICollectionElementKindSectionHeader,
at: [originalAttributes.indexPath])
return context
}

Game Maker bounce code not woking

Okay so I am making one of those scrolling shooter Galaga-type game using Game Maker Studio. I created the first enemy and set up a spawner for them. They are supposed to just fly downwards towards your ship. That worked fine. But when I made the 2nd enemy, I wanted to make it move more slowly and side-to-side. I also wanted to make them bounce off the edges of the screen. But it just won't work. I can't figure what the hell the problem is and it it driving me insane. If anyone has any ideas, please, share them with me. If you need any more info on the game i can provide it. Here is the code for the step event of the 2nd enemy:
// Control the enemy
if (y > room_height+16)
{
instance_destroy();
}
// Die code
if (armor <= 0)
{
instance_create(x, y, o_explosion_center);
instance_destroy();
}
// Bounce off edges
if (x >= room_width-16)
{
hspeed = -1;
}
if (x < 16)
{
hspeed = 1;
}
First of all, you didn't say what wasn't working. The code you posted is correct, everything depends on the expected result.
One issue I can see id if this code is used by the two enemies. You want them to have different speeds, but once they bounce, their horizontal speeds will be 1 because you set hspeed to 1 and -1. When you create them, you should set a move_speed variable, and for the bouncing, write in the step event :
hspeed = -1*move_speed //instead of hspeed = -1
and
hspeed = move_speed //instead of hspeed = 1
This way, they will keep their initial speeds.
For more help, could you please explain what doesn't work and post the creation code ?

Autolayout constraints in TextField of an outlineView

last night a have an autolayout issue. I was googling and try to find something similar in SO. Even the apple doc doesn't point me to the right direction. Maybe my search terms are completely wrong.
Maybe you guys can bring some light into my darkness.
I added a NSOutlineView in storyboard and added some constraints to the NSTableCellView. As you can see, i added a trailing space to Superview of 50:
My example code adds some foo's and bar's into the outlineView by identifyer:
func outlineView(outlineView: NSOutlineView, viewForTableColumn tableColumn: NSTableColumn?, item: AnyObject) -> NSView? {
if tableColumn?.identifier == "NAME_COLUMN" {
cell = outlineView.makeViewWithIdentifier("NAME_COLUMN", owner: self) as NSTableCellView
cell.textField!.stringValue = "foo"
cell.textField!.editable = true
cell.textField!.delegate = self
} else
if tableColumn?.identifier == "VALUE_COLUMN" {
cell = outlineView.makeViewWithIdentifier("VALUE_COLUMN", owner: self) as NSTableCellView
cell.textField!.stringValue = "bar"
cell.textField!.editable = true
cell.textField!.delegate = self
}
return cell
}
But the trailing space will not show up in my running application!
I even try to set the cell display:
cell.needsDisplay = true
cell.needsLayout = true
cell.needsUpdateConstraints = true
or - according to someone on the internet - add "requiresConstraintBasedLayout":
class func requiresConstraintBasedLayout() -> Bool {
return true
}
but all without luck. The trailing space do not appears and the bar's on the right side border looks awful.
How do i use a TableViewCell inside a OutlineView with a trailing space?
Thanks a lot for any kind of hint.
ps

JavaFx can not continually zoom out

I encountered some strange problem with JavaFX.
The use case is: I want to zoom out a swatch.
I implemented a Scroll event handler, like this:
private void handleSwatchScrollAction(ScrollEvent se){
if(se.getEventType().equals(ScrollEvent.SCROLL)){
System.out.println("Enter into handleSwatchScrollAction ");
//The following two lines just want to calculate a ration to scale
double diff = (se.getDeltaY() / 40) * 2;
double diffFactor = diff / 2 / this.fxDial.getRadius() + 1;
System.out.println("diffFactor is: " + diffFactor);
//The following lines is to set scale
this.fxSwatch.setScaleX(diffFactor);
this.fxSwatch.setScaleY(diffFactor);
}
System.out.println("finish handleDialScrollAction");
}
Problem is, when I use scroll to zoom out, the first time zoom out operation works well.
But from the second time, from the view there is no change.
The log looks right, but just no change to display, everything the same as before.
for example, the log is always like this when I continually do zoom out operation(scroll up), no matter the first time or after that:
Enter into handleSwatchScrollAction
(deltaX, deltaY) = (0.0, 40.0)
diffFactor is: 1.005
finish handleSwatchScrollAction
So, why from the view there is no change from the second time to do zoom out(scroll up) operation?
Thank you so much in advance!
You are just setting the scale values to the same value (1.005) every time you scroll. The first time, you will see a zoom of 0.5% or so; but on subsequent scroll events you are not changing the value.
You need something like:
double diffFactor = ... // as before
double scale = this.fxSwatch.getScaleX() * diffFactor ;
this.fxSwatch.setScaleX(scale);
this.fxSwatch.setScaleY(scale);

How to get multiple key presses in single event?

I am creating an application, where "Left Arrow + Down Arrow" press has different behavior ( It is not same as first left arrow and then left arrow ), currently in keyPressEvent event I am getting them one by one in two separate calls.
Is there any way by which I can get multiple keypress in one keyboard event?
Thanks for this. I am posting code for the Python (PyQt) equivalent so that someone else might find it useful.
def keyPressEvent(self, event):
self.firstrelease = True
astr = "pressed: " + str(event.key())
self.keylist.append(astr)
def keyReleaseEvent(self, event):
if self.firstrelease == True:
self.processmultikeys(self.keylist)
self.firstrelease = False
del self.keylist[-1]
def processmultikeys(self,keyspressed):
print keyspressed
I solved the problem by below code.
QSet<Qt::Key> keysPressed;
void Widget::keyPressEvent(QKeyEvent * event) {
m_bFirstRelease = true;
keysPressed+= event->key();
}
void Widget::keyReleaseEvent(QKeyEvent *) {
if(m_bFirstRelease) {
processMultiKeys(keysPressed);
}
m_bFirstRelease = false;
keysPressed-= event->key();
}
Nothing is "at the same time" and I believe in Qt you can't have that type of behaviour (except for modifier keys like shift, alt, etc).
Approach the problem in a different way. When you receive one of the keys, check to see if you received the other in a short while back, say 20ms before.

Resources