安卓系统:使用ViewPager与多个动态片段



我正在使用ViewPager来显示9个片段。在每一个片段中,我只想展示一幅不同的画面。我想使用一个单独的片段布局,但在图片中动态添加。此外,我想在最后一个片段上添加一个"继续"按钮,按下后将转到另一个活动。

如何使片段布局动态化?

主要活动

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>

如何使片段布局动态化?

与使任何其他"布局动态"的方式相同。如果要将图像放入ImageView中,请调用setImageBitmap()setImageDrawable()或其他方法。例如,PagerAdapter可以向片段提供position(通过工厂方法),然后片段可以知道要加载什么图像。

此示例项目演示了使用基于页面的position的自定义值填充EditTexthint

关于"继续"按钮,可以为其创建一个单独的片段类(以及PagerAdapter中的适当智能),也可以在布局中始终设置该按钮,但默认情况下设置为android:visibility="gone",通过setVisibility(View.VISIBLE)为需要它的片段进行切换。

最新更新