I would like to implement the UI like the below image.
I layout the UI using GridView and I got only the result like the below image with the same horizontal spacing.
I would like to know that does Android GridView support difference spacing size between grid item? If there is any solution or third party lib, please help. Thank you. Sorry for my poor in english.
Edit:
According to sgadde and Gary Bak's answers, I calculate and add right margin to items in second column in adapter's getView method with the following code.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 20, 0);
viewHolder.numberTextView.setLayoutParams(params);
and I got the following result.
My code to add margin of grid item shrinked the size of grid item. Is something wrong? Please help how to add spacing. Thank you.
You should calculate and add right margin to items in second column, left margin to items in third column dynamically. You can do it in getView() of your adapter.
In your adapter's getView method you have the position, when you hit the position for 2, 6, 10, etc.. then you can add addition spacing on the right.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
My answer will be a little tricky.
First you need to reduce horizontalSpacing to adjust to be square.
Now, you need to add margin for all columns.
There is four columns, and so
column 1: left margin
column 2: right margin
column 3: left margin
column 4: right margin
This will be like what you want.
Related
I want to add custom grid divider to RecyclerView , I have no idea please suggest some idea. For first row divider should start from beginning and for others it should be centered.
For your solution we need to access middle column sequence 1,4,7,10... to get that
if (i % 3 == 1) {
System.out.print(i);
}
from this you'll get position and by that change/ add padding to line
I have a ListView which contains a custom layout.
Each row of the list View look like this:
The row contains 2 LinearLayouts, one for Date and one for Progress details. The progress Details Layout consits of 2 TextViews(Heading and data below it) and 1 Button (View More).
When the user clicks 'View More' button the data below the heading expands to 10-12 lines.
My problem is that when the TextView expands, a scrollbar comes at the edge and the user has to scroll to read. The width of the row does not change i.e the row does not expand.
I want the row width to expand so that the user does not have to scroll to read the text.
I did read a lot and have already tried the following options but they did not work
1. android:scrollbar="none"
2. View.setOverScrollMode(View.OVER_SCROLL_NEVER);
3. View.setScrollContainer(false);
Please help me with this.
You have to use exapanding listview animation for that you can use this lib for that
https://github.com/nhaarman/ListViewAnimations
Use a expandable List View such that the normal view of your list will have the image that you provided and on clicking that view it will expand to reveal the details that you wanted to show by clicking the view more option(in your image)
Here is a tut link for more info.
Try to use LayoutParams and change the height programatically inside the listener for you Button.
LayoutParams should derive from the type of layout containing your LinearLayout. If for instance it is a RelativeLayout, it will look something like that:
int heightForRow = 100;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.height = heightForRow;
yourLinearLayout.setLayoutParams(params);
Please note that your LinearLayout must then be inside another layout (RelativeLayout in the example)
I have 2 column grid inside RecyclerView. I would like to use different margins on items in left column and different margins on items in right column.
Inside method bindViewHolder(VH holder, int position) of class RecyclerView.Adapter, how can I know if current item is on left side or right side?
I tried using position argument however sometimes it starts filling items from right side (image below, look at item 5) so it is no use.
You can get the column index through the itemView of the viewHolder.
StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
int position = lp.getSpanIndex();
I have 2 column grid inside RecyclerView. I would like to use
different margins on items in left column and different margins on
items in right column.
what about using different paddings for your RecyclerView? use different padding value for paddingLeft and paddingRight.
I'm using the TwoWayView library : https://github.com/lucasr/twoway-view to try and implement a horizontal ListView of sorts to overhaul what I had before. (HorizontalScrollView with a horizontally oriented LinearLayout that I add views to)
I want to have a margin before the first item, so there's a blank space at the left when the View is first created. But when scrolled, the blank space would be scrolled and disappear as well. When using a normal ListView it is essentially a HeaderView that I want.
When I implemented the scroll using HorizontalScrollView, I simply programmatically checked the first item and added the margin, which worked since its parent was LinearLayout and accepts margins. But I cannot do this in the getView() of the adapter used for this AdapterView since its LayoutParams do not inherit ViewGroup.MarginLayoutParams
I've also tried setting clipToPadding="false", but the views gets recycled too early, which is unacceptable since the padding I need is noticeably big.
Is there a way to achieve this behavior without moving all the HeaderView code from ListView into the TwoWayView library?
You can try something like this:
on getView(int pos, View convertView, ViewGroup parent){}
{
if (convertView == null)
{ ... }
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT);
/// To set your margin, you just need to test the position :)
params.setMargins(left, top, right, bottom);
viewHolder.LinearLayout.setLayoutParams(params);
}
Hope this would help you.
Is there a way to force GridView to only be a single row? Right now by default it will extend its height to accommodate all views supplied by its adapter
You Should combine a horizontal SCROLLVIEW and a LINEARLAYOUT and a GRIDVIEW to achieve what you want!
put grid view in linearlayout and put the linear layout in the horizontal scroll view;
then when setting adapter! count the number of data!
after that you should calculate the desired width to show all your items!
for example you want to show 8 item! each width is 100dp . so the desired width would be 800dp!
then you should add these lines of code
yourGridView.setNumColumns(8);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(800, ViewGroup.LayoutParams.MATCH_PARENT);
yourGridView.setLayoutParams(lp);
dnt forget to set the width of the linear layout as WRAP_CONTENT in the xml!
*** IMPORTANT NOTE
as i know, by doing this, gridview can't garbage collection because Scroll View doesn't support such a thing and your grid view nested in it!
so dnt use this method for lots of images or you will get HEAP SIZE error!