I have an android application that uses AchartEngine to display two graphs. Everything is up and running and I can scroll each chart individually. However, I would like to be able to make the two charts scroll simultaneously, meaning that if scroll one chart, the other gets scrolled by the same amount. Does anyone have any pointers on how to proceed?
Thanks
You can add a listener on scrolling on each chart and change the other chart accordingly, something like this:
For the first chart mChartView_1
mChartView_1.addPanListener(new PanListener() {
public void panApplied() {
mRenderer_2.setRange(new double[] {
mRenderer_1.getXAxisMin(),
mRenderer_1.getXAxisMax(),
mRenderer_1.getYAxisMin(),
mRenderer_1.getYAxisMax()
});
}
});
and for the second chart mChartView_2
mChartView_2.addPanListener(new PanListener() {
public void panApplied() {
mRenderer_1.setRange(new double[] {
mRenderer_2.getXAxisMin(),
mRenderer_2.getXAxisMax(),
mRenderer_2.getYAxisMin(),
mRenderer_2.getYAxisMax()
});
}
});
Should work, but I didn't try it myself.
Add repaint() function to the above 2 code snippets. It will work only when we repaint.
mChartView_2.repaint(); // for mChartView_1 pan listener
mChartView_1.repaint(); // for mChartView_2 pan listener
Its late. But, hope it helps future readers.
Related
I am working on an Android app that runs on only one devicerunning KitKat.
The smooth scrolling feature for a RecylerView I used that was working on other physical tablets and genymotion has unfortunately stopped working on the one device it needs to work on.
Instead of scrolling to a certain position it passes over the target position and scrolls all the way to the bottom and looks really bad.
I am able to track down the error to the abstract SmoothScroller in the RecyclerView class.
if (getChildPosition(mTargetView) == mTargetPosition) {
onTargetFound(mTargetView, recyclerView.mState, mRecyclingAction);
mRecyclingAction.runIfNecessary(recyclerView);
stop();
} else {
Log.e(TAG, "Passed over target position while smooth scrolling.");
mTargetView = null;
}
I was using a SnappingLinearLayoutManager that I found online, but swapped it out with the normal LinearLayoutManager from Android, and still am having the same problem.
The list is 7 items long (user can see 4 at a time) and I scroll to the 5th item (position 4) item.
When I scroll to the 3rd I don't receive this error.
Also after I scroll the list up and down once, the error stops happening.
EDIT:
I am able to use layoutManager.scrollToPositionWithOffset(); But I am trying to do this with the smooth scroll animation.
Here is some of my code and details:
private void setupMainRecyclerViewWithAdapter() {
mainLayoutManager = new SnappingLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mainListRecyclerView.setLayoutManager(mainLayoutManager);
settingsMainListAdapter = new SettingsListAdapter(SettingsActivity.this,
settingsPresenter.getSettingsItems(),
settingsPresenter);
mainListRecyclerView.setAdapter(settingsMainListAdapter);
mainListRecyclerView.addItemDecoration(new BottomOffsetDecoration(EXTRA_VERTICAL_SCROLLING_SPACE));
}
#Override
public void scrollMainList(boolean listAtTop) {
if(listAtTop) {
mainListRecyclerView.smoothScrollToPosition(4);
moveMainMoreButtonAboveList();
} else {
mainListRecyclerView.smoothScrollToPosition(0);
moveMainMoreButtonBelowList();
}
}
If you call recyclerView.smoothScrollToPosition(pos) will be called immediately on the UI thread and if recyclerView's Adapter is too much busy to generating view items then the calling of smoothScrollToPosition will be missed then because recyclerView has no data to smooth scroll. So it's better to do that in a background thread by recyclerView.post(). By calling this it goes into the Main thread queue and gets executed after the other pending tasks are finished.
Therefore you should do something like this which worked for my case:
recyclerView.post(new Runnable() {
#Override
public void run() {
recyclerView.smoothScrollToPosition(pos);
}
});
Well, I realize it's too late, however I tried some different solutions and found one...
in custom LinearSmoothScroller I override updateActionForInterimTarget
#Override
protected void updateActionForInterimTarget(Action action) {
action.jumpTo(position);
}
It's appears not very smooth, but not instant in contrast with scrollToPositionWithOffset.
Just add one line for smooth scroll
recyclerView.setNestedScrollingEnabled(false);
it will work fine
Take a look at hasPendingAdapterUpdates(). You can use this along with a delay() for coroutines or Thread.sleep() to enable the backing data to be available before doing the scroll.
Update: Thx for the answers, problem solved.
Yes code is missing, I am using Mapsforge library.
It had nothing to do with anything else than a bad comparison in a sqlite lookup
which resulted no calling of toggleColor(). Now it works just fine!
Hi I am having trouble changing the color of a circle drawn on a canvas. The other color represents a different state of the circle.
It works fine with onTap i.e when I tap the circle on the screen, but when I try to do it programmatically like
circle.toggleColor() and then
circle.requestRedraw() nothing happens.
How can I make this work programmatically?
#Override
public boolean onTap(LatLong geoPoint, Point viewPosition, Point tapPoint) {
if (this.contains(viewPosition, tapPoint)) {
toggleColor();
this.requestRedraw();
return true;
}
return false;
}
#Override
private void toggleColor() {
if (this.getPaintFill().equals(LongPressAction.GREEN)) {
this.setPaintFill(LongPressAction.RED);
} else {
this.setPaintFill(LongPressAction.GREEN);
}
}
Thx for answering
Yes code is missing in the snippet, I am using Mapsforge library.
It had nothing to do with anything else than a bad comparison in a sqlite lookup which resulted no calling of toggleColor(). Too much time spent on chasing that stupid mistake...
Now it works just fine!
I have 2 MyGameScreen objects that extends cocos2d::CCLayer. I am capturing the ccTouchesMove of the first screen so that I can create the moving effect exactly like sliding between pages of iOS application screen.
My class is like so:
class MyGameScreen: public cocos2d::CCLayer {
cocos2d::CCLayer* m_pNextScreen;
}
bool MyGameScreen::init() {
m_pNextScreen = MyOtherScreen::create();
}
void MyGameScreen::ccTouchesMoved(CCSet *touches, CCEvent *event){
// it crashes here... on the setPosition... m_pNextScreen is valid pointer though I am not sure that MyOtherScreen::create() is all I need to do...
m_pNextScreen->setPosition( CCPointMake( (fMoveTo - (2*fScreenHalfWidth)), 0.0f ) );
}
EDIT: adding clear question
It crashed when I try to setPosition on m_pNextScreen...
I have no idea why it crashed as m_pNextScreen is a valid pointer and is properly initialized. Could anybody explain why?
EDIT: adding progress report
I remodelled the whole system and make a class CContainerLayer : public cocos2d::CCLayer that contains both MyGameScreen and MyOtherScreen side by side. However, this looked like not an efficient approach, as when it grows I may need to have more than 2 pages scrollable side by side, I'd prefer to load the next page only when it is needed rather than the entire CContainerLayer that contains all the upcoming pages whether the user will scroll there or not... Do you have any better idea or github open source sample that does this?
Thank you very much for your input!
Use paging enable scrollview.download files from following link and place in your cocos2d/extenision/gui/ after that you have to set property of scrollview to enablepaging true with paging view size.
https://github.com/shauket/paging-scrollview
For Scene Transitions you can do this:
void MyGameScreen::ccTouchesMoved(CCSet *touches, CCEvent *event)
{
CCScene* MyOtherScene = CCTransitionFadeUp::create(0.2f, MyOtherScreen::scene());
CCDirector::sharedDirector()->replaceScene(MyOtherScene);
}
We are developing an Android application where the requirement is to have single image having different navigation when the different parts of the image are clicked. E.g. A full image of a building and clicking on the different flats we want to show the amenities offered in the flat ( build up area, number of rooms, number of balconies, terrace (if pent house) etc).
Have thought the following two possibilities,
Slice the images and put together (such that it look as a single image) in layout xml, once user clicks it, with the image view id we will know which portion is clicked.
Slice the images and put together (such that it look as a single image) in html with onclick event, load the html in webview, create JavaScript class that handles clicks.
With above two it is difficult because the parts are not proper rectangle or square shaped.
ASP.Net has ImageMap control for this purpose, do we have anything of that sort in Android?
There is no such component to do this in android, but you can easily create one:
public class ImageMap extends ImageView implements onClickListener
{
List<Rect> hotspot;
public ImageMap(Context c)
{
super(c);
hotspot = new List<Rect>();
setOnClickListener(this);
}
public void addHotSpot(Rect r)
{
hotspots.add(r);
}
public void onClick(Event e)
{
int currentPosition = 0;
for(Rect spot : hotspot){
if(spot.contains(e.getX(), e.getY()))
triggerClickInHotSpotAtPos(currentPosition);
currentPosition ++;
}
}
public void triggerHotSpot(int hotspotIndex)
{
//TODO do something useful like calling listener for this given hotspot etc ...
}
}
be aware that is not a finished work, but its more like pseudo code, the important is you got the idea.
You could do this with a single ImageView by tapping into its onTouch event handler.
See the MotionEvent documentation for specifics on how to get coordinates of the touch event.
I have created a custom gallery in which I have implemented the drag and drop mechanism and rearranged the items.Drag happens during a long click event and fling also works fine.Now my problem is I am trying to auto scroll my gallery during a drag operation when a particular threshold value is reached on the left and side of the gallery,for this i tried using scrollto, setselection both of them seems to be working weird,can somebody help me out with this issue.
Instead of :
myGallery.setSelection(position);
Try :
myGallery.post(new Runnable() {
#Override
public void run() {
myGallery.setSelection(position);
}
});
The same thing with scrollTo should also work.
I hope it will help you.