Skip to content

Instantly share code, notes, and snippets.

@AizazZaidee
Last active December 15, 2017 18:08
Show Gist options
  • Save AizazZaidee/3880ea62131cdd2c631f to your computer and use it in GitHub Desktop.
Save AizazZaidee/3880ea62131cdd2c631f to your computer and use it in GitHub Desktop.
android - listview get item view by position, get listview item on which click is perfrmed, update listview without calling notifyDataSetChanged();
public View getViewByPosition(int pos, XListView listView) {
try {
final int firstListItemPosition = listView
.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition
+ listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition) {
//This may occure using Android Monkey, else will work otherwise
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Inside your public void onClick(final View v) { method call getViewByPosition(position, mAdapterView); (position on which click is performed, you can tag it in hierarchy, (hint)) then make changes to the view returned and then after you are finished call
refreshDrawableState() on that view. that's it :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment