I'm trying to align some items inside a RecyclerView with others outside it. However, seems like all my tries are failing.
What I'm trying to do specifically: I have a couple of views as a header which are aligned using various methods* then a RecyclerView just below it, in its same parent, stretching the whole width, that inflates views.
The problem: Items inside the RecyclerView do not align perfectly to the items outside of it. Knowing that I'm using the same layout for both the parent and the items! And knowing that whatever method I used for horizontal alignment for the header items, is exactly used the same way for each item inflated for the RecyclerView. AND knowing that both items have same properties (they're TextViews, same size, width, height, etc...)
What methods have I tried? FOR BOTH SIDES (up and down), I tried the following:
Using ConstraintLayout for both parent and item layouts, and stretching the items between both start and end of parent, then using the constraint ratio to position it exactly where I want. Outcome for header is different from that of RecyclerView holders.
Using a TableLayout with children stretched across the whole width, same number of columns, same stretching, still different outcome, not pixel perfect.
Placing the header views exactly how I want, but not using any Android placement 'methodology' at all, but instead getX() and setX() later inside the adapter (using .post() so I ensure the position is accurate after inflation) and STILL the same wrong placement.
What am I missing? Shouldn't a RecyclerView inflating children that span the whole width when I specify they should match_parent? I tried debugging the X values for header and view holders, THEY'RE THE SAME but my eyes see different things. I delayed it for some milliseconds but this didn't change a thing.
So the problem turned out that whatever method I used, if the TextViews widths were wrap_content it'll always fit the word inside it. The solution was setting the width to 0dp and let the parent ViewGroup balance everything evenly. In my case I used a TableLayout.
Related
I want to place two layouts containing RecyclerViews side by side, so that they have the same width and fill their parents, i.e. each one should have 50% of its parent's layout width.
Usually you set layout_weight for that, but layout_weight does not work with RecyclerViews on old devices. If layout_weight is set, it stops recycling and just creates all views for all items at once, which is too slow, especially on old devices.
What are the alternatives?
I use Jetpack 1.1.0. Perhaps the issue has been fixed in 1.2.0?
Is there some other layout xml attribute for half-width?
Do not use the xml layout and set layoutparams in code to screen width / 2?
Change the item count, so that the adapter only has the visible items, and somehow new items are added, when the user scrolls down in the view?
I am trying to move my app from using GridView to using RecyclerView with GridLayoutManager. I am new to RecylcerViews, but have successfully converted my ListViews, now working on my GridView. A couple things I am unsure about:
My current GridView has a certain number of columns, determined at runtime, with each grid column the same, hard-coded width. It is scrollable both horizontally and vertically (I wrap my Gridview in a HorizontalScrollView). So I basically need to have a view with a set number of columns that are a set width, irrespective of screen width.
I have been having problems finding a method to set the column(span) width for GridLayoutManager, so I assume that is not how GridLayoutManager. It almost sounds like it is built to always fit all columns on the screen, rather than letting them spill off the screen? What is the best way to tell GridLayoutManager that I want, for example, 6 columns that are each 150 units wide (either dp or pixels)?
For scrolling in both directions, it sounds like I can use my current approach and just wrap my RecyclerView in a HorizontalScrollView, is that correct?
Make the width of the RecyclerView wrap_content and set the number of spans you want. (Make sure parents of the RecyclerView are also wrap_content.) When you create the item views in the RecyclerView's onCreateViewHolder() make sure that it is the width that you want. The RecyclerView will grow to the width of the view holder layout times the number of spans.
All you need to do now is to wrap everything in a HorizontalScrollView.
Is there a way to connect items within a GridView with lines / arrows preferably in the background?
I have a dynamic amount of items (buttons) within a GridView and have to connect certain buttons with others depending on data of a database.
The GridView will most likely be larger than the screen so the view containing the lines/arrows will have to scroll with the GridView simultaneously.
[question]: Android: network relationship graph describes my problem in a more detailed way.
If it were me, I would think to define a layout to be used as the "background" object of your gridview. The layout would be a single relative layout with a custom view that fills parent in both dimensions.
Within this custom view, I would then override the "onDraw" method and all appropriate constructors to perform your arrow drawing on the singular canvas that should be the exact size now of your gridview!
This is where I would think to begin.
Also look here:
Draw background of custom View from .png file on Android
as it shows how to set the backgroundDrawable of a view. If you implement a custom view as drawable, then you should be able to create a new one and send it into your grid view's background drawable.
To make it scroll along with your actual grid data, you may need to play around a bit with the size of the gridview being the full size of the DataSet you are displaying, and containing that within other scrollviews to get this accomplished.
I do have a gridview in an app of mine, that I implemented a horizontal scrollview (as I have buttons at the top allowing me to sort by any of the columns and want those always to be visible, and the gridview to vertically scroll underneath). This creates the overall width of the gridview for me as it simply follows the parent width which wraps the content of the linear layout defining the button set at the top. The gridview then auto scrolls in the vertical direction, leaving the higher level horizontal scroller to perform in the horizontal direction. For you, you may need to have a vertical scroller within a horizontal scroller, and allow your gridview to be full size (i.e. larger than the content view area) and allow the outer scrollers to perform the scrolling for you. This may be the only way to ensure that your background drawable view is the proper size to the table you are trying to display!!!
I have multiple custom views rendered on my screen in RelativeLayout using LayoutParams. Now first time user touches on view it increase it's size, after that when user once again clicks on that view, that view should get disappear. Now the problem is when a view gets disappeared other views on screen changes their size and location. By debugging I found that this happens when I pass dynamic values as width and height in LayoutParams. If I go for static then all works good. But then I'm not able to increase the size of view. How can I solve this? I also tried changing View.GONE to View.INVISIBLE but that also doesn't help.
I create all views in code and place them on screen. In my xml I don not have any view entry. I do this because initial size of views should be random. So I create view and assign him initial size in LayoutParams(width, height) and then use topMargin and leftMargin to place them at various positions on screen. More details can be found here: Custom object click issue in android
Best guess without seeing any code... It sounds like you are using layout_weight, and if a view is removed, the other views with a layout_weight will expand to fill the now available space.
I have a list view which is rendered each time with a different list of items, with variable height.
What I want is to set all the Views in the list view to the same layout height according to the view with the highest layout_height, when setting it to layout_height="wrap_content" for each View.
Also I would like to apply min and max values for the height.
So if I define min=30dp,max=100dp, and the biggest View is automatically rendered with 70dp
all Views in the ListView should be set to 70dp.
I have no idea how to go about it, expect for calculating in the code the max view height values, and setting them to all views, but this doesn't seem very elegant to me, especially as I need to translate it to DPs in the code.
Any simpler ideas?
Your right what you try to achieve wont be as pretty as the regular way, but it'll work ;) usually you would decide height beforehand and then go with it :)