【问题标题】:Change view onClick coverflow - Android更改视图 onClick 覆盖流 - Android
【发布时间】:2012-11-06 11:27:01
【问题描述】:

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

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

我稍微修改了封面流,以便改用 XML 布局。

这是我的 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
    }
 });

我的 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>

【问题讨论】:

  • 如果我的回答没有意义,请告诉我
  • 嘿伙计,非常感谢您的回答,我是一名学生,该应用程序是我正在为我正在工作的公司开发的应用程序,但是本周和下周我是一名大学学生(该课程是在公司 2 周/在大学 2 周等)所以我还没有机会测试你的答案,但它看起来很有希望:)
  • 哈哈。明白了。对不起,我不是故意的。这是一个赏金问题,所以我只是好奇。祝你好运!
  • 下周可以给你赏金还是有限制?给我时间先测试一下
  • 嘿,迈克,谢谢你考虑到这一点,你真的很酷 :) 但别担心。是的,一个upvote就足够了。只有在你尝试过并且它有效并且我已经回答了你所有的问题之后才接受。因为否则其他人会认为我给出了正确的答案,也许我错过了一些东西。或者有时我不会将 cmets 读入已经标记为正确的问题。不用担心赏金积分。让我们先帮助您调试您的代码,并确保我的答案是正确的,对于您和堆栈溢出的其他人也是如此。这就是堆栈溢出的用途。

标签: android view onclick coverflow


【解决方案1】:

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

我只需要添加:

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

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

但是,coverFlow.selection( int , bool) 没有动画。它只是加载视图时的 set 方法。例如,coverFlow.selection(3,true) 会将第四张图片设置在封面流的中心。

【讨论】:

    【解决方案2】:

    当我试图找出如何添加动画时,我遇到了这个只需一行就可以完成所有事情的动画:

    向左走

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

    向右走

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

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

    希望这可以帮助那些发现自己处于与我相同情况的人:)

    【讨论】:

    • onKeyDown,我如何打印吐司。上面的代码需要修改什么?
    【解决方案3】:

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

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

    所以在您的 Activity 中,添加一个实例变量 int 来跟踪位置,例如...

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

    接下来,您需要能够更新当前在封面流中显示的图像的当前位置。您可以通过覆盖 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
                }
    };
    

    最后,您只需使用setSelection( currentImagePosition+1 )setSelection( currentImagePosition-1 ) 将每个按钮的onclick 侦听器设置为更改为上一个或下一个图像。顺便问一下,setSelection() 的真假参数是干什么的?我不确定它的作用,所以我将像您最初那样使用true。你的 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);
                            }
            }
        } ) ;
    
    
    }
    

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

    祝你好运!!!

    【讨论】:

    • 你能告诉我,一旦我点击任何位置,那么只有该图像应该在屏幕中心可见,而 Coverflow 中的其余图像应该被隐藏......怎么做这 ?那么当我向左或向右滑动到项目的中心时,所有项目都应该可见?
    • @Erum 我认为 android 视图有办法做类似setVisibility 的事情,所以我会用它来隐藏视图。但我认为你想做的具体事情可能会更复杂一些。也许你应该在这里问一个关于堆栈溢出的新问题,然后更多的人(也许也可以链接我)看看?
    • 你可以请你看看我想要这种类型的结果dailymotion.com/video/x2qx2if
    • @Erum 是的,我认为要达到这个效果,你应该做的是,当一个图标获得onClick 时,你应该将整个图标的视图可见性设置为View.INVISIBLE。并在检测到左右滑动时,浏览所有图标并将它们设为View.VISIBLE。但是您真的应该在 StackOverflow 上将其作为一个新问题提出,以便更多人可以为您提供详细信息
    • 还有一件事我在第 0 个索引处无法向左滑动,为什么?是否可以设置向左滑动,我们在哪个索引处无关紧要@David T。我怎么能做到这一点你可以来这里chat.stackoverflow.com/rooms/78662/fancycoverflow
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多