问题描述
|
我是android的新手。我对视图翻转器功能有疑问。当我双击第一个视图出现的第三个视图,并且双击第二个视图而不是第三个视图的第一个视图时,我的视图脚蹼中有三个视图。如何停止此操作,以便双击也可以转到下一个视图,任何想法?
这是代码
public class gif extends Activity {
ViewFlipper flipper;
// RelativeLayout r1 = (RelativeLayout)findViewById(R.id.second_view);
private Animation inFromTopAnimation() {
Animation inFromTop = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,0.0f,Animation.RELATIVE_TO_PARENT,-1.0f,0.0f
);
inFromTop.setDuration(1000);
inFromTop.setInterpolator(new AccelerateInterpolator());
return inFromTop;
}
private Animation outToBottomAnimation() {
Animation outtoBottom = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,+1.0f
);
outtoBottom.setDuration(1000);
outtoBottom.setInterpolator(new AccelerateInterpolator());
return outtoBottom;
}
private Animation outToTopAnimation() {
Animation inFromTop = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,+1.0f,0.0f
);
inFromTop.setDuration(1000);
inFromTop.setInterpolator(new AccelerateInterpolator());
return inFromTop;
}
private Animation outFromBottomAnimation() {
Animation outFromBottom = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,-1.0f
);
outFromBottom.setDuration(1000);
outFromBottom.setInterpolator(new AccelerateInterpolator());
return outFromBottom;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper = (ViewFlipper) findViewById(R.id.flipper);
ImageView imgview1 = (ImageView) findViewById(R.id.imageview1);
Button button2 = (Button) findViewById(R.id.flipback);
imgview1.setonClickListener(new View.OnClickListener() {
public void onClick(View view) {
flipper.setInAnimation(inFromTopAnimation());
flipper.setoutAnimation(outToBottomAnimation());
flipper.showNext();
}
});
button2.setonClickListener(new View.OnClickListener() {
public void onClick(View view) {
flipper.setInAnimation(outToTopAnimation());
flipper.setoutAnimation(outFromBottomAnimation());
flipper.showPrevIoUs();
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
ImageView imgview1 = (ImageView)findViewById(R.id.imageview1);
imgview1.setVisibility(ImageView.VISIBLE);
imgview1.setBackgroundResource(R.anim.topimageview);
AnimationDrawable frameAnimation =
(AnimationDrawable) imgview1.getBackground();
if (frameAnimation.isRunning()) {
frameAnimation.stop();
}
else {
frameAnimation.stop();
frameAnimation.start();
}
ImageView imgview2 = (ImageView)findViewById(R.id.imageview2);
imgview2.setVisibility(ImageView.VISIBLE);
imgview2.setBackgroundResource(R.anim.rectbackground);
AnimationDrawable frameAnimation1 =
(AnimationDrawable) imgview2.getBackground();
if (frameAnimation1.isRunning()) {
frameAnimation1.stop();
}
else {
frameAnimation1.stop();
frameAnimation1.start();
}
ImageView imgview3 = (ImageView)findViewById(R.id.imageview3);
imgview3.setVisibility(ImageView.VISIBLE);
imgview3.setBackgroundResource(R.anim.background1);
AnimationDrawable frameAnimation2 =
(AnimationDrawable) imgview3.getBackground();
if (frameAnimation2.isRunning()) {
frameAnimation2.stop();
}
else {
frameAnimation2.stop();
frameAnimation2.start();
}
ImageView imgView = (ImageView)findViewById(R.id.imageview01);
imgView.setVisibility(ImageView.VISIBLE);
imgView.setBackgroundResource(R.anim.background);
AnimationDrawable frameAnimation3 =
(AnimationDrawable) imgView.getBackground();
if (frameAnimation3.isRunning()) {
frameAnimation3.stop();
}
else {
frameAnimation3.stop();
frameAnimation3.start();
}
ImageView imgview02 = (ImageView)findViewById(R.id.imageview02);
imgview02.setVisibility(ImageView.VISIBLE);
imgview02.setBackgroundResource(R.anim.background3);
AnimationDrawable frameAnimation5 =
(AnimationDrawable) imgview02.getBackground();
if (frameAnimation5.isRunning()) {
frameAnimation5.stop();
}
else {
frameAnimation5.stop();
frameAnimation5.start();
}
ImageView imgview03 = (ImageView)findViewById(R.id.imageview03);
imgview03.setVisibility(ImageView.VISIBLE);
imgview03.setBackgroundResource(R.anim.background2);
AnimationDrawable frameAnimation4 =
(AnimationDrawable) imgview03.getBackground();
if (frameAnimation4.isRunning()) {
frameAnimation4.stop();
}
else {
frameAnimation4.stop();
frameAnimation4.start();
}
ImageView imgview04 = (ImageView)findViewById(R.id.imageview4);
imgview04.setVisibility(ImageView.VISIBLE);
imgview04.setBackgroundResource(R.anim.background4);
AnimationDrawable frameAnimation6 =
(AnimationDrawable) imgview04.getBackground();
if (frameAnimation6.isRunning()) {
frameAnimation6.stop();
}
else {
frameAnimation6.stop();
frameAnimation6.start();
}
;
final ImageView hearttap =(ImageView)findViewById(R.id.hearttap);
// final View second = null ;
Button ekgbutton = (Button)findViewById(R.id.ekgbutton);
final ImageView i2 = (ImageView) findViewById(R.id.imageview02);
final ImageView i3 = (ImageView) findViewById(R.id.imageview03);
final ImageView i4 = (ImageView) findViewById(R.id.imageview04);
final ImageView i5 = (ImageView) findViewById(R.id.imageview05);
final ImageView i6 = (ImageView) findViewById(R.id.imageview06);
final ImageView i7 = (ImageView) findViewById(R.id.imageview07);
hearttap.setonTouchListener(new View.OnTouchListener() {
private Animation inFromrightAnimation() {
Animation inFromright = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,0.0f
);
inFromright.setDuration(500);
inFromright.setInterpolator(new AccelerateInterpolator());
return inFromright;
}
private Animation outToLeftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,0.0f
);
outtoLeft.setDuration(500);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
@Override
public boolean onTouch(View v,MotionEvent event) {
// Todo Auto-generated method stub
// r1.setVisibility(View.GONE);
i2.setVisibility(View.GONE);
i3.setVisibility(View.GONE);
i4.setVisibility(View.GONE);
i5.setVisibility(View.GONE);
i6.setVisibility(View.GONE);
i7.setVisibility(View.GONE);
flipper.setInAnimation(inFromrightAnimation());
flipper.setoutAnimation(outToLeftAnimation());
flipper.showNext();
// flipper.getChildAt(2)
flipper.setdisplayedChild(2);
// WebView webview1= (WebView)findViewById(R.id.ekgwebview1);
// webview1.loadUrl(\"C://Users/Android/Desktop/bg only.gif\");
// flipper.cleardisappearingChildren();
// flipper.dispatchWindowVisibilityChanged(2);
flipper.setEnabled(true);
if (hearttap.onTouchEvent(event))
return true;
else
return false;
}
});
ekgbutton.setonTouchListener(new View.OnTouchListener() {
private Animation inFromLeftAnimation() {
Animation inFromLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,0.0f
);
inFromLeft.setDuration(500);
inFromLeft.setInterpolator(new AccelerateInterpolator());
return inFromLeft;
}
private Animation outToRightAnimation() {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,0.0f
);
outtoRight.setDuration(500);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}
@Override
public boolean onTouch(View v,MotionEvent event) {
// Todo Auto-generated method stub
i2.setVisibility(View.GONE);
i3.setVisibility(View.GONE);
i4.setVisibility(View.GONE);
i5.setVisibility(View.GONE);
i6.setVisibility(View.GONE);
i7.setVisibility(View.GONE);
flipper.setInAnimation(inFromLeftAnimation());
flipper.setoutAnimation(outToRightAnimation());
flipper.showPrevIoUs();
flipper.setEnabled(true);
// flipper.getChildAt(0);
flipper.setdisplayedChild(0);
return true;
}
});
}
}
解决方法
问题在于设置显示子级
在按钮button1上单击,您已经设置了
flipper.setDisplayedChild(0);
,这意味着将打开第一个视图。设为flipper.setDisplayedChild(2);
flipper.setDisplayedChild(0) = display first view
flipper.setDisplayedChild(1) = display second view
flipper.setDisplayedChild(2) = display third view
, 您可以创建一个hasmap,将cutton对象存储为键,然后单击counter作为值。如果计数器的模数除法为零,则在每次单击时递增计数器,然后设置为不显示视图。这样,您可以处理双击事件。
谢谢
苏尼尔
, Jamesdroid我也遇到过同样的问题,该问题的解决方案是
1.OnClick功能检查在您的情况下按下了哪个button_tab,您有3个视图,分别是v1,v2和v3,现在还包括3个按钮,如按钮单击上的选项卡,您将显示相应的选项卡。
2.在内部按钮上再次单击,以检查当前视图是处于活动状态还是已显示,然后根据当前视图通过view.ShowNext()转到所需视图。
以下是您推荐的代码,其中我有三个视图(view_beverages,view_dimsum,view_appetizers)和该视图的三个按钮(选项卡)。
ViewFlipper mVf =(ViewFlipper)findViewById(R.id.MyViewFlipper); //我的ViewFilpper显示3个选项卡(1.饮料,2.DimSum,3。开胃菜)
public void onClick(View v){
//饮料标签点击
如果(v == btn_beverages){
btn_beverages.setBackgroundResource(R.drawable.beverages_on);
btn_dimsum.setBackgroundResource(R.drawable.dimsum_off);
btn_appetizers.setBackgroundResource(R.drawable.appetizers_off);
View myCurrentView = mVf.getCurrentView();//Collecting the current displayed view.
if (myCurrentView == view_beverages) {
}
if (myCurrentView == view_dimsum) {
mVf.showNext();
mVf.showNext();
}
if (myCurrentView == view_appetizer) {
mVf.showNext();
}
}
// Dimsum tab click
if (v == btn_dimsum) {
btn_beverages.setBackgroundResource(R.drawable.beverages_off);
btn_dimsum.setBackgroundResource(R.drawable.dimsum_on);
btn_appetizers.setBackgroundResource(R.drawable.appetizers_off);
View myCurrentView = mVf.getCurrentView();
if (myCurrentView == view_beverages) {
mVf.showNext();
}
if (myCurrentView == view_dimsum) {
}
if (myCurrentView == view_appetizer) {
mVf.showNext();
mVf.showNext();
}
}
// Appetizer tab click
if (v == btn_appetizers) {
btn_beverages.setBackgroundResource(R.drawable.beverages_off);
btn_dimsum.setBackgroundResource(R.drawable.dimsum_off);
btn_appetizers.setBackgroundResource(R.drawable.appetizers_on);
View myCurrentView = mVf.getCurrentView();
if (myCurrentView == view_beverages) {
mVf.showNext();
mVf.showNext();
}
if (myCurrentView == view_dimsum) {
mVf.showNext();
}
if (myCurrentView == view_appetizer) {
}
}