【问题标题】:Android: Using ViewPager with multiple dynamic FragmentsAndroid:将 ViewPager 与多个动态片段一起使用
【发布时间】:2013-12-27 08:30:42
【问题描述】:

我正在使用 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>

【问题讨论】:

    标签: android dynamic android-viewpager fragment


    【解决方案1】:

    如何使片段布局动态化?

    与您制作任何其他“动态布局”的方式相同。如果您想将图像放入ImageView,请致电setImageBitmap()setImageDrawable() 或其他任何方式。例如,PagerAdapter 可以将position 提供给片段(通过工厂方法),片段然后可以知道要加载什么图像。

    This sample project 演示使用基于页面的position 的自定义值填充EditTexthint

    关于“继续”按钮,要么有一个单独的片段类(和你的PagerAdapter 中的适当智能,或者总是在你的布局中拥有按钮,但默认设置为android:visibility="gone",切换它通过setVisibility(View.VISIBLE) 获取需要它的片段。

    【讨论】:

    • 我在查看您的示例项目后更改了我的代码,但是当我进入活动时它没有正确加载。它不会崩溃,只是不会加载并重新加载 Main Activity。我更新了上面的代码。你能给我一两个指点吗?
    • @D-Rock:“它只是不加载并重新加载主活动”——我不知道你的意思是什么,抱歉。除了让images 成为static final int[] 之外,我看不出你在这里的任何明显问题。
    • 高分辨率图像位于常规 drawables 文件夹而不是 xhdpi 文件夹中存在问题。我不得不在 LogCat 中搜索错误。感谢您为我指明正确的方向!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2017-10-13
    • 2019-03-01
    相关资源
    最近更新 更多