我正在使用这个CoverFlow: http://www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html
我希望能够在单击按钮时更改View,该按钮是一个后退/前进图标,可以将您带到封面流中的上一个/下一个项目。
我稍微修改了封面流程,以便我使用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>
答案 0 :(得分:3)
感谢大卫,我是迈克同事之一,但我从来没有写过一行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)
会将第四张图像设置在封面流的中心。
答案 1 :(得分:3)
当我试图找出如何添加动画时,我遇到了这个,它只用一行来完成所有事情:
左转
coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, new KeyEvent(0, 0));
走吧
coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, new KeyEvent(0, 0));
(只需将每一行添加到各自的按钮onclick方法
希望这可以帮助那些在与我相同的情况下发现自己的人:)
答案 2 :(得分:2)
我希望我理解你的问题。如果是这样,这是一个想法:
您需要存储一个私有实例变量,该变量跟踪从封面流中显示的图像的当前位置。然后,您从setOnItemSelectedListener()
方法更新它,该方法继承自Gallery。在“后退”或“前进”按钮上,您只需根据按下的按钮设置当前选择+/- 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
}
};
最后,您只需设置每个按钮的onclick侦听器,即可使用setSelection( currentImagePosition+1 )
或setSelection( currentImagePosition-1 )
更改为上一张或下一张图片。顺便问一下,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
祝你好运!!!