简体   繁体   English

更改视图onClick coverflow - Android

[英]Change view onClick coverflow - Android

I'm using this CoverFlow : http://www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html 我正在使用这个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. 我希望能够在单击按钮时更改View,该按钮是后退/前进图标,可以将您带到封面流中的上一个/下一个项目。

I have modded the coverflow slightly so that I use an XML layout instead. 我稍微修改了封面流程,以便我使用XML布局。

Here is my onCreate() method: 这是我的onCreate()方法:

 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:

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

Thanks David, I'm one of the Mike colleagues but I've never written a line of Java. 谢谢大卫,我是迈克同事之一,但我从来没有写过一行Java。 I work in the iOS department of our company. 我在公司的iOS部门工作。 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. 因为未调用onItemSelected(更新当前标记),我不知道为什么。

However, coverFlow.selection( int , bool) is not animated. 但是, coverFlow.selection( int , bool)不是动画。 It is just a set method when the view is loaded. 它只是加载视图时的set方法。 For example, coverFlow.selection(3,true) will set the fourth image on the center of the coverflow. 例如, coverFlow.selection(3,true)将在Coverflow的中心设置第四个图像。

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 (只需将每一行添加到各自的按钮onclick方法

Hope this can help anyone else who finds them self in the same situation as I was :) 希望这可以帮助其他人在与我相同的情况下找到他们自己:)

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. 然后,从setOnItemSelectedListener()方法更新它,该方法继承自Gallery。 On the "back" or "forward" button, you simply set your current selection +/- 1 depending on the button pressed. 在“后退”或“前进”按钮上,您只需根据按下的按钮设置当前选择+/- 1。

so in your Activity, add an instance variable int that will keep track of position like... 所以在你的Activity中,添加一个实例变量int,它将跟踪位置,如...

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): 你可以通过重写setOnItemSelectedListener来实现这一点(在你的onCreate方法中):

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 ) . 最后,您所要做的就是通过使用setSelection( currentImagePosition+1 )setSelection( currentImagePosition-1 )将每个按钮的onclick侦听器设置为更改为上一个或下一个图像。 By the way, what's the true or false parameter of setSelection() do? 顺便说一句, setSelection()的真或假参数是什么? i'm not sure what it does so i'm just going to use true like you did initially. 我不确定它是做什么所以我只会像你最初那样使用true your onCreate() method will now look something like: 你的onCreate()方法现在看起来像:

@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. 注意:关于更新位置的部分假定从读取工作获取Gallery中显示的当前图像的位置,所以我希望这是有效的。 if not, at least i tried :D 如果没有,至少我试过:D

Good luck!!! 祝好运!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM