Animating ListView items between two states - android

I have a ListView which I want to be able to toggle between two states. In the first state, all items in the list are a square shape besides some text. In the second state, all items in the list are the same except that the square shape is now a circle shape. What I want is to be able to transition the square in all visible items to a circle and vice-versa (using a ViewSwitcher or something like that). Anyone know whether this is possible and how I could go about implementing it? (My ListView's data comes from an ArrayAdapter.)

Turned out to be simpler than first thought. In the getView(int position, View convertView, ViewGroup parent) method of my adapter, I inflate a new View if convertView is null, and following that I find my ViewSwitcher from within my View, check the ViewSwitcher.getDisplayedChild() method, and call ViewSwitcher.showNext() or ViewSwitcher.showNext() as appropriate.

Related

Sometimes Linear layout background color gets changed automatically

I have a main layout. This layout is inflated to the fragment. I have layout named "default_layout_item". The main layout has a header bar, whose background color is blue. Below that there is a listview. Listview item background color is set to white in getview .
Everything works fine The problem what I'm facing is, sometimes header bar color gets changed to white. It's not a part of listview. Then why its happening
Is anyone facing the similar problem? Thanks in Advance..
There are lots of possible issues. Though I would strongly urge you to proof read future questions.. to make it easier on us. Simple thought organization goes a long way.
Following I don't understand most of your question, but I think I might know what you are looking for.. It sounds like you are having issues with the fact Android recycles views in a list view. IE it doesn't actually make a view for every item in a list (if it goes over the screen length) that would be bad. Instead it only makes as many as the screen shows.
To get the correct background you want to set it in your getView in your custom adapter code.
IE
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
//logic for figuring out the color here. Followed by the code to set it.
return rowView;
}
All you need will be in http://www.vogella.com/tutorials/AndroidListView/article.html
I changed the color in this case when I was doing multiple selections on a very custom widget.
If you are trying to set a general color, you should be learning about styles and themes.

How to force vertical scrollbar to show if AutoCompleteTextView results can scroll?

I've got an AutoCompleteTextView in my app and I've been tasked with forcing the vertical scroll bar to always show if the results retrieved by said textview are numerous enough to be scrolled (i.e. there are more results than can fit at once in the autocreated listview).
I've tried adding the following xml attributes to the AutoCompleteTextView itself to no avail:
fadeScrollbars="false"
scrollbarFadeDuration="0"
scrollbarAlwaysDrawVertical="true"
I'm thinking if I could somehow obtain a reference to the listview created automatically for the AutoCompleteTextView and applying one or more of the above attributes to it that I could force the scrollbar to always show but I have no idea how to get a reference to that listview.
Thanks
UPDATE
In the Adapter I created for this AutoCompleteTextView, in the Overridden getView() method, I have a reference to the parent view. I can set those attributes above programatically on the parent and I get the desired functionality, the downside is that those attributes are set each time getView is called which isn't the most efficient?
I have tried styles but they don't work properly, so your coded way seems like the only possibility.
But the group is given on every time, so you could just add a boolean for this:
public View getView(int position, View convertView, ViewGroup parent) {
//Change the list attr programmatically becuase sometimes Android sucks :/
if (!_changedListAttr) {
_changedListAttr = true;
ListView list = (ListView) parent;
list.setBackgroundResource(R.color.color_white);
list.setVerticalScrollBarEnabled(false);
list.setDividerHeight(0);
}

GridView children sometimes not drawing

I have a GridView with a custom view which basically look like buttons. The gridview starts off with no children, and everytime the user presses a button another custom view will be added.
There is some strange behaviour with this. I am drawing some things like text, lines etc on the custom view in onDraw and sometimes they are drawn at all. They are completely blank. The behaviour seems quite random in terms of which views show or don't show the drawn graphics.
I have a feeling it is to do with me setting the layoutparameters. I store the child views in an array once they are created, and in getView() I return the view relevant for the position parameter. So I only ever create buttons for each position once.
So I have two questions.
What am I doing to cause this?
Should I even be using a gridview for what I am doing?
The code for get view is:
public View getView(int position, View convertView, ViewGroup parent)
{
GridButton button;
GridView gridView = (GridView)parent;
if(childBuittons.size() <= position) //if we need to create a new button
{
button = createButton(position);
int nWidth = getButtonSize(gridView);
GridView.LayoutParams params = new GridView.LayoutParams(nWidth, nWidth);
button.setLayoutParams(params);
}
else //we already have a button that we created
{
button = buttons.get(position);
}
return button;
}
Some more information:
- The gridbutton class is just a class that extends View and overrides onDraw to draw some graphics such as text and lines
- What im trying to achieve is a grid of squares that the user can add or remove (although they wont do this often) and then press the squares to perform certain functions
- It is possible that there will be more squares than can fit in the screen
What am I doing to cause this?
Without providing more details related to the GridButton class nobody can really help you. It would be good to know how you handle the measuring of the view and what(and how) exactly do you draw in that view.
I have a feeling it is to do with me setting the layoutparameters.
I doubt this. To be sure you could also draw a simple color in that custom view and see if it appears on the grid. If it does the LayoutParams aren't the reason for what's happening. Also check what values do you get for nWidth.
I store the child views in an array once they are created, and in
getView() I return the view relevant for the position parameter. So I
only ever create buttons for each position once.
This is not ok. The main reason for using a GridView is that you could use its adapter to avoid having to create all grid views(occupying memory(if not crashing) and slowing your app) up front. You should look in implementing the proper recycling mechanism specific to a GridView/ListView. Your getView() method should be like this:
View getView(int position, View convertView, ViewGroup parent) {
GridButton button;
GridView gridView = (GridView)parent;
if(convertView == null) {
button = createButton(position);
int nWidth = getButtonSize(gridView);
GridView.LayoutParams params = new GridView.LayoutParams(nWidth, nWidth);
button.setLayoutParams(params);
} else {
button = (GridButton) convertView;
}
return button;
}
If you need access to those buttons then access them through the adapter, changing some data according to the button's position and calling notifyDataSetChanged().
Should I even be using a gridview for what I am doing?
You didn't say what you want do do. If you think that the number of cells is high enough and it's expected to not see all of them then use a GridView. If you think all the cells will be visible then you could make that grid using the standard layouts without a GridView.

Android list view with static selected position

I've working on app that need custom list view...
List view should act like some kind of vertical gallery.
In gallery selected item is always on center.
In my app I use navigation keys only or remote controller.
So there is no fling or regular scrolling effect.
I need for example second element in my list to be selected always.
If I want to move up, all elements are scrolled one place up, and selected item has to be changed, but on same place on the screen like previous selected item.
It's the same thing like gallery only vertical.
Is there a simple way to do this?
Is there a vertical gallery implementation?
How can I make gallery widget vertical?
Or how to customize list view to act like that?
Tnx!
try setting onClick Listener on ListView
ListView listView1=(ListView) findViewById(R.id.listView1);
then implemt a class class SearchItemClickListener implements OnItemClickListener
and set listview.onItemClickListenr you will get position and view.
Hope this helps
You can get the current visible first postion by using,
mListView.getFirstVisiblePosition();
after getting the current visible position and its respective data, you can perform your desired task (i.e. display the large image after getting the thumbnail or something like this).
OR
If you don't want to depend on only first or last position and want to select some other visible row then you can do something like this,
public View getView(int position, View convertView, ViewGroup parent)
{
int visiblePosition = position % nObjects; // nObjects if the total number of objects to display
if(visiblePosition == yourDesiredPosition){
// do your custom work here.
}
}
Here is a demo for vertical slide show,
Hope this will give you some hint about implementing your functionality.
I've managed this using setSelected() method on ListView item...and managing key listener.
Didn't found smarter way to handle this.

Alternating Listview in Android xml

I woud like to create a listview that alternates background images. For example, the first item would have background image a and the second item would have background image b and the third backgroud a. In basic terms I would like help on creating a listview that for every odd item (egg first, third, fith) has a certian background image different to those listview items which are even (egg second, fourth, and sixth listview item). Here's an exampe.
http://www.gadgetreview.com/wp-content/uploads/2011/10/SIRI-Reminders.jpg
In this example the speech bubbles are background image and each different background image is a different listview item.
In your list adapter in the getView method divide the position attribute that gets sent into the method by 2. If the remaining number is 0 than you are in the even row of your listview. Depending on that you can change the layout of your list view item.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(position % 2 = 0)
//set layout for even row
}else{
//set layout for odd row
}
Last time that I tried I didn't find an xml parameter to do that, but you can try to use the same workaround used in this question:
Stack Overflow: How do I alternate colors in between Listviews?
you have to make your own custom adapter and then in the following method:
public View getView(int position, View convertView, ViewGroup parent) {
}
you can use position to change the background if position is odd
There is SetEmptyView method in list view use it
listView.setEmptyView( findViewById( R.id.empty_list_view ) );

Categories

Resources