2011-09-13 92 views
0

我有兩個視圖(廣告),當一個廣告網絡沒有廣告顯示視圖應該翻轉成admob作爲備份。第一個看法是inmobi(不確定這是否有任何相關性)。如果我通過在視圖上投放測試廣告來迫使inmobi廣告失敗,將翻轉並顯示admob廣告,但是當實時廣告開啓時,即使收到失敗的廣告回調,視圖也不會翻轉。請幫忙。Android中的Viewflipper始終無法翻轉

@Override 
public void adRequestFailed(InMobiAdView arg0) { 
    // TODO Auto-generated method stub 
    Log.v("","inmobi ad request failed"); 

    // Initiate a generic request to load it with an ad 
    loadAdmob(); 
    ViewFlipper vf = (ViewFlipper) findViewById(R.id.ads); 

    vf.showNext(); 
} 

private void loadAdmob() { 

    AdView adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxxx"); 

    LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout); 
    // Add the adView to it 
    layout.addView(adView); 
    adView.loadAd(new AdRequest()); 

} 

和XML

<ViewFlipper android:id="@+id/ads" 
    android:layout_width="320dip" 
    android:layout_height="50dip"> 

    <LinearLayout 
      android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 
    <com.inmobi.androidsdk.impl.InMobiAdView 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:id = "@+id/adview" 
    /> 
    </LinearLayout> 


    <LinearLayout 
    android:orientation="vertical" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:layout_gravity="center" 
    android:id="@+id/mainLayout" ></LinearLayout> 

    </ViewFlipper> 

回答

0

我沒有看到這個問題呢。但是如果你需要在佈局之間翻轉,我可以提供我自己的代碼如何使用翻轉功能。首先,您應該在res.layout文件夾中創建未來佈局的單獨頁面,如page_1.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/GreenColor" 
    > 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:text="1" 
     android:textSize="120dip"> 
    </TextView> 
</LinearLayout> 

你做了之後,描述一個main.xml中文件包括外部XML的網頁:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<ViewFlipper android:id="@+id/ViewFlipper01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <include android:id="@+id/libraryView1" layout="@layout/page_1" /> 
    <include android:id="@+id/libraryView2" layout="@layout/page_2" /> 
    <include android:id="@+id/libraryView3" layout="@layout/page_3" /> 
</ViewFlipper> 
</RelativeLayout> 

比類文件,不要忘記申報VF =(ViewFlipper)findViewById( R.id.ViewFlipper01); 你應該下一步覆蓋的onTouchEvent來計算新的座標並顯示翻頁的動畫:

@Override 
    public boolean onTouchEvent(MotionEvent touchevent) { 
     switch (touchevent.getAction()) 
     { 
      case MotionEvent.ACTION_DOWN: 
      { 
       oldTouchValueX = touchevent.getX(); 
       oldTouchValueY = touchevent.getY(); 
       break; 
      } 
      case MotionEvent.ACTION_UP: 
      { 
       //if(this.searchOk==false) return false; 
       float currentX = touchevent.getX(); 
       float currentY = touchevent.getY(); 

       float deltaX = getAbs(oldTouchValueX - currentX); 
       float deltaY = getAbs(oldTouchValueY - currentY); 

       if (deltaX >= deltaY) 
       { 
        if (oldTouchValueX < currentX) 
        { 
         vf.setInAnimation(inFromLeftAnimation()); 
         vf.setOutAnimation(outToRightAnimation()); 
         vf.showNext(); 
        } 
        if (oldTouchValueX > currentX) 
        { 
         vf.setInAnimation(inFromRightAnimation()); 
         vf.setOutAnimation(outToLeftAnimation()); 
         vf.showPrevious(); 
        } 
       } //if deltaX 
       else 
        { 
        if (oldTouchValueY < currentY) 
        { 
         vf.setInAnimation(inFromUpAnimation()); 
         vf.setOutAnimation(outToDownAnimation()); 
         vf.showNext(); 

        } 
        if (oldTouchValueY > currentY) 
        { 
         vf.setInAnimation(inFromDownAnimation()); 
         vf.setOutAnimation(outToUpAnimation()); 
         vf.showPrevious(); 
        } 
       } //Else 
      break; 
      } 
     } 
     return false; 
    } 

,最後一個 - 描述了在的onTouchEvent提到動畫的方法:

public static 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(350); 
    inFromRight.setInterpolator(new AccelerateInterpolator()); 
    return inFromRight; 
} 

public static 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(350); 
    outtoLeft.setInterpolator(new AccelerateInterpolator()); 
    return outtoLeft; 
} 

寫類似其餘的進/出翻轉方向。

+0

感謝您的迅速回復。你的代碼是非常有用的,但不幸的是,我想我沒有描述那個問題,它並沒有回答這個問題。我有一個包含兩個不同廣告網絡瀏覽的佈局的小視圖快速翻轉器,當第一個失敗時,當我調用vf.shownext()時,視圖應該翻轉到第二個佈局。但它永遠不會翻轉。只有當我爲第一個網絡啓用TEST廣告時,該視圖才能正確翻轉到第二個網絡,我不知道爲什麼。 – Jason