how can move scroll bar automatically?
I am using grid view for display images, I want to scroll开发者_StackOverflow move automatically, whenever I add another images in gridview, is it possible?
I think you have to read a little bit more :
https://developer.android.com/reference/android/widget/AdapterView.html#setSelection(int)
ListView.setSelection(position);
https://developer.android.com/reference/android/widget/AbsListView.html#smoothScrollToPosition(int)
ListView.smoothScrollToPosition(position).
Either call setSelection()
or use smoothScrollToPosition()
.
I got result using GridView.setSelection(GridView.getCount())
,so scroll bar automatically move to last element of Grid View
Use TimerTask
to do that since Adapter.notifyDataSetChanged()
takes delay to update the grid or list.
Code is below:
new Timer().schedule(new TimerTask()
{
@Override
public void run() {
nameHandler.sendEmptyMessage(0);
}
},500);
//// And in handler::
Handler nameHandler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
gridView.setSelection(selectorIndex);
// OR
gridView.smoothScrollToPosition(selectorIndex);
gridView.invalidate();
}
} ;
精彩评论