I hope i understand your question. if so, here's an idea:
you need to store a private instance variable that tracks the current position of the image that is being displayed from the coverflow. then, you update it from the setOnItemSelectedListener()
method, which is inherited from Gallery. On the "back" or "forward" button, you simply set your current selection +/- 1 depending on the button pressed.
so in your Activity, add an instance variable int that will keep track of position like...
public class MyActivity {
private int currentImagePosition = -1;
//add the rest of your onCreate code here...
}
Next, you need to be able to update the current position of the image that is currently being displayed in the coverflow. you can do so by overriding setOnItemSelectedListener
like so (inside your onCreate method):
coverFlow.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
currentImagePosition = position; //this will update your current marker
}
};
Lastly, all you have to do is set each button's onclick listeners to change to the previous or next image by using setSelection( currentImagePosition+1 )
or setSelection( currentImagePosition-1 )
. By the way, what's the true or false parameter of setSelection()
do? i'm not sure what it does so i'm just going to use true
like you did initially. your onCreate()
method will now look something like:
@Override
public void onCreate(Bundle savedInstanceState){
//insert your other code in onCreate here in this spot....
//add the following lines to your code:
Button goLeft = (Button) findViewById(R.id.button1);
goLeft.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
//go to previous image
if( currentImagePosition > 0 ){ //i'm assuming you dont want to go left anymore once at the leftmost image
coverFlow.setSelection(currentImagePosition - 1, true);
}
}
} ) ;
Button goRight = (Button) findViewById(R.id.button2);
goRight.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
//go to previous image
if( currentImagePosition < coverFlow.getCount()-1 ){ //i'm assuming you dont want to go left anymore once at the leftmost image
coverFlow.setSelection(currentImagePosition + 1, true);
}
}
} ) ;
}
NOTE: the part about updating position is assumed to work from reading Get the position of the current image displayed in Gallery so i hope this works. if not, at least i tried :D
Good luck!!!