Getting buttons position in fragment with viewPager - android

I'm building an app with 2 fragments (with ViewPager). Each fragment consists of 4 buttons. I use also onTouch listener with these buttons. I use getLocationOnScreen(...) method to get the view's (button) position on screen. But as I slide from first fragment to the second one, the location of the next 4 buttons is relative to the first fragment. I mean, the x coordinate of these buttons is taking into consideration the previous fragment width.
My question is - how do I get the second fragment's buttons position relative to this fragment page and not the previous one?
Thanks!
EDIT : Added a code snippet:
Here is how i keep my buttons position on screen into a hash-map collection (this is done for each of the two fragments i have):
public void getViewsPosition(final ArrayList<View> viewArr) {
for (final View v : viewArr) {
final ViewTreeObserver vto = v.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int[] i = new int[2];
v.getLocationOnScreen(i);
_rect = new Rect(i[0], i[1], i[0] + v.getWidth(), i[1]
+ v.getHeight());
view_to_rect.put(v, _rect);
if (Build.VERSION.SDK_INT < 16) {
vto.removeGlobalOnLayoutListener(this);
} else {
vto.removeOnGlobalLayoutListener(this);
}
}
});
}
}
This keeps the correct coordinates for the first fragment's buttons. But for the second fragment, the x-coordinate is relative to the first fragment. So I get the button's x coordinate within this fragment PLUS the first fragment width. The y-coordinate is, of course, correct within all fragment.

Related

Programmatically add items to scrollview top, but should not scroll to top

I have scrollview with child LinearLayout . I am adding data programmaticaly to it. When i add some data to top of linearlayout it automatically scrolls to top element. But i want something like , user reaches top -> scrolls upside to load previous data ->add data to linearlayout top but should not get focus, after addition complete , if user scrolls then and then only it should display .
How to achieve this?
Well I thought of a way and it works almost perfectly.
I have a LinearLayout (llCommunicationsLayout) inside a ScrollView (svCommunications) .
I inflate a new LinearLayout, I'm going to add views to the top of this new LinearLayout and then add this new layout to the LinearLayout inside the ScrollView.
This is the new layout:
final LinearLayout wrapperLayout = (LinearLayout) mInflater.inflate(R.layout.empty_layout, null);
I add my views to the 0'th position of this layout.
wrapperLayout.addView(view, 0);
After all the views are added into the wrapperLayout, I add the wrapperLayout into the llCommunicationsLayout (the one inside my ScrollView)
llCommunicationsLayout.addView(wrapperLayout, 0);
After this, I calculate their heights after the wrapperLayout is on screen (has a measurable height)
wrapperLayout.post(new Runnable() {
#Override
public void run() {
int wrapperHeight = wrapperLayout.getHeight();
int svHeight = svCommunications.getHeight();
int scrollHeight = Math.abs(svHeight - wrapperHeight);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int displayHeight = displaymetrics.heightPixels / 4;
svCommunications.scrollTo(0, (scrollHeight + displayHeight));
}
});
Here, I get both the newly added layout's and the ScrollView's heights.
I calculate their difference, add the 1/4 of the height of the screen of my device and scroll it, voila!
It's not perfect, but after the layouts are added, it no longer scrolls to the top of the screen. Experiment with the displayHeight for different results.
Hope this helps someone out.
You can grab the current view which is on top of your LinearLayout then add new content to your LinearLayout and then scroll back to view which was previously on top. The code would be something like:
public void addViewsOnTop(List<View> views) {
final View currentViewOnTop = (linearLayout.getChildCount() > 0) ? linearLayout.getChildAt(0) : null;
// Add Views. Note that views will appear in reverse order
for(View view : views) {
linearLayout.addView(view, 0);
}
// Scroll back to view which was on top
if(currentViewOnTop != null) {
new Handler().post(new Runnable() {
#Override
public void run() {
scrollView.scrollTo(0, currentViewOnTop.getBottom());
}
});
}
}
Solved....
Try this, worked for me ,
lv_chat.setAdapter(adapter);
lv_chat.setSelection(somePreviousPosition);

Android how to calculate amount of elements on a page

Part of application I'm currently working on act exactly as android shell, shows several pages filled with icons with text labels. User could slide between pages to find needed element. I'm using PagerView with GridView in each page.
Content of each page should fit exactly of visible area, no scroll. The question how to calculate number of icons on each page?
The issue next, I can't call pagerView.getHeight(), I'll have 0 in result because actual layout calculation wasn't executed yet.
UPDATED:
Seems I wasn't able to describe my problem well, I'll try to provide more simple case, suppose I do have activity with status bar at the top and some button bar at the bottom, both fixed height. Whole remaining area in the middle is used by GridView.
Grid view should show rectangular icons, and what I need to calculate is how many icons it could show without scroll (because remaining icons will be shown on next activity).
You can try something like:
public int countElements(ViewGroup group) {
int count = 0;
for (int i = 0; i < group.getChildCount(); i++) {
View v = layout.getChildAt(i);
Class c = v.getClass();
if (c == *Icon*) {
count++;
}
}
return count;
}
In case you have inner views you can check if c is a GroupView and call the method recursively.
As for your second issue, try using this in onCreate:
private void calculateSize(int height, int width){
int rows = Math.floor(height/imageHeight);
int columns = Math.floor(width/imageWidth);
//do something with numbers
}
ViewTreeObserver vto = pagerView.getViewTreeObserver();
if(vto.isAlive()){
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
vto.removeGlobalOnLayoutListener(this);
calculateSize(gridView.getHeight(), gridView.getWidth());
}
});
}

Android: call requestLayout() after update v.layout parameter causes layout reverts back to it's original position

I have framelayout which contains two relative layouts, one is on top of the other. When user clicks a button, the one on the top move 80% off the screen to the right. Then one on the bottom becomes clickable. This is what it looks like.
FrameLayout
RelativeLayout (bottom) RelativeLayout (top)
FilterWidgets Open/close button, ListView
It's really easy to achieve on 3.0+ with the new animation api which is Property base Animation. For the pre 3.0, because animation is view based. So I end up manually modify the layout property on onAnimationEnd. The call requestLayout to make it permanent, but only to find out the layout reverts back to original position. Anybody know how to move layout permanently?
see my other post if you want to see the whole picture:
https://stackoverflow.com/questions/14541265/changecursor-cause-layout-container-of-the-listview-to-reposition
theTranslationX.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator nullPointer) {
v.clearAnimation();
int theL = isMenuOn() ? 0 : v.getLeft() + getFilterWidth();
int theR = isMenuOn() ? v.getWidth() : v.getLeft() + getFilterWidth() + v.getWidth();
int theHeight = v.getHeight();
int theT = 0;
v.layout(theL, theT, theR, theHeight);
v.requestLayout();
}
});
This is 9 months late but try using:
yourView.layout(left,top,right,bottom); //all parameters are type int
However I don't think this is permanent, the position of the view will still be reset when you call requestLayout(), but give it a try.

Android layout resetting on redraw after an animated move

I have a listview in a fragment container.
Each view within the list has a gesture detector which on gesture up invalidates the list for redraw.
After an item selection I animate the fragment container to another part of the screen
ObjectAnimator x = ObjectAnimator.ofInt(ListLayout, "left", ListLayout.getLeft(), ListLayout.getLeft() - 336);
x.setDuration(500);
x.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
View ListLayout = findViewById(R.id.fragment_list_container);
Log.d(TAG, "ListLayout x after move completed = " + ListLayout.getLeft());
}
});
x.start();
All good to this point.
However if I select another item from the list (list should not move this time so animation not called) when the gesture up is detected and the list invalidated it reverts to it's original position?
If I getLeft before and after the initial animated move it returns the correct positions.
Any help or insight gratefully received,
Slip
For those that run into this problem it was simply because I was using "left" rather than translating the value.
I ended up compacting everything and using the new feature
mLayout.animate().setDuration(ANIMATE_TIME_MILLIS).translationXBy(ANIMATE_MOVE_DISTANCE);

Scroll to particular view

I have home activity on which I have 3 buttons : Move To A , Move To B and Move To C.
On click of these buttons, I navigate to inner activity where I have 3 layouts within a HorizontalScrollView, say A, B and C which in turn consists of several views.
Now what I want is , inner activity should navigate to one of the 3 layouts based on clicked button from Home activity i.e.
If Move To A on home activity is pressed, inner activity should navigate to Layout A and so on...
Possible solution might be to use ScrollTo() but it isn't efficient with variable screen sizes.
Any help appreciated.
Edit
This doesn't work:
RelativeLayout LayoutA = (RelativeLayout) findViewById(R.id.LayoutA);
HorizontalScrollView main = (HorizontalScrollView) findViewById(R.id.scrollview);
main.scrollTo(PoCLayout.getLeft(), 0);
Even this doesn't work:
main.scrollTo(500, 0);
If main.scrollTo(500,0) does not work, try :
main.post(new Runnable() {
#Override
public void run() {
main.scrollTo(500, 0);
}
}
Edit : the working solution in the listener of the button :
main.post(new Runnable() {
#Override
public void run() {
main.scrollTo(layoutX.getLeft(), 0);
}
}
I'm not sure it will work, but you can try to get the position of your layout A, B or C calling the method getLeft() and then use scrollTo().
Example to scroll to layout A : scrollTo(a.getLeft(), 0);
You can do a little Math here
Use View.getLeft() and View.getRight() then divide calculated width per 2 to scroll to the View center
Use :
final HorizontalScrollView scrollView= (HorizontalScrollView)
int speed=1000;
findViewById(R.id.main_scrollView);
final View view = findViewById(R.id.your_view);
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() { view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
ObjectAnimator.ofInt(scrollView, "scrollY", (int) view.getX()).setDuration(speed).start();
}
});
main.post(new Runnable() {
#Override
public void run() {
main.scrollTo(500,500);
}
}

Categories

Resources