I want to scroll my listview by pressing the buttons. Am using two buttons up and down and when i press up button the listview should move one row up and when i press down button the listview should move one row down.
My listview is based on this link. I found a good example of scrollview here. Now my question is instead of scrollview i need to use my listview and it should increase/decrease by one row. As am new to android anyone help me in solving this issue. Thanks in advance.
You can try either of the following:
For a direct scroll:
getListView().setSelection(int);
For a smooth scroll:
getListView().smoothScrollToPosition(int);
Sample Code:
public View.OnClickListener onChk = new View.OnClickListener() {
public void onClick(View v) {
int index = list.getFirstVisiblePosition();
getListView().smoothScrollToPosition(index+1); // For increment.
}
});
However you need to handle one case, that is if only half or part of the view at the top is visible.
Have you even checked for IDE suggestions? ;)
ListView lv = (ListView) findViewById(R.id.myListView);
// to scroll to a given position
lv.scrollTo(int x, int y);
// to scroll by a given number of dp
lv.scrollBy(int x, int y);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With