Add Fixed button inside scrollview which is visible even when scrolling - android

I want to make a view( a button actually) which is inside a scrollview , but when the user scolls down and the button is going up, it moves up only till it reaches the top of the visible screen and then stays there like a fixed header until the user scrolls up again and then it returns to its original position.
I have given the screens for a better understanding.

One way I have solved this is by copying the same view outside the scrollview and keeping it hidden. Only to make it visible when the old button is visisble again.
#Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
int oldx, int oldy) {
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (mPriceBtn.getLocalVisibleRect(scrollBounds)) {
// View is within the visible window
mPriceHiddenBtn.setVisibility(View.GONE);
} else {
// View is not within the visible window
//mPriceBtn.setY(y);
mPriceHiddenBtn.setVisibility(View.VISIBLE);
}
}

Check out this tutorial. It's about an ActionBar with similar behavior, made by ome of Google's own Android devs. I'm guessing you can find most of what you need there.

Related

Position of views on the screen

How to detect if cardview located in scrollview(linearlayout) went beyond the screen Or returned back to the field of view?
I tried to do this through OnScrollChanged But it seems to me that this is not the case, and it is impossible to get the correct algorithm of work
You have to use the method getLocalVisibleRect() on your CardView:
Rect scrollViewRect = new Rect();
// Get the visible Rect of the ScrollView.
scrollView.getHitRect(scrollViewRect);
// Check if CardView's Rect is inside it.
if (!cardView.getLocalVisibleRect(scrollViewRect)) {
// The CardView is not visibile anymore.
}
You can place this code wherever you want to check if the CardView is not visible anymore on the screen.

How to know a view is showing in physical screen or not?

I want to know when a view of my layout is showing in physical screen. You know in some apps that uses ScrollView, you must scroll down to see other views. I want to know when a view like Button or TextView or another view is showing in physical screen to do some job. Thanks in advance:)
Try this:
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (view.getLocalVisibleRect(scrollBounds)) {
// Any portion of the view, even a single pixel, is within the visible window
} else {
// NONE of the view is within the visible window
}

Android facebook bottom bar animation

I want to implement a slide up/slide down animation on bottom bar when scrolling the screen up/down.
Implementation has to be like in facebook app when the user slide up/down the bottom options bar slowly slides up/down .
I tried using scrollview to get the scrollchanged method to identify the scrolling behaviour of user and hide the bar based on the events.the tricky part is how to animate it slowly based on user slide up/down.
tried using moveup/move down animation but it was not fluid as the next animation starts when the first one ends . so the movement of bottom bar is more animated.
The proper name to that behavior is "quick return pattern" and you can implement it with this gist:
https://gist.github.com/JohNan/df776dc4926a1676cc05
Referring from the post of #baniares
https://gist.github.com/JohNan/df776dc4926a1676cc05
Used the on scroll method to compute the scroll of slideup/down and settranslate method to update my view.
public void onScrollChanged(ObservableScrollView scrollView, int newX,
int newY, int oldX, int oldY) {
if (scrollup)
{
<View>.setTranslationY(0);
}
else
{
<View>.setTranslationY(position+1);
}
}
The value 0 should be used in .settranslateY to bring to original postion of view. Increasing the value will translate the view to make it slide down.

Vertical MenuDrawer and ListView scrolling up issue

I have a problem which I am not sure how can I handle.
I started using a library called MenuDrawer on which I have given the following:
mDrawer.setTouchMode(MenuDrawer.TOUCH_MODE_FULLSCREEN);
Which allows it to pop open whenever I drag the screen down, but this actually rises another issue for me, because I use a ListView there that needs to be scrolled also.
I actually have 2 questions, taking in regards that not all may have used this library.
If you have used this library: Is it possible to focus the dragging on just 1 element? For example if I have a TextView there can I drag only that item to open the menu?
If you haven't used this library: How can a child element steal the focus from the parent on touch event? So that when I touch and drag somewhere where the listview is to achieve a successful scrolling up and down.
Right now I can scroll freely only down and when I try to scroll up opens the menu, unless I scroll the listview first down and up without breaking contact with the screen.
I found how to manage that. For me this simple piece of code worked like a charm:
mDrawer.setOnInterceptMoveEventListener(new OnInterceptMoveEventListener(){
#Override
public boolean isViewDraggable(View v, int dx, int x, int y) {
//v.getId();
if(y > 50){
return true;
}
return false;
}});
if anything below Y 50 is pulled it won't trigger the menu drawer.

Refreshing custom view layout position

I have to implement slidable menu in my app. User should be able to show/hide it by tapping on it and move left/right.
To implement this, I`ve created custom SlidableFrameLayout that extends FrameLayout.
By overriding onTouchEvent method I calculate slide position and call
private void setLeftLayout(int left) {
layout(left, getTop(), getRight(), getBottom());
}
to change position for SlidableFrameLayout. Everything works fine, but when I keyboard shows (after focusing EditText), SlidableFrameLayout changes its left value to 0, so if menu was closed, it becomes opened. The same thing happens when I hide the keyboard (generally, it appears in all actions that prevents calling layout methods, I think).
I can not invent how to fix it. Could you help me? If any additional info is required, I`ll attach it. Thanks
I think your issue is related to calling layout directly. When the container view changes size as a result of the keyboard being shown/hidden onMeasure/onLayout will be called with the view's default bounds. In your case I'm guessing the left bound is 0, so layout is being called with 0 for left every time your window resizes.
Instead you probably want to use something like setTranslationX or scrollTo ..
Good luck!
As a result, I changed my logic:
private void setLeftLayout(int left) {
FrameLayout.LayoutParams params = getFrameLayoutParams();
params.leftMargin = left;
params.rightMargin = -left;
requestLayout();
}
In my case, view is a parent of FrameLayout, so I haven`t added any conditions in 1st line of the method. This method works pretty fine.

Categories

Resources