在ViewPager中打开新视图

时间:2017-02-09 20:54:36

标签: java android android-fragments

在我的Android应用上使用Devlight NavigationTabBar。这是整个代码:

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_horizontal_ntb);
        initUI();
    }

    private void initUI() {
        final ViewPager viewPager = (ViewPager) findViewById(R.id.vp_horizontal_ntb);
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return 5;
            }

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

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

            @Override
            public Object instantiateItem(final ViewGroup container, final int position) {

                final View viewNews = LayoutInflater.from(
                        getBaseContext()).inflate(R.layout.item_vp_list, null, false);

                final View ViewSol = LayoutInflater.from(
                        getBaseContext()).inflate(R.layout.activity_sol, null, false);

                final View viewProfile = LayoutInflater.from(
                        getBaseContext()).inflate(R.layout.activity_profile, null, false);

                final View viewContact = LayoutInflater.from(
                        getBaseContext()).inflate(R.layout.activity_contact, null, false);

                View finalView = null;

                if (position == 0) {
                    final RecyclerView recyclerView = (RecyclerView) viewNews.findViewById(R.id.rv);
                    recyclerView.setHasFixedSize(true);
                    recyclerView.setLayoutManager(new LinearLayoutManager(
                                    getBaseContext(), LinearLayoutManager.VERTICAL, false
                            )
                    );
                    recyclerView.setAdapter(new RecycleAdapter());
                    container.addView(viewNews);
                    finalView = viewNews;
                } else if (position == 1) {
                    container.addView(viewSol);
                    finalView = viewSol;
                } else if (position == 2) {
                    container.addView(viewProfile);
                    finalView = viewProfile;
                } else if (position == 3){
                    container.addView(viewContact);
                    finalView = viewContact;
                }

                return finalView;
            }
        });

        final String[] colors = getResources().getStringArray(R.array.default_preview);

        final NavigationTabBar navigationTabBar = (NavigationTabBar) findViewById(R.id.ntb_horizontal);
        final ArrayList<NavigationTabBar.Model> models = new ArrayList<>();
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_library),
                        Color.parseColor(colors[0]))
                        .selectedIcon(getResources().getDrawable(R.drawable.ic_library))
                        .title("News")
                        .badgeTitle("+10")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_error_black_24dp),
                        Color.parseColor(colors[1]))
//                        .selectedIcon(getResources().getDrawable(R.drawable.ic_eighth))
                        .title("Solicitation")
                        //.badgeTitle("with")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_person_black_24dp),
                        Color.parseColor(colors[3]))
//                        .selectedIcon(getResources().getDrawable(R.drawable.ic_eighth))
                        .title("My Account")
                        //.badgeTitle("icon")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_phone_black_24dp),
                        Color.parseColor(colors[2]))
                        .selectedIcon(getResources().getDrawable(R.drawable.ic_phone_black_24dp))
                        .title("Contact")
                        //.badgeTitle("state")
                        .build()
        );

        navigationTabBar.setModels(models);
        navigationTabBar.setViewPager(viewPager, 2);
        navigationTabBar.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(final int position) {
                navigationTabBar.getModels().get(position).hideBadge();
            }

            @Override
            public void onPageScrollStateChanged(final int state) {

            }
        });

        navigationTabBar.postDelayed(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < navigationTabBar.getModels().size(); i++) {
                    final NavigationTabBar.Model model = navigationTabBar.getModels().get(i);
                    navigationTabBar.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //model.showBadge();
                        }
                    }, i * 100);
                }
            }
        }, 500);
    }

正如您所看到的,每当我选择一个标签时,它都会打开视图寻呼机内的视图。

但是,当我点击按钮时,我不知道如何打开另一个视图。我试图以意图开始一项全新的活动,但它崩溃了,而且它并不是我想要的。

我想要的是在viewpager ViewPager viewPager = (ViewPager) findViewById(R.id.vp_horizontal_ntb);

上打开视图

如果我实现这个:

public void openNewView(View view) {
    //the code to open a view inside the viewpager should go here
}

我该怎么办?

2 个答案:

答案 0 :(得分:0)

在最初设置为Visibility = GONE或Invisible的布局中为viewPager添加一个视图,并在希望它在屏幕上显示时将其更改为Visible。

https://developer.android.com/reference/android/view/View.html#GONE

或使用layoutInflater将其他布局的视图添加到viewpager

https://developer.android.com/reference/android/view/LayoutInflater.html

答案 1 :(得分:0)

要点击某个按钮打开新视图,您可以执行以下操作:

假设您想要在第一个按钮上有一个新视图,那么在PagerAdapter的instantiateItem方法中,您可以检查位置是否为0。

如果位置为0,那么您可以膨胀要显示的布局。

类似的东西:

            @Override
            public Object instantiateItem(final ViewGroup container, final int position) {
if (position == 0)
{
                final View view = LayoutInflater.from(
                        getBaseContext()).inflate(R.layout.your_layout, null, false);

                final TextView txtPage = (TextView) view.findViewById(R.id.txt_vp_item_page);
                txtPage.setText(String.format("Page #%d", position));

                container.addView(view);
                return view;
}
else if (position == 1)
{
                final View view = LayoutInflater.from(
                        getBaseContext()).inflate(R.layout.item_vp, null, false);

                final TextView txtPage = (TextView) view.findViewById(R.id.txt_vp_item_page);
                txtPage.setText(String.format("Page #%d", position));

                container.addView(view);
                return view;
}
else
{

                final View view = LayoutInflater.from(
                        getBaseContext()).inflate(R.layout.item_vp, null, false);

                final TextView txtPage = (TextView) view.findViewById(R.id.txt_vp_item_page);
                txtPage.setText(String.format("Page #%d", position));

                container.addView(view);
                return view;
}
            }

为了使您的代码更加模块化,您可以在适配器中使用自定义片段和它们。

希望这有帮助