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
Related
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.
Can I somehow extract the number out of a Layout?
I have code similar to this:
linearLayout.addView(randomView, 3);
This means, the element randomView is inserted at position 3 (so it is the 4. element).
I removed another element inside the linearLayout and because of the hardcoded number 3 I ran into an Exception. I want to add my randomView to the end of the layout, so it would be nice if I could do something like this:
linearLayout.addView(randomView, linearLayout.size());
Even if I'm changing the elements of a specific layout, the randomView would then always be at the end of all other elements.
If you don't specify the position view will be added at the end of the ViewGroup.
linearLayout.addView(randomView);
But if you need to specify the position you can get the child count and do this
int childCount = ((ViewGroup)linearLayout).getChildCount();
linearLayout.addView(randomView, childCount);
If you need to access the view inside your linear layout then you can do this
View childView = ((ViewGroup)linearLayout).getChildAt(position);
Why just not do:
linearLayout.addView(randomView);
I remember it adds the view to end of the viewgroup.
I use a checkable listview ( I've set : Mylistview.setChoiceMode(1)).
Inside each row I have 2 ImageViews and 2 TextViews.
I'd like that when my item is checked, the text inside the TextViews scrolls if the text is too long. So in the XML of my row, I've assign the following to both of my TextViews :
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
Unfortunately, this will make the text scroll only if the item is focused and not checked.
So the solution I think about are :
1) Make my own Custom TextView class and make it focus itself when checked (I don't know if it is possible ?)
2) Put the focus to the checked item even if we're in touch mode (use SetFocusableInTouchMode)
I haven't achieved to do the 2nd solution and I don't know which method override if I want to make my own own Custom TextView.
Could you help me to make the text scroll when the item is checked ? Thanks.
You need to do something like following.
int scrollAmount = mTextView.getLayout().getLineTop(mTextView.getLineCount())
-mTextView.getHeight();
// if there is no need to scroll, scrollAmount will be <=0
if(scrollAmount>0)
mTextView.scrollTo(0, scrollAmount);
else
mTextView.scrollTo(0,0);
I have to show list view as like below image
Here I have the view in the xml as like this
<View
android:id="#id/margin"
android:background="#6DAAE9"
android:layout_width="20.0dip"
android:layout_height="fill_parent" />
I Have to change this background (#6DAAE9) dynamically when list is loaded and I have to fix these colors to corresponding item even the list items are increased.
Can any body help me. Thanks in advance
Basiclly you need to edit your getView method.
you can use
convertView.setBackgroundColor(Color.parseColor("#6DAAE9"));
With your appropriate logic, meaning if you want it to be random set random color or hold an array with the order, etc.
See this listview I added list item as per odd or even row and you can do it checking position in getView() method.And you should put imageview for that and set color rather that set color to row.
if(position==0){
//here set color for imageview which position is 0
}else if(position==1){
//here set color for imageview which position is 1
}
....
I am trying to implement the below Android ListView. The each yellow box represents one row of ListView. If you see the rows, each row is over lapped by previous and next row. I have searched Google, but I could not find any proper solutions. If anyone has clues, please let me know.
Thanks in advance.
Think outside the box. Imagine this list not having the rows overlap, but just having diagonal lines on it. In your code, set the Listview's divider height to 0:
ListView listView = (ListView)findViewById(R.id.your_listview);
listView.setDivider(null);
listView.setDividerHeight(0);
Then create two drawables for the rows- one for odd rows, another for even rows:
and
(don't use these two images, as they are not properly/evenly sized, but create the ones for your specific list).
Then create an adapter for your list view - and in its getView method, set the corresponding background to your element:
#override
public void getView(int position, View convertView, ViewGroup parent) {
MyView row;
if(convertView == null) {
row = (MyView)convertView;
}
else {
row = ... //create your view - inflate or create in code
}
row.setBackgroundResource(position%2==1 ? R.drawable.row_odd : R.drawable.row_even);
... //populate the rest of the row with whatever you need
return row;
}
And voila! You get what the effect you need (note, on this schematic the black lines represent the boundaries between rows - these are for schematic purposes only - in your final result you will not have them):
Finally, please note that if you want highlight on your "rows" when an item is selected, you'll have to implement custom state change listeners, which would change the background of the cell that's being selected and also the ones above and below it as to create the proper visual effect.
I got the same issue but I managed to find a solution
In the ListView in the xml you can set a negative dividerHeight, it works for me.
<ListView
...
android:dividerHeight="-5dp" />
I hope it can help someone :)