I would like to know if there is an easy way of having a grid of elements, where when one of the elements is pressed,
This state:
Changes, to reveal the touched element (center element here)
For a start, I thought manually re-adding the element as over all others, then programmatically setting the position to keep it appear to be centered.
Is there an easier way, or do I have to break a bit of sweat for it?
As far as I know, Android animations don't affect views boundaries. Therefore, you could simply use setScale method of view to scale them up and down. Also, you could considering animations for better UX.
In Android there are two general way for displaying a grid of views:
Grid View
Grid Layout
In both approaches, this trick can be used.
For each cell (individual views) add a touch listener in which whenever the touch is of type down, scale it up and otherwise scale it to normal.
In Grid Views, you should do this in its adapter while for a grid layout you could iterate over its children and apply this.
Related
I am experimenting with a RecyclerView and it currently displays four CardViews vertically on the screen. Using an adapter, I am able to resize each CardView's height equally in the space given.
What I'm trying to accomplish:
On click, I would like the selected RecyclerView child to expand to fullscreen. Currently, I can programmatically set the height and expand the selected CardView dimensions, but the other CardViews after it are pushed down off-screen. How can I have all the selected CardView positioning become absolute, and lock the other views positions and expand "over" them? Is this the proper approach, or should I be looking into shared-element transitions or something else?
Side-ask: Is there a way to control all top/left positioning of RecyclerView children in an adapter?
The comments above seem to be correct - after looking into shared transitions, I found numerous examples performing the exact behavior I described. Crediting #AmratSingh since he answered first.
If it helps anyone, here is the one I am following currently: Michael Scammell - Shared Element Transitions
This one in particular: Shared element transitions within a RecyclerView
Is it possible to animate RecyclerView height, or otherwise change it programmatically? For example, if I have a large header view that is sometimes visible, and other times not - I would want to animate the RecyclerView height to fill the screen when the header is animated out.
Changing LayoutParams.height does not seem to work. LinearLayout animateLayoutChanges causes a crash.
<LinearLayout>
<RelativeLayout (header)>
<RecyclerView>
</LinearLayout>
I want to make the RelativeLayout animate out the top (translationY) and then at the same time make the recyclerview animate to be taller to fit.
There are possible options to tackle this:
Follow suggestion from #Ari to start animation and on every animation tick update layout params. This will make an effect of recyclerview changing its size. However, this is a horrible idea from performance stand point. Layout and Measure process is quite expensive, so generally you want to minimize calls which trigger layout. Call to setLayoutParams will trigger layout & measure process for RecyclerView + all its children which means that on every single frame you will do really expensive work which most likely will lead to framedrop and bad user experience
There is another way though. It might not work in all cases - it all depends on your final layout, but still that's what I would recommend doing. The idea - is to make your recyclerView taller before you start animation. It requires some advanced Android skills though. Basically you need to override onMeasure & layout methods in your RecyclerView (you actually need to extend RecyclerView class to do that).
You can introduce some flag to your recyclerView to measure itself a bit taller than normal (how much taller - the exact height of your header view)
when you need to animate header out - set your flag to true and request new layout. This will re-layout recyclerView with some invisible part at the bottom.
Now you can just animate y translations of both RecyclerView & Header so header moves out of the screen and recyclerview goes higher. This will make user feel like recyclerview "expands"
Once animation is done - set your custom flag to false and change visibility of your header to GONE since it is off screen now
Here is some information about implementing custom onMeasure logic:
https://medium.com/android-news/perfmatters-introduction-to-custom-viewgroups-to-improve-performance-part-2-f14fbcd47c
I want an expandable list in which I can drag a child item and drop it over some other parent list item which will result in child moving from one parent to other. I only need a direction how we can achieve this in general listview (let alone exapandable). Examples are available for picking up a listitem to change the order of listview i.e. to sort that list. How can I accomplish dropping over a view in order to group them.
Your best bet will be putting your effort on customizing and making your own listview, Consider extending AdapterView, this will give you more power over controlling child display and organizations, Putting animation such and drag and drop will be easier for you in this case, Otherwise with simple list view it might work but i doubt if it would generate the required result you want.
Here is a link for exending AdapterView and customizations, go through it it will give you enough confidance to put in your animation. I tried almost similar stuff and was succesfull by same way, unfortunately I dont have implemented code with me.
http://developer.sonymobile.com/2010/05/20/android-tutorial-making-your-own-3d-list-part-1/
Else with list view try doing following,
Your listview should be wrapped inside a framelayout, you will need layers
Enable drawing cache for childrens, coz animation you seek requires playing with bitmaps
Second, when you touch a child in listview, get the bitmap of child, and inflate it at same coordinates of touched child, you can get position of Child easily.
Now time for some animation, you enable drag and drop over inflated bitmap, now when you move it, first thing you need to do is, shifting all the childrens in list view either Up or Down depending upon movement of finger, you can define somekind of threshold like unless half the height of children is moved you wont shift childs in listview up or down.
Moving child will be easy, all you need to do is applying Transformation animation to all the currently visible child in listview, use childCount and ChildAt api for the same, and animation set for playing them together.
Thats it when you build space by shifting child, user will feel like drag drop and shift, all the thing you need, when user places it a place, just modify your dataset underneath listview reposition it based on recent changes by user and refresh it,so that listview reorders itself.
How to create this below view.
1)using listview or
2)inflating custom view in scrollview.
This is not only problem.
If list view then how to overlap the rows.
Each row have different color and that will come from api webservice.
In listview, at each row i have problem with red part which i have marked in second image.
even each row background is different and that same color will appear in below row.
How to do texture effect in listview?
The issue breaks to many things:
How to draw overlapping views?
I would try setting negative values for vertical paddings, but it's a wild guess. Also, you can just divide the graphics and draw views as rectangles containing view N and some parts of the N+1th view.
What about non-rectangular hit area?
You can manually detect clicks using color picking, shapes, or just simplify the view's clickable area to a rectangle.
What about visibility detection?
ListView shows only views, which are visible. I'm not sure, but most likely it won't work with negative paddings or similar hacks.
Personally I would write a custom view with ListView-like adapter and dynamic row loading.
I have try a many ways, and the best and fastest is simple set negative divider to listview like so:
android:dividerHeight="-100dp"
I began to study Android not so long ago and have a question relating to which approach I should use to solve the simple task. Let's suppose I have a view (maybe, a button) and I want user to be able to move it across the screen with a finger. Until AbsoluteLayout was deprecated, the right approach seamed obvious. I would've just changed position of my view based on corresponding events. But what is right now?
Create a custom view of your own and add a onTouch event listener. It is very simple. Explained very well here.
If you're trying to move your views in order to navigate through your application or page through images in a gallery, Android provides a collection of widgets to do things like that. If you're trying to, say, pan across a large image, maybe this will help: Image in Canvas with touch events
Im using FrameLayout and update marginLeft and marginTop
You can add multiple children to frame layout, though this is not recommended because of multiple resolutions issues.
from the android docs:
FrameLayout is designed to block out an area on the screen to display
a single item. Generally, FrameLayout should be used to hold a single
child view, because it can be difficult to organize child views in a
way that's scalable to different screen sizes without the children
overlapping each other. You can, however, add multiple children to a
FrameLayout and control their position within the FrameLayout by
assigning gravity to each child, using the android:layout_gravity
attribute.
This "warning" is not applicable in your case because you are explicitly setting the margins according to the user touch event.