GridView animation - android

I want add animation to every single grid view item. I did something like this in method GetView():
convertView.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.animation));
And animation works, but when I am adding new elements to my grid view, it refreshes whole grid view and all items animates again. My purpose is to invoe animation only when item is created and when I scroll. How to achieve that aim?

I think you have to set up a map of Boolean in your adapter.
For each adapter pos, check if the position has already been animated.
Something like that :
if(map.get(position) == null || !map.get(position)){
map.put(position, true);
convertView.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.animation));
}

Related

update adapter time gridview scroll automatically on top

I am integrating endless grid view. end less is working perfect. but on scroll while update my adapter data at that time grid view goes to top. and i need that it should be maintain it's current position where it is.
For the feel adapter in grid view i used below code.
myDataAdapter = new ProductGridAdapter(activity, results, null);
gridView.setAdapter(myDataAdapter);
gridView.invalidate();
myDataAdapter.notifyDataSetChanged();
Can any one tell me that how can i resolve this issue?
Have a variable that captures the top element of your grid view that is being displayed. Then whenever you invalidate the view, use setSelection() function. http://developer.android.com/reference/android/widget/ListView.html#setSelection%28int%29
I got the way. in first time i add adapter in list view and after that i just update the adapter.
if (myDataAdapter == null)
{
myDataAdapter = new ProductGridAdapter(activity, results, null);
gridView.setAdapter(myDataAdapter);
}
else
{
myDataAdapter.updateData(results); //update adapter's data
myDataAdapter.notifyDataSetChanged();
}

ListView wrong selection on ItemLongClistener

I have a method that paint the View of selected item but if my ListView has a scroll ( i mean: if my screen can display 9 "lines" - positions - and the total size is bigger than 9 ) and i select one the first nine positions, it will select the position that i choose and one from the last positions ( a position that i need to scroll to see ).
Example: if i select select the position 0 and paint it, the view on position 0 and on position 11 will be painted.
If i try do to:
getListView().getChildAt(int)
And if this position is like '12', it will return null so i'm using the View that i get when onItemLongClick(AdapterView adapterView, View v, int position , long arg3) is called. It looks like that View is based on scroll because i have same View objects for scroll positions.
Here is how i paint the Views:
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View v,
int position , long arg3) {
Log.d("", "");
View tmp = adapterView.getChildAt(position);
int oi = adapterView.getSelectedItemPosition();
if(!mPositions.contains(Integer.valueOf(position))){
v.setBackgroundColor(Color.CYAN);
mViews.add(v);
mPositions.add(Integer.valueOf(position));
} else {
v.setBackgroundColor(Color.TRANSPARENT);
mViews.remove(v);
mPositions.remove(Integer.valueOf(position));
}
return true;
}
});
How can i get distinct views independently if the list is bigger than my screen?
The problem here is that you really shouldn't be modifying the view directly. The whole point of a ListView is that it will create a view for an item, and then potentially use this same view to paint each and every one of the items in the list. (row recycling). It is possible here, that when you modify the background colour of the selected item's view, this view object is then actually being used to draw another row's items - looking like it's just doing it for the wrong row.
What you should do, is create a custom adapter with your own implementation of getView(). Inside this function you will do one of two things: If convertView is null, then create the View that you desire (possibly a TextView?), otherwise you will grab the view from convertView. Finally you will then set this view up however you would like based on it's position in the list. Ie: if it's text, set the value, and then set your background.
To respond to the longpress, simply change your data (mSeleted) and call notifyDataSetChanged() so that the list will redraw based on this new data.
This is a pretty simplistic overview, listviews can get as complicated as you want. There are many examples of how to create a custom listview on the web if you search.

How to set items in adapter onclicklistener temporary to null? And later set it back?

I have a listview with items from the adapter. And when I try and retrieve new information, I'm putting a RelativeLayout with loading images and stuff on top of it.
The problem is, is that my listview and adapter items are still clickable. They can only be clickable after the loading is done. When the loading is done I am triggering (notification) a setVisibility(View.gone) on that LoadingLayout.
I tried:
listview.setFocusable(false);
listview.setClickable(false);
listview.setEnabled(false);
But it is not working
I guess the:
row.setOnClickListener(this);
in the getview method of the adapter is still on?
How do I make it not clickable? Do I get a list of views or something and make them unclickable, but then again. Only the views that are onscreen should be not clickable for that moment? Or is there some other clever method? (I thought the overlaying relativelayout would be clever ... guess not)
Declare a variable in class where you are calling setAdapter to list view as follows
OnItemClickListener myAdapterItemClickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
// TODO your row onclick actions
}
};
Before loading all the stuff, you can set
listview.setOnItemClickListener(null);
After loading it,
listview.setOnItemClickListener(myAdapterItemClickListener);

How to update listview as user scroll down?

I want to make a listview that as user scroll to bottom of list view other items of list fill automatically from internet.I wrote code to that in the adapter of that expandable list view (within getGroupView() method)as like this,
public View getGroupView(final int arg0, final boolean arg1, View arg2, ViewGroup arg3) {
//if(arg2==null){
//arg2=act.getLayoutInflater().inflate(R.layout.layout_exlistview_group, null);
}
//((TextView)arg2.findViewById(R.id.nameText)).setText(item.getItemName());
if(arg0>=getGroupCount()-1){//chech is near for end
/*here i run a asynctask to download data and add items to SparseArray of this class.That sparsearray is the source to the adapter to view them in listview*/
}
//return arg2;
}
So is this correct way to do this or is there any good way to do that?
From the fact that you're using getGroupView I assume you're using an ExpandableListView, not a regular ListView, which should probably be stated in your question.
Either way, the best way to do this would be to assign a OnScrollListener onto your list and then do your check there, rather than in getGroupView.
I'd recommend you put something along the lines of the following into your onScroll method:
if (firstVisibleItem + visibleItemCount > (totalItemCount - NUM_BEFORE_LOAD)) {
loadMore()
}
where NUM_BEFORE_LOAD based on your example would be 1, but you could make it anything you want to make the list load quicker than when it hits the bottom.

Synchronize two ListView positions when you scroll from both list views Android

I have two ListViews. Is there any way to synchronize the position of ListViews when I scroll both the Lists
Use the following method to get the scroll position of your first listView-
private void saveListScrollPosition()
{
// save index and top position
index = _listview1.getFirstVisiblePosition();
View view = _listview1.getChildAt(0);
top = (view == null) ? 0 : view.getTop();
}
And scroll the second listView to that position with-
// restore
_listview2.setSelectionFromTop(index, top);
You could use this in your second list view: smoothScrollToPosition(position)
And in your first ListView you could use a OnScrollListener and check the first visible item with getFirstVisiblePosition.
Best wishes,
Tim

Categories

Resources