[英]How to add a click event in ViewFlipper
我正在尝试设计至少10张图像的视角鳍状肢。 现在,如果用户单击图像,则每个图像都有其自己的活动。我如何获得不同图像的不同单击事件。 我的意思是,如果image1在屏幕上,并且用户单击屏幕,则单击toast image1,对于单击了image2 toast image2,依此类推。等等。
public class PaperSelectionActivity extends Activity implements OnClickListener
{
ViewFlipper page;
LinearLayout lyt;
Animation animFlipInForeward;
Animation animFlipOutForeward;
Animation animFlipInBackward;
Animation animFlipOutBackward;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paper_selection);
page = (ViewFlipper)findViewById(R.id.flipper);
animFlipInForeward = AnimationUtils.loadAnimation(this, R.anim.flipin);
animFlipOutForeward = AnimationUtils.loadAnimation(this, R.anim.flipout);
animFlipInBackward = AnimationUtils.loadAnimation(this, R.anim.flipin_reverse);
animFlipOutBackward = AnimationUtils.loadAnimation(this, R.anim.flipout_reverse);
}
private void SwipeRight(){
page.setInAnimation(animFlipInBackward);
page.setOutAnimation(animFlipOutBackward);
page.showPrevious();
}
private void SwipeLeft(){
page.setInAnimation(animFlipInForeward);
page.setOutAnimation(animFlipOutForeward);
page.showNext();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return gestureDetector.onTouchEvent(event);
}
SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener(){
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
float sensitvity = 50;
if((e1.getX() - e2.getX()) > sensitvity){
SwipeLeft();
}else if((e2.getX() - e1.getX()) > sensitvity){
SwipeRight();
}
return true;
}
};
GestureDetector gestureDetector = new GestureDetector(simpleOnGestureListener);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.paper_selection, menu);
return true;
}
}
XML档案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical">
<ViewFlipper
android:id="@+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/image1"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/image2"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/image3"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/image4"/>
</RelativeLayout>
</ViewFlipper>
</LinearLayout>
冒着鼓励过早优化的风险,我解决了一个与此非常相似的问题(翻转一组可能包含数十个或多或少相同的页面),结果很好,尽管可以间接地解决您的问题。
我的技术基础是让ViewFlipper包含一个子页面。 当用户单击“上一个”或“下一个”时,将发生以下情况:
我需要加倍努力,因为我的页面非常复杂...几个列表,各种按钮,图像等。如果在我的视图层次结构中只有2个或3个,那将是非常浪费的,更不用说几十个了。 上面的技术可以最大程度地降低布局的复杂度,并且如果页面很复杂,则翻转动画的运行速度也要快得多。 您可以在那里找到更多细节
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.