How to overlay rows of Android Listview - android

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 :)

Related

Set empty cell in the middle of android GridView layout

I working on my project with gridview layout, and the whole code is pretty identical to Android Developers example (I can't post links because of my low reputation, sorry).
Its works fantastic, when I need to fill screen with 3*3 or 4*4 (and etc) grids, but now I have to design my gridview with only 8 imageviews and I want to set the empty cell in the middle of GridView, just like that (click for images):
That is my current position:
This is what I need to do:
I have googled that a lot, but I can't find some example that shows how to implement that.
Have you any idea how can I solve that issue?
Set it to invisible -
imageView.setVisibility(View.INVISIBLE);
not works for me, because then I "lost" one of my imageViews.
Thanks a lot!
Let's consider you will always have an (x*x) number of items in your GridView, where x is an odd number bigger than 1, like 3 or 5. The index of the item that you want to hide is items.size()/2. Note that in Android, 9/2 = 4 and 25/2 = 12. So, we already know the index of the item that should be hidden.
Following, in the code, you just have to make your item View stay invisible when that your adapter.getView() method is called for that index. Here is that getView() portion of the code:
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
LayoutInflater inflater = yourContext.getLayoutInflater();
if(convertView==null)
{
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.your_grid_item_layout, null);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
if(position == yourItemsList.size()/2){
convertView.setVisibility(View.INVISIBLE);
}
else{
convertView.setVisibility(View.VISIBLE);
}
return convertView;
}
This will make your middle View invisible. The effect that you wanted.
Also note that your GridView number of lines has to be equal to the number of rows for this method to work.
You can always change the number of rows dynamically, according to the number of items that you have. In your Activity code, before gridView.setAdapter(), like this:
gridView.setNumColumns((int) Math.sqrt(myItemsList.size()));
Again I warn you that this code only works for GridView configurations of 3*3, 5*5, 7*7, and so on.

Android Listview : Center items in layout when fewer items

I have a listview that can have items from 1 to 100, dynamically varying.
However, when I have fewer items, I want the items to appear vertically centered in
the listview layout. By default, Android draws the items form the top. I have attached a picture to illustrate what I need.
Is there any way I can achieve this, either by code or in XML layout ?
Thanks.
Edit: Based on the answers I have received now, I am adding more clarity to my question as the answers are off mark.
The positioning of the Listview in the main layout or positioning of individual item layouts is not an ISSUE here. The listview is positioned properly as shown in the shaded background. The question to which I need answer is:
Assume there are 10 items, the entire shared area of Listview will be used by items,
possibly Items 1 to 5 are shown.
Assume there are 3 items only. The listview is still shown but Items 1,2 and 3 are rendered at the top of the listview (shaded area). However, in this case, I want the items to be drawn at centre of the listview's layout (as shown in the picture).
Anyway this can be done ?
Use this property
mLoginUserNameValueView.setGravity(Gravity.CENTER_HORIZONTAL);
which one you want to center in the List view adapter and which position exactly where you need
e-x you need your 55th position you want center the item means
in getview you will use this line like below.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = null;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.navigationlayout,
null);
mHolder.mLayout = (LinearLayout) convertView
.findViewById(R.id.navigationLayout);
mHolder.mNavigationtext = (TextView) convertView
.findViewById(R.id.navigationText);
convertView.setTag(mHolder);
}
if (\\Your desired position checking )
\\ and set the above line here
}
return convertView;
}

How to make an empty space between Expandable ListView groups [duplicate]

This question already has answers here:
android expandablelistview how to set space between groups items
(6 answers)
Closed 8 years ago.
Can somebody tell me how to make an empty space between the groups of Expandable ListView. I tried many solutions but whenever I give height to divider it always increase the height of child divider too. I tried separately android:divider="#drawable/group_separator" and android:childDivider="#drawable/child_separator" and also tried to set the height programmatically separately in getChildView and in getGroupView but the same result. It always increase the space between child divider too but i just want to make space only between the groups even if there no item between them. Like to give the space of 40dp even if there is no item just like in the below image. Any help will be highly appreciated. Thanks in advance.
and after I applied the divider height it become like
Answering to my own question so that anybody else may happen this problem so here is a solution after trying to much.I have done it through a single condition by just checking that if the group has no child then it should set the white layout that will be shown as a white blank space and no other things like padding, dividers or margin needed. just like in the below code in getGroupView() and you don't have to do anything else. The layout (blank_white_layout) I use is just a relativelayout having white background and you can give it height according to your needs.
if (getChildrenCount(groupPosition) == 0) {
view = inflater.inflate(R.layout.blank_white_layout, null);
}
and below is the complete getGroupView code in my adapter class
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
if (groupPosition == 0 ) {
convertView = inflater.inflate(R.layout.group_layout1, null);
}
if (getChildrenCount(groupPosition) == 0) {
convertView = inflater.inflate(R.layout.blank_white_layout, null);
}else{
convertView = inflater.inflate(R.layout.group_layout2, null);
}
TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
return convertView;
}
From your getView() method detect the rows that you want to separate( by position and type)
and just add to them padding or margin, very simple.
Just make sure to remove that padding or margin when the listview recycles that view.
Create separate xml layout for group and child views. Set padding to the group layout xml file.
Simply add this tag in the group2 xml view.
android:layout_marginTop="40dp"

ListView onItemLongClickListener is not working properly

I am working on a ListView and I have used setBackgroundColor inside onItemLongClickListener on the selected item. My problem is that when I am doing this and scrolling, it is setting color of some invisible child of ListView too. How can it be solved.
Try putting the following attributes in your xml:
`
<ListView
android:dividerHeight="1dp"
android:scrollingCache="false" >
`
this is caused since a listview uses old views in order to avoid re-creation of views when you scroll.
in fact , this is common to all of the adapterView classes .
in order to handle this , store the status of the position of the view (using an arrayList or whatever collection you wish) and on the getView , if the position is set in the list to be of this background , use this background , otherwise use the default background.
for more information about listview , either read the API , or (and i highly recommend it) watch the video "the world of listView" .
In your adapter class:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = inflater.inflate(...);
}
convertView.setBackgroundColor(defaultcolor);
...
}
This however will overwrite the background you set in the onlongclicklistener when that view would be redrawn. So you might want to keep a list of the positions of the clicked items so you can set these in the getView method.

Android HorizontalScrolling laggy

I have a HorzontalScrollView with a LinearLayout inside. During Runtime I can add more LinearLayouts to the LinearLayout.
Now I have the problem that the Scrollview only scrolls a little bit and not smooth with one finger slide!
Does anyone have a solution for this problem?
HorizontalScrollView doesn't use an adapter that manages the list's memory, therefore it can't handle heavy (images, custom views, etc) lists.
You can use this Horizontal ListView http://www.dev-smart.com/archives/34 but make sure you don't write the on list item click method inside the getView, it will make the list scroll slow. Other than that, that's a great resource for a smooth horizontal list view.
You can also explore the Android view pager, which is also supported on lower Android versions using the compatibility pack: http://developer.android.com/sdk/compatibility-library.html
Edit - something like that in the adapter that inflates the XML you want (the linearLayout) and then populates every view with the relevant data.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_friends_list_item, null);
}
ImageView status = (ImageView)convertView.findViewById(R.id.status);
ImageView image = (ImageView)convertView.findViewById(R.id.image);
ImageView imageBorder = (ImageView)convertView.findViewById(R.id.image_border);
TextView title = (TextView)convertView.findViewById(R.id.title);
}
The problem was my parent Viewflow because it has stolen the swipe event! The HorizontalListView is too buggy for me! (Problems with size attributes)
However,thank you for your answer! ;)

Categories

Resources