7

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

I want to be able to change View when I click on a button, the button being a back/forward icon which will take you to the previous/next item in the coverflow.

I have modded the coverflow slightly so that I use an 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>
Mike Bryant
  • 2,455
  • 9
  • 39
  • 60
  • let me know if my answer didn't make sense – David T. Nov 16 '12 at 16:42
  • 1
    hey man, thanks a lot for your answer, I'm a student, the application is a app that I'm developing for the company that I'm working at, however this week and next week I'm an uni (the course is 2 weeks at the company / 2 weeks at uni etc..) so I haven't had a chance to test your answer yet but it looks very promising :) – Mike Bryant Nov 16 '12 at 18:36
  • hahah. gotcha. sorry, i didn't mean to rush. it was a bounty question, so i was just curious. good luck!! – David T. Nov 16 '12 at 18:45
  • is it possible to give you the bounty next week or is there a limit? to give me the time to test it first – Mike Bryant Nov 16 '12 at 18:58
  • hey Mike, thanks for considering that, that's really cool of you :) but don't worry. yeah, an upvote is good enough. only accept after you've tried and it works and i've answered ALL your questions. because otherwise other ppl will think that i gave the right answer, when maybe there's something i missed. or sometimes i won't read comments to a question that's already marked as correct. don't worry about the bounty points. let's help you debug your code first and make sure my answer is correct, for you and also others on Stack overflow. that's what stack overflow is for. – David T. Nov 17 '12 at 17:59

3 Answers3

3

Thanks David, I'm one of the Mike colleagues but I've never written a line of Java. I work in the iOS department of our company. I understood 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;
}

Because your onItemSelected was not called (update the current marker), I don't know why.

However, coverFlow.selection( int , bool) is not animated. It is just a set method when the view is loaded. For example, coverFlow.selection(3,true) will set the fourth image on the center of the coverflow.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
Defoncesko
  • 657
  • 5
  • 25
3

While I was trying to find out how to add the animation I came across this which does everything in just 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 their respective button onclick methods

Hope this can help anyone else who finds them self in the same situation as I was :)

Mike Bryant
  • 2,455
  • 9
  • 39
  • 60
2

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!!!

Community
  • 1
  • 1
David T.
  • 22,301
  • 23
  • 71
  • 123
  • T can u tell me once i click on any position then only that image should be visible in center of screen while rest of the images in coverflow should be hidden ....... how to do this ? then when i swipe left or right to the center of item then all items should be visible ? – Erum May 21 '15 at 08:30
  • @Erum I think android views have a way to do something like `setVisibility`, so i would use that to hide views. But i think the specific thing you wanted to do might be a little more complicated. maybe you should ask a new question here on stack overflow, and then more people (maybe link me as well) can take a look? – David T. May 21 '15 at 08:50
  • T can u pls cehck this i want this type of result http://www.dailymotion.com/video/x2qx2if – Erum May 21 '15 at 09:00
  • @Erum Yeah, i think to achieve that effect, what you should do is that, when an icon gets the `onClick`, you should set all over icon's views' visibility to `View.INVISIBLE`. and upon the swiping left and right detected, go through all the icons and make them `View.VISIBLE`. but you should really ask that as a new question here on StackOverflow so more people can give you details – David T. May 26 '15 at 02:18
  • one thing more i m failed to swipe left once i m at 0th index why ?? is it posible to set swipe left it does not matter at which index we are @David T. how can i achieve that can u come here http://chat.stackoverflow.com/rooms/78662/fancycoverflow – Erum May 26 '15 at 04:34