单击coverflow-Android更改视图
我正在使用此CoverFlow: 我希望在单击按钮时能够更改视图,该按钮是一个后退/前进图标,它将引导您进入coverflow中的上一个/下一个项目 我稍微修改了coverflow,以便使用XML布局 下面是我的onCreate()方法:单击coverflow-Android更改视图,android,view,onclick,coverflow,Android,View,Onclick,Coverflow,我正在使用此CoverFlow: 我希望在单击按钮时能够更改视图,该按钮是一个后退/前进图标,它将引导您进入coverflow中的上一个/下一个项目 我稍微修改了coverflow,以便使用XML布局 下面是我的onCreate()方法: setContentView(R.layout.main); CoverFlow CoverFlow=(CoverFlow)findViewById(R.id.CoverFlow); setAdapter(新的ImageAdapter(this)); Ima
setContentView(R.layout.main);
CoverFlow CoverFlow=(CoverFlow)findViewById(R.id.CoverFlow);
setAdapter(新的ImageAdapter(this));
ImageAdapter coverImageAdapter=新的ImageAdapter(此);
coverFlow.setAdapter(coverImageAdapter);
coverFlow.setspace(-20);
coverFlow.setSelection(0,真);
coverFlow.setAnimationDuration(1000);
tv=(TextView)findViewById(R.id.textView1);
coverFlow.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
Toast.makeText(getBaseContext(),String.valueOf(arg2),Toast.LENGTH_SHORT.show();
}
});
coverFlow.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0、视图arg1、内部arg2、长arg3){
tv.setText(String.valueOf(arg2));
}
@凌驾
未选择公共无效(AdapterView arg0){
//做点什么
}
});
我的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>
我希望我能理解你的问题。如果是这样,这里有一个想法: 您需要存储一个私有实例变量,该变量跟踪从coverflow显示的图像的当前位置。然后,从继承自Gallery的
setOnItemSelectedListener()
方法更新它。在“后退”或“前进”按钮上,您只需根据按下的按钮设置当前选择+/-1
因此,在您的活动中,添加一个实例变量int,它将跟踪位置,如
public class MyActivity {
private int currentImagePosition = -1;
//add the rest of your onCreate code here...
}
接下来,您需要能够更新coverflow中当前显示的图像的当前位置。您可以像这样重写setOnItemSelectedListener
(在onCreate方法中):
注:关于更新位置的部分假设从阅读开始起作用,所以我希望这能起作用。如果没有,至少我试过了:D
祝你好运 谢谢David,我是Mike的同事之一,但我从来没有写过一行Java。 我在我们公司的iOS部门工作。 我明白你在说什么。你的回答很有帮助,写得很好 我只想补充一句:
if ( currentimageposition < coverFlow.getcount()-1) {
coverFlow.setSelection(currentImagePosition +1, true);
currentImagePosition = currentImagePosition +1;
}
if(currentimageposition
因为没有调用您的onItemSelected(更新当前标记),我不知道为什么
但是,
coverFlow.selection(int,bool)
未设置动画。加载视图时,它只是一个set方法。例如,coverFlow.selection(3,true)
将在coverFlow的中心设置第四个图像。当我试图了解如何添加动画时,我遇到了这样一个问题,它在一行中完成所有操作:
向左走
coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_左,新的KeyEvent(0,0))代码>
向右转
coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT,newkeyevent(0,0))代码>
(只需将每一行添加到它们各自的按钮onclick方法中
希望这能帮助那些发现自己与我处境相同的人:)如果我的回答不合理,请告诉我。伙计,非常感谢你的回答,我是一名学生,该应用程序是我为我工作的公司开发的应用程序,但本周和下周我都是大学的学生(课程在公司2周/2周在大学等)所以我还没有机会测试你的答案,但看起来很有希望:)哈哈。明白了。对不起,我不是故意赶时间的。这是一个悬赏问题,所以我只是好奇。祝你好运下周有可能给你奖金吗?还是有限制?给我时间先测试一下嘿,迈克,谢谢你的考虑,你真是太酷了:)不过别担心。是的,向上投票就足够了。只有在你尝试过并且它有效并且我已经回答了你所有的问题之后,才接受。因为否则其他人会认为我给出了正确的答案,也许我错过了什么。或者有时候我不会读对已经标记为正确的问题的评论。别担心悬赏积分。让我们首先帮助您调试代码,并确保我的答案是正确的,这对您和其他有关堆栈溢出的人都是如此。这就是堆栈溢出的原因。onKeyDown,如何打印toast。在上面的代码中需要修改什么?一旦我点击任何位置,你能告诉我只有该图像应该在屏幕中心可见,而coverflow中的其余图像应该隐藏。。。。。。。如何做到这一点?然后,当我向左或向右滑动到项目中心时,所有项目都应该可见?@Erum我认为android视图有一种类似于setVisibility
的方法,所以我会使用它来隐藏视图。但我认为你想做的具体事情可能会更复杂一些。也许你应该在这里问一个关于堆栈溢出的新问题,然后更多的人(也许也可以链接我)可以看一看吗?你能不能切记一下这个我想要这种类型的结果@Erum是的,我认为要达到这种效果,你应该做的是,当一个图标被点击时,您应该将所有图标视图的可见性设置为View.INVISIBLE
。当检测到向左和向右滑动时,检查所有图标并使其可见。但是你真的应该在StackOverflow上问一个新问题,这样更多的人可以给你更多的细节,还有一件事我在第0个索引时没能向左滑动为什么??是否可以设置向左滑动?在哪个位置不重要
coverFlow.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
currentImagePosition = position; //this will update your current marker
}
};
@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);
}
}
} ) ;
}
if ( currentimageposition < coverFlow.getcount()-1) {
coverFlow.setSelection(currentImagePosition +1, true);
currentImagePosition = currentImagePosition +1;
}