2013-12-10 36 views
0

我正在使用ViewPager顯示9個片段。在這些片段中,我只想展示不同的圖片。我想使用一個單獨的片段佈局,但動態添加圖片。另外,想要在按下的最後一個片段上添加「繼續」按鈕將轉到另一個活動。Android:使用具有多個動態片段的ViewPager

我該如何做一個片段佈局動態?

主要活動

public class StoryboardPageActivity extends FragmentActivity { 
// The number of pages (wizard steps) to show in this demo. 
private static final int NUM_PAGES = 9; 

// The pager widget, which handles animation and allows swiping horizontally to access previous and next wizard steps. 
private ViewPager mPager; 

// The pager adapter, which provides the pages to the view pager widget. 
private PagerAdapter mPagerAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_storyboard_page); 

    // Instantiate a ViewPager and a PagerAdapter. 
    mPager = (ViewPager) findViewById(R.id.storyboardPager); 
    mPagerAdapter = new StoryboardPagerAdapter(getSupportFragmentManager()); 
    mPager.setAdapter(mPagerAdapter); 
} 

@Override 
public void onBackPressed() { 
    if (mPager.getCurrentItem() == 0) { 
     // If the user is currently looking at the first step, allow the system to handle the 
     // Back button. This calls finish() on this activity and pops the back stack. 
     super.onBackPressed(); 
    } else { 
     // Otherwise, select the previous step. 
     mPager.setCurrentItem(mPager.getCurrentItem() - 1); 
    } 
} 

// A simple pager adapter that represents 5 fragment objects, in sequence. 
private class StoryboardPagerAdapter extends FragmentStatePagerAdapter { 
    public StoryboardPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return StoryboardFragment.newInstance(position); 
    } 

    @Override 
    public int getCount() { 
     return NUM_PAGES; 
    } 
} 
} 

片段

public class StoryboardFragment extends Fragment { 
private static final String KEY_POSITION = "position"; 

static StoryboardFragment newInstance(int position) { 
    StoryboardFragment frag = new StoryboardFragment(); 
    Bundle args = new Bundle(); 

    args.putInt(KEY_POSITION, position); 
    frag.setArguments(args); 

    return(frag); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_storyboard_page, container, false); 

    ImageView image = (ImageView)rootView.findViewById(R.id.imgStoryboard); 
    int position = getArguments().getInt(KEY_POSITION, -1); 

    int[] images = {R.drawable.storyboard1, R.drawable.storyboard2, R.drawable.storyboard3, 
        R.drawable.storyboard4, R.drawable.storyboard5, R.drawable.storyboard6, 
        R.drawable.storyboard7, R.drawable.storyboard8, R.drawable.storyboard9}; 

    image.setImageResource(images[position]); 

    return rootView; 
} 

} 

片段XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#fff" > 

<ImageView 
    android:id="@+id/imgStoryboard" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:contentDescription="@string/storyboardSlide" /> 

</RelativeLayout> 

回答

2

如何讓片段佈局動態化?

與其他「佈局動態」一樣。如果你想把圖像放在ImageView,請致電setImageBitmap()setImageDrawable()或其他。例如,PagerAdapter可以將position提供給片段(通過工廠方法),然後片段可以知道要加載的圖像。

This sample project演示了使用基於頁面position的自定義值填充EditTexthint

關於「繼續」按鈕,或者有一個獨立的片段類(適當的智慧在你的PagerAdapter,或者總是在你的佈局按鈕,但默認設置爲android:visibility="gone",通過setVisibility(View.VISIBLE)切換它需要它的片段

+0

查看你的示例項目後,我改變了我的代碼,但是當我去看活動時,它沒有正確加載,它不會崩潰,但它不會加載並重新加載Main我更新了上面的代碼,你能給我一兩個指針嗎? –

+0

@ D-Rock:「它只是不加載和重新加載主要活動」 - 我不知道你的意思,對不起。除了使'images'成爲'static final int []',我沒有看到任何明顯的問題與你在這裏。 – CommonsWare

+0

這是一個高分辨率圖像在常規'drawables'文件夾而不是'xhdpi'文件夾中的問題。我不得不在LogCat中搜索錯誤。感謝您指點我正確的方向! –