首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Change view onClick coverflow - Android

Change view onClick coverflow - Android
EN

Stack Overflow用户
提问于 2012-11-06 19:27:01
回答 3查看 2.2K关注 0票数 7

我正在使用这个CoverFlow:http://www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html

我希望当我单击一个按钮时能够更改视图,该按钮是一个后退/前进图标,它会将您带到coverflow中的上一项/下一项。

我稍微修改了一下coverflow,以便改用XML布局。

下面是我的onCreate()方法:

代码语言:javascript
复制
 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
    }
 });

我的XML:

代码语言:javascript
复制
<?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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-26 19:03:08

当我试图找出如何添加动画时,我遇到了这个,它只用一行代码就能完成所有工作:

向左转

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

往右走

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

(只需将每一行添加到各自的button onclick方法中

希望这篇文章能帮助那些发现自己和我处于同样境地的人:)

票数 3
EN

Stack Overflow用户

发布于 2012-11-21 01:26:11

谢谢David,我是Mike的同事之一,但我从来没有写过一行Java。我在我们公司的iOS部门工作。我明白你在说什么。你的回答很有帮助,而且写得很好!

我只需要补充一句:

代码语言:javascript
复制
if ( currentimageposition < coverFlow.getcount()-1) {
     coverFlow.setSelection(currentImagePosition +1, true);
     currentImagePosition = currentImagePosition +1;
}

因为您的onItemSelected没有被调用(更新当前标记),我不知道为什么。

但是,coverFlow.selection( int , bool)不是动画。它只是一个加载视图时的set方法。例如,coverFlow.selection(3,true)将在coverflow的中心设置第四个图像。

票数 3
EN

Stack Overflow用户

发布于 2012-11-10 06:46:49

我希望我能理解你的问题。如果是这样的话,这里有一个想法:

您需要存储一个私有实例变量,该变量跟踪从coverflow显示的图像的当前位置。然后,从setOnItemSelectedListener()方法更新它,该方法继承自Gallery。在“后退”或“前进”按钮上,您只需根据按下的按钮将当前选择设置为+/- 1。

因此,在您的活动中,添加一个实例变量int,它将跟踪位置,如...

代码语言:javascript
复制
public class MyActivity {
    private int currentImagePosition = -1;

    //add the rest of your onCreate code here...
}

接下来,您需要能够更新当前显示在coverflow中的图像的当前位置。你可以像这样(在你的onCreate方法中)覆盖setOnItemSelectedListener

代码语言:javascript
复制
coverFlow.setOnItemSelectedListener(new OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                currentImagePosition = position; //this will update your current marker
            }
};

最后,您需要做的就是使用setSelection( currentImagePosition+1 )setSelection( currentImagePosition-1 )将每个按钮的onclick侦听器设置为上一个或下一个图像。顺便问一下,setSelection()的true或false参数做什么?我不确定它是做什么的,所以我将像您最初一样使用true。您的onCreate()方法现在将如下所示:

代码语言:javascript
复制
@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);
                        }
        }
    } ) ;


}

注意:关于更新位置的部分假设是从读取 Get the position of the current image displayed in Gallery开始工作的,所以我希望这能起作用。如果没有,至少我试过了:D

祝你好运!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13250030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档