2012-01-30 20 views
2

我使用jason fry的SwipeView,雖然他使用它進行圖像瀏覽,但我努力用佈局替換它。使用SwipeView作爲佈局而不是圖像

的,如果我有一個TextView更換ImageView的,但我怎麼會用一個佈局 更換ImageView的任何幫助表示讚賞感謝

包com。示例它運作的瞬間;

import android.graphics.Typeface; 
import android.text.Layout; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import com.example.R; 
import uk.co.jasonfry.android.tools.ui.PageControl; 
import uk.co.jasonfry.android.tools.ui.SwipeView; 
import uk.co.jasonfry.android.tools.ui.SwipeView.OnPageChangedListener; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.FrameLayout; 
import android.widget.ImageView; 

public class MyActivity extends Activity 
{ 
SwipeView mSwipeView; 

LinearLayout ll; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ll = new LinearLayout(this); 
    ll = loadLayout(); 

    PageControl mPageControl = (PageControl) findViewById(R.id.page_control); 
    mSwipeView = (SwipeView) findViewById(R.id.swipe_view); 

    //loadImages(); 

    for(int i=0; i<4;i++) 
    { 
     mSwipeView.addView(new FrameLayout(this)); 
    } 

    TextView i0 = new TextView(this); 
    TextView i1 = new TextView(this); 
    i0.setText("page 1"); 
    i1.setText("page 2"); 

    ((FrameLayout) mSwipeView.getChildContainer().getChildAt(0)).addView(ll); 
    ((FrameLayout) mSwipeView.getChildContainer().getChildAt(1)).addView(ll); 

    SwipeImageLoader mSwipeImageLoader = new SwipeImageLoader(); 

    mSwipeView.setOnPageChangedListener(mSwipeImageLoader); 
    mSwipeView.setPageControl(mPageControl); 
} 

private class SwipeImageLoader implements OnPageChangedListener 
{ 

    public void onPageChanged(int oldPage, int newPage) 
    { 
     if(newPage>oldPage)//going forwards 
     { 
      if(newPage != (mSwipeView.getPageCount()-1))//if at the end, don't load one page after the end 
      { 
       TextView v = new TextView(MyActivity.this); 
       v.setText("page :"+(newPage+1)); 
       ((FrameLayout) mSwipeView.getChildContainer().getChildAt(newPage+1)).addView(ll); 
      } 
      if(oldPage!=0)//if at the beginning, don't destroy one before the beginning 
      { 
       ((FrameLayout) mSwipeView.getChildContainer().getChildAt(oldPage-1)).removeAllViews(); 
      } 

     } 
     else //going backwards 
     { 
      if(newPage!=0)//if at the beginning, don't load one before the beginning 
      { 

       TextView v = new TextView(MyActivity.this); 
       v.setText("page :"+(newPage+1)); 
       ((FrameLayout) mSwipeView.getChildContainer().getChildAt(newPage-1)).addView(ll); 
      } 
      if(oldPage != (mSwipeView.getPageCount()-1))//if at the end, don't destroy one page after the end 
      { 
       ((FrameLayout) mSwipeView.getChildContainer().getChildAt(oldPage+1)).removeAllViews(); 
      } 
     } 

    } 

} 

private LinearLayout loadLayout() 
{ 
    //logo 
    ImageView logo = new ImageView(this); 
    logo.setImageResource(R.drawable.image001); 
    logo.setLayoutParams(new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

    // espace 
    TextView espace = new TextView(this); 
    espace.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
    espace.setText(" "); 

    // wrap prest 
    LinearLayout wprest = new LinearLayout(this); 

    //Prestation 
    TextView txt_pres = new TextView(this); 
    txt_pres.setText(" Prestation n° "); 
    txt_pres.setTextColor(R.color.black); 
    // plaid 
    TextView plaid = new TextView(this); 
    plaid.setTextColor(R.color.black); 
    plaid.setTypeface(null, Typeface.BOLD); 
    plaid.setText("4558"); 
    // - 
    TextView tiret = new TextView(this); 
    tiret.setTextColor(R.color.black); 
    tiret.setTypeface(null, Typeface.BOLD); 
    tiret.setText(" - "); 
    // plaid 
    TextView platyp = new TextView(this); 
    platyp.setTextColor(R.color.black); 
    platyp.setTypeface(null, Typeface.BOLD); 
    platyp.setText("ECHANGE"); 

    wprest.addView(txt_pres); 
    wprest.addView(plaid); 
    wprest.addView(tiret); 
    wprest.addView(platyp); 



    LinearLayout ll = new LinearLayout(this); 

    ll.setBackgroundResource(R.color.white); 
    ll.setOrientation(LinearLayout.VERTICAL); 
    ll.setLayoutParams(new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,  LinearLayout.LayoutParams.FILL_PARENT)); 
    ll.addView(logo); 
    ll.addView(espace); 
    ll.addView(wprest); 

    return ll; 
} 
} 

回答

2

你爲什麼不嘗試另一種ViewPager庫,如:

  • Android的viewflow(https://github.com/pakerfeldt/android-viewflow)
  • Android的ViewPagerIndicator (https://github.com/JakeWharton/Android-ViewPagerIndicator)

因爲使用它,您可以輕鬆地自定義兒童佈局。

我看到它是最流行的ViewPager庫。有關更多詳細信息,請查看Market上的「Android UI Patterns」應用程序:https://market.android.com/details?id=com.groidify.uipatterns。開發人員有許多有用的樣本。

+0

謝謝我只能找到jason fry的例子。順便提一下 – 2012-01-30 11:42:57

+0

我使用的是IntelliJ IDEA,我發現不可能實現ViewFlow – 2012-01-30 16:02:11

+0

SwipeView的存在是因爲在我最初創建它時沒有做任何事情。我從來沒有把SwipeView的優點和缺點與android-viewflow,greendroid的優缺點進行比較,也沒有比較兼容包中的官方android版本,所以我不能說哪一個最好:) – jsonfry 2012-02-10 17:46:37

0

我使用這個庫遍佈整個地方不僅僅是圖像。我用它與複雜的佈局,所以它的工作。

我已經添加了一些新的佈局對象,但沒有記錄它們或創建如何使用它們的示例(我是一個忙碌的傢伙!:p)其中之一被稱爲PageView,它使用適配器一個ListView,所以如果你知道如何使用ListView,你應該可以使用它。關鍵是使用.setAdapter(BaseAdapter適配器)方法。

請確保您使用的是github上的最新版本。這裏是直接鏈接到GitHub上的PageView類代碼的鏈接:https://github.com/fry15/uk.co.jasonfry.android.tools/blob/master/src/uk/co/jasonfry/android/tools/widget/PageView.java