Android:如何在 ViewPager 中创建不同的视图?

Android:How to create different view in ViewPager?(Android:如何在 ViewPager 中创建不同的视图?)
本文介绍了Android:如何在 ViewPager 中创建不同的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用 Android Studio 创建了带有 ViewPager 的 Main_Activity,它有这样的代码

I have create Main_Activity with ViewPager with Android Studio that has a code like this

public class MainActivity extends Activity {
    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager;

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

        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());        
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {       

          getMenuInflater().inflate(R.menu.main, menu);
          return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

         int id = item.getItemId();
         if (id == R.id.action_settings) {
         return true;
         }
         return super.onOptionsItemSelected(item);
    }

 public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {           
        return PlaceholderFragment.newInstance(position + 1);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}

public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}

}

在这种情况下,有 3 个页面可以正常工作.我的问题是,如果我想创建自定义视图,例如:第 1 页:有 3 个按钮 4 个单选按钮第 2 页:有 2 个按钮 2 个 TextView第 3 页:有 1 个按钮 2 Imageview

In this case has 3 pages that work fine.My question is,if i want to create custom view like: Page 1: has 3 button 4 radiobutton Page 2: has 2 Button 2 TextView Page 3: has 1 button 2 Imageview

怎么做,我应该把代码放在哪里.对不起我的语法,谢谢大家的回答.

How to do it and where i should put the code in.Sorry for my grammar ,Thank you for all answer.

推荐答案

在我的另一篇文章中你的问题的一个非常简单的实现.

A very simple implementation of your question in my other post.

如何在带有 android 的视图寻呼机页面中使用滑动手势?

在那篇文章中,我提到了如何自定义每个布局.在每个布局中,您可以添加自己的按钮或文本视图或您想要添加的任何内容.例如,在那篇文章中,将布局放在 MainLayout 中的 ViewPager 中,而不是使用该图像视图和所有其他东西,您可以添加自己想要的项目.

In that post I have mention how you can customize every layout. In each layout you can add your own buttons or textviews or whatever you want to add. For example, in that post, Layout to put inside the ViewPager in MainLayout, instead of using that image view and all other things, you can add your own desired items.

我相信这会解决你的问题,而且你可以添加尽可能多的布局,然后像这样在适配器中进行更改:

I am sure that will solve you problem and also you can add as many layouts as you can and just make that change in the adapter like this:

private class MyPagerAdapter extends PagerAdapter {

这里可以是您的页数.您可以根据布局增加页数.

Here can be your page count.. You can increase you page count depending on your layouts.

public int getCount() {
            return 3;
        }

        public Object instantiateItem(ViewGroup container, int position) {
            LayoutInflater inflater = (LayoutInflater) container.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            // Using different layouts in the view pager instead of images.

            int resId = -1;

                   //Getting my layout's in my adapter. Three layouts defined.

您可以在此处添加您设计的布局.

Here you can add your layouts you have designed.

  switch (position) {
            case 0:
                resId = R.layout.tutorial1;
                break;
            case 1:
                resId = R.layout.tutorial2;
                break;
            case 2:
                resId = R.layout.tutorial3;
                break;

            }

            View view = inflater.inflate(resId, container, false);
            ((ViewPager) container).addView(view, 0);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

}

希望我回答了你的问题.. :)

Hope I answered your question .. :)

这篇关于Android:如何在 ViewPager 中创建不同的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)