Change view onClick coverflow - Android

I use this CoverFlow: http://www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html

I want to be able to change the view when I click the button, the button is the back / forward icon, which will lead you to the previous / next item in the cover.

I slightly modified the cover to use the XML layout instead.

Here is my onCreate () method:

setContentView(R.layout.main); CoverFlow coverFlow = (CoverFlow)findViewById(R.id.coverflow); coverFlow.setAdapter(new ImageAdapter(this)); ImageAdapter coverImageAdapter = new ImageAdapter(this); coverFlow.setAdapter(coverImageAdapter); coverFlow.setSpacing(-20); coverFlow.setSelection(0, true); coverFlow.setAnimationDuration(1000); tv = (TextView)findViewById(R.id.textView1); coverFlow.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(getBaseContext(), String.valueOf(arg2), Toast.LENGTH_SHORT).show(); } }); coverFlow.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { tv.setText(String.valueOf(arg2)); } @Override public void onNothingSelected(AdapterView<?> arg0) { // Do something } }); 

My XML:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/home_background" android:orientation="vertical" > <com.demo.app.coverflow.CoverFlow android:id="@+id/coverflow" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginBottom="39dp" android:layout_marginLeft="35dp" android:background="@drawable/button_left" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="39dp" android:layout_marginRight="35dp" android:background="@drawable/button_right" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="55dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="hello" android:textColor="@color/White" android:textSize="16dp" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_marginRight="170dp" android:layout_marginTop="15dp" android:src="@drawable/unsa_logo" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="14dp" android:layout_marginTop="23dp" android:src="@drawable/pnc_logo" /> </RelativeLayout> 
+7
source share
3 answers

While I was trying to learn how to add animation, I came across this, which does everything in only one line:

go left

coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, new KeyEvent(0, 0));

go right

coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, new KeyEvent(0, 0));

(just add each line to the corresponding onclick methods button

Hope this helps anyone who finds them in the same situation as me :)

+3
source

Thanks, David, I'm one of Mike's colleagues, but I never wrote Java lines. I work in the iOS department of our company. I understand what you were talking about. Your answer was helpful and well written!

I just had to add:

 if ( currentimageposition < coverFlow.getcount()-1) { coverFlow.setSelection(currentImagePosition +1, true); currentImagePosition = currentImagePosition +1; } 

Since your onItemSelected was not called (update current token), I don't know why.

However, coverFlow.selection( int , bool) not animated. This is just a given method when loading a view. For example, coverFlow.selection(3,true) set the fourth image in the center of the coverage.

+3
source

I hope I understand your question. if yes, then here is the idea:

you need to keep a private instance variable that keeps track of the current position of the image displayed from the cover stream. then you update it using 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 you pressed.

so in your Activity add an int instance variable that will track the position as ...

 public class MyActivity { private int currentImagePosition = -1; //add the rest of your onCreate code here... } 

Then you will need to update the current position of the image, which is currently displayed in the cover stream. you can do this by overriding setOnItemSelectedListener like this (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 } }; 

Finally, all you have to do is set each onclick listeners button to go to the previous or next image using setSelection( currentImagePosition+1 ) or setSelection( currentImagePosition-1 ) . By the way, what does the true or false setSelection() parameter setSelection() ? I'm not sure what it does, so I just use true , as you did initially. your onCreate() method will now look something like this:

 @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. It is assumed that the part on the update position works from reading Get the position of the current image displayed in the Gallery , so I hope this works. if not, at least I tried: D

Good luck !!!

+2
source

All Articles