2011-05-30 74 views
0

我是新手到android。我對視圖鰭狀肢功能有疑問。在我的視圖中,我有三個視圖,當我雙擊第一個視圖出現第三個視圖時,以及當我雙擊第二個視圖而不是第三個視圖時,第一個視圖出現。如何阻止這種情況,雙擊它也會進入下一個視圖,任何想法?Viewflipper雙擊第一個第三個視圖出現

這裏是代碼

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, 0.0f, 
    Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
    ); 
    inFromTop.setDuration(1000); 
    inFromTop.setInterpolator(new AccelerateInterpolator()); 
    return inFromTop; 
    } 
    private Animation outToBottomAnimation() { 
    Animation outtoBottom = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, 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, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
    Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
    ); 
    inFromTop.setDuration(1000); 
    inFromTop.setInterpolator(new AccelerateInterpolator()); 
    return inFromTop; 
    } 
    private Animation outFromBottomAnimation() { 
    Animation outFromBottom = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, 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, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, 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, Animation.RELATIVE_TO_PARENT, -1.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, 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, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, 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, Animation.RELATIVE_TO_PARENT, +1.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, 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; 

     } 

    }); 

    } 
    } 
+1

你能發佈你的代碼嗎? – 2011-05-30 05:28:21

+0

@Deepak嗨,看到我發佈的代碼。 – Jamesdroid 2011-05-30 05:31:30

回答

0

問題是在設置顯示孩子 在點擊按鈕的ekgbutton您已設置flipper.setDisplayedChild(0);這意味着第一個視圖將打開。讓它爲flipper.setDisplayedChild(2);

flipper.setDisplayedChild(0) = display first view 
flipper.setDisplayedChild(1) = display second view 
flipper.setDisplayedChild(2) = display third view 
+0

非常感謝,但這nt的確切解決方案因爲,單擊一切都工作正常,方法r也工作正常,唯一的問題是與雙擊記住:) – Jamesdroid 2011-05-30 05:56:07

+1

嗨,詹姆斯,請看看http:///stackoverflow.com/questions/2217670/question-how-to-implement-android-double-tap – 2011-05-30 06:05:00

+0

@James Long Press是UI準則中推薦的交互方式,雙擊不是。 – 2011-05-30 06:05:52

0

您可以創建您存儲cutton對象鍵並單擊計數器作爲價值hasmap。如果計數器的模數除以零,則在每次點擊時增加計數器,然後設置顯示視圖。這樣你可以處理雙擊事件。

感謝 蘇尼爾

0

Jamesdroid我也出現同樣的問題,該問題的解決方案是, 1.OnClick功能檢查其button_tab按下這是你的情況u有3次隨它去吧v1,v2和v3現在讓我們還包括3個按鈕,如按鈕上的標籤點擊你將顯示相應的標籤。 2. 裏面的按鈕再次點擊檢查當前視圖是活動還是顯示然後根據當前視圖通過view.ShowNext()獲得所需的視圖。

以下是您的推薦代碼,其中有三個視圖(view_beverages,view_dimsum,view_appetizers)和theree按鈕(選項卡)用於此視圖。

ViewFlipper MVF =(ViewFlipper)findViewById(R.id.MyViewFlipper); //我ViewFilpper示出3個標籤(1.Beverages,2.DimSum,3.Appetizers) 公共無效的onClick(視圖v){ //飲料標籤點擊 if(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) { 

     } 

    } 
相關問題