Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling listview with buttons

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.

like image 623
AndroidOptimist Avatar asked Sep 04 '13 12:09

AndroidOptimist


2 Answers

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.

like image 67
Skynet Avatar answered Nov 03 '22 20:11

Skynet


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);
like image 43
meeDamian Avatar answered Nov 03 '22 21:11

meeDamian