Horizontal Scrollview with snap effect - java

I need a widget that functions as a horizontal scrollview, but snaps to the nearest value when scrolled (it should snap from value to value, and not be possible to stop the scrolling in between values). Theoretically I would solve this with a viewpager with negative margins - so that all the views are simultaneously visible, but it also "snaps". But I have a problem - if I use a viewpager then only the "foreground" (i.e. currentView) view is "clickable", and I need all onscreen visible views to be clickable at any given time. So - is there any way of having a "snapping" horizontal scroll widget where all visible views are clickable?
For example, the screen might look like this:
A B C D E
where scrolling right reveals F, G, H..
initial state should allow clicking on any of A,B,C,D,E and not just on A (if it was the "currentview")

You can use Snap ScrollView library. It supports both horizontal and vertical snap scrolling. You can add views directly. Click on this SnapScrollView library
And regarding click, you can implement onClick listener for each view and call correspondingly.

Related

Android ScrollView jumps up after changing child element visibility

I have a ScrollView that contains elements with visibility set to View.GONE by default. The problem is that whenever this visibility changes from View.GONE to View.VISIBLE, scroll jumps up as it is shown on second picture:
https://i.imgur.com/WmRc4M2.png
The red box represents current scroll position, blue rectangle is an element which visibility is changed, and green rectangle is some element (for example button) that I can refer to. I want my ScrollView to work as it is shown on third picture. If green button is on the screen, it should also be visible on screen after displaying new element (the blue one) above it. Any ideas how can I achieve it?
well, it behaves exacly as it should - scroll position is on e.g. 200px from top and this doesn't change when new item changes its visibility
you should scroll by yourself to proper (new) point, use scrollTo method - measure height of this new View and call scrollView.scrollTo(0, currentScrollY + heightOfNewView)
be aware that when you change visibility then you can't call above scrollTo just one line below, you have to wait for this View being measured and drawn. so you should call scrollTo in e.g.
visibilityChangedView.setVisibility(View.VISIBLE);
visibilityChangedView.post(new Runnable() {
// this will be called when visibilityChangedView won't have
// any pending tasks to do, but we just changed visibility,
// so at first view will be measured and drawn, after that
// below run() method will be called
#Override
public void run() {
int heightOfNewView = visibilityChangedView.getMeasuredHeight();
int currentScrollY = scrollView.getScrollY();
scrollView.scrollTo(0, currentScrollY + heightOfNewView);
}
);
there is a chance that this automatic scroll will be visible to user as quick jump - at first whole view will be drawn as #2 and after split second/one frame it will redraw at new position like #3. I don't know how your content is built (rest of childs in Scrollview), but I suspect that this should be RecyclerView or at least ListView with proper Adapter, not ScrollView with fixed layout
You can use NestedScrollView. It did the job.
Put your scrollView inside constraint layout and set all constraints to parent.

How to resize image as I scroll?

I have a ScrollView in which I have inserted a ConstraintLayout that contains a fullscreen ImageView and some other components below it.
What I want is for the image to shrink in height (to a certain limit) whenever I scroll down.
Here's an example of what I'm seeking: https://imgur.com/rlOr0HA
As for resizing, after some research I figured I'd have to create a LayoutParams object and then affect it to the ImageView. No problem there.
But as for detecting the scroll event, I've had some trouble. I tried the setOnScrollChangeListener on my ScrollView, but the problem is that it requires an API level of at least 23. So I wonder if there is another solution that works for lower levels as well.
Another problem I'm having is how to make the resizing proportionate to how much the user has scrolled.
You are looking to do the coordinator layout with a collapsing toolbar. This is built into android and you do not need any code changes. The inflater will inflate the layout and everything will work.
In your xml layout file you will need
<coordinatorlayout
<Appbar layout
<collapsingtoolbar
<ImageView> <-- your image goes here
/collapsingtoolbar
/Appbar layout
/coordinatorlayout
<nestedscrollview
<textview> <--Your content that moves up goes here
/nestedscrollview
<floating action button> <-- your example shows one of these buttons but its optional

Layout scrolling ripple effect

How could this scrolling behavior be accomplished?
Screenshot
To be specific, I need the wave effect to be shown whenever I try to scroll and reach the limit of a layout. Is there some property that I can tweak?
To get this wave effect, use any horizontally or vertically scrollable view as a container layout in your layout XML.
For example: ScrollView, HorizontalScrollView, NestedScrollView
List and Grid layouts like ListView, RecyclerView also has this scrolling effect.

List view with horizontal scroll

I am using custom list view with base adapter. The width of row is longer than screen. But I am not getting horizontal scroll bar like automatic vertical scrollbar. Do I need to specify any extra attribute? I tried with putting listview in horizontal scroll view but still no success. How should I achieve this?
Thanx in advance.
You might just need to use android:layout_width="wrap_content" in your horizontal scroll. You should also show your XML layout in case it is just some other minor change, like having the list view inside the horizontal scroll view.
As a side note, you won't get a horizontal scroll bar but the views inside the horizontal scroll view will slide left/right if their width is wider than the screen.

How to set the contents of a vertical scrollview depending on what button is chosen from a horizontal scroll above?

I was just wondering if any of you guys could give me a heads up as to what this involves?
I have used ScrollViews, I know they need a LinearLayout to scroll through and that isn't the problem.
It is that there is a dynamic horizontal ScrollView containing several buttons with buttons can be added/removed.
I also have a vertical scrollview.
The GOAL is to set the contents of the vertical ScrollView depending on which button is pressed in the above horizontal ScrolLView.
Any direction/insight would be appreciated.
Cheers
In ur Horizontal scroll view U have to set tag for individual views. Then in on click event of a view u can populate the vertical scroll by required data. I thinks for ur vertical scroll u can use the listview. It will be easy to handle. Also if ur horizontal things are in more numbers then its better to use the ListView for that even..

Categories