如何在ViewPager中添加自定义视图以进行演练

时间:2014-01-23 14:28:27

标签: android android-viewpager viewpagerindicator walkthrough

我使用ViewPagerIndicator像大多数流行的应用程序一样获取WalkThrough。但我无法理解如何在 ViewPager 中添加图片,其中显示了如何使用该应用程序。

Walk Through 我想要的就是这些走过。

Where I went to Where I went to 到目前为止我得到了什么。

我不知道

如何在 ViewPager 中添加 ImageView TextView 等自定义视图?

欢迎任何指导。

2 个答案:

答案 0 :(得分:0)

由于这个问题很老,我只想写一个简短的一般准则:

要使用内容填充viewpager,可以将片段添加到适配器,然后将适配器设置为viewpager。你可以在片段中做任何布局,放置图像/文本等。

答案 1 :(得分:0)

正如Inteist建议的那样,可以将任何布局放在片段中并将该片段提供给适配器。

片段:

    public final class SelectModelFragment extends Fragment {
    private static final String KEY_CONTENT = "SelectModelFragment:Content";
    private static String TAG = SelectModelFragment.class.getSimpleName();

    private SelectModel mSelectModelObj;

    private CircularImageView mImageView;

    public static SelectModelFragment newInstance(SelectModel obj) {
        SelectModelFragment fragment = new SelectModelFragment();
        fragment.mSelectModelObj =obj;

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            mSelectModelObj = savedInstanceState.getParcelable(KEY_CONTENT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_select_model, container, false);
        mImageView = (CircularImageView)view.findViewById(R.id.fragment_select_model_iv);
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(KEY_CONTENT, mSelectModelObj);
    }
}

片段适配器:

public class SelectModelAdapter extends FragmentPagerAdapter {

    ArrayList<SelectModel> mList;
    private int mCount;
    private static final String TAG = SelectModelAdapter.class.getSimpleName();

    public SelectModelAdapter(FragmentManager fm, ArrayList<SelectModel> mList) {
        super(fm);
        this.mList = mList;
        mCount =  mList.size();
    }

    @Override
    public Fragment getItem(int position) {
        return SelectModelFragment.newInstance(mList.get(position));
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return TAG;
    }

    public void setCount(int count) {
        if (count > 0 && count <= 10) {
            mCount = count;
            notifyDataSetChanged();
        }
    }
}

活动:ViewPager具有片段适配器,用于提供片段。

public class SelectModelActivity extends BaseSliderActivity {

    private ViewPager mPager;
    private SelectModelAdapter mAdapter;

    private ArrayList<SelectModel> mList;
    private void setAdapter() {
            mAdapter = new SelectModelAdapter(getSupportFragmentManager(), mList);
            mPager.setAdapter(mAdapter);
            mIndicator.setViewPager(mPager);
        }
}