简体   繁体   English

带有片段的ViewPager是过大的?

[英]ViewPager with Fragments is overkill?

I'm trying to implement a viewPager to display an array of strings (that contains three strings). 我正在尝试实现一个viewPager以显示字符串数组(包含三个字符串)。 I'm trying to look up a tutorial on how I would go about something like this, but it seems that I need to use a ViewPager with fragments. 我正在尝试查找有关如何进行此类操作的教程,但似乎我需要使用带有片段的ViewPager。 Isn't there a simple was to use a ViewPager without involving fragments? 使用ViewPager而不涉及片段是不是很简单?

Again, I'm following the developer docs: http://developer.android.com/training/animation/screen-slide.html 同样,我正在关注开发人员文档: http : //developer.android.com/training/animation/screen-slide.html

But, I don't necessarily want to make the entire screen move over. 但是,我不一定要使整个屏幕都移过去。 I'd only like for the bottom 1/3 of my screen to swipe. 我只想滑动屏幕的底部1/3。 Can anyone confirm that this is possible to do without fragments? 任何人都可以确认没有碎片的可能性吗? Or is there another view I should use (horizontal Scroll View) 还是我应该使用其他视图(水平滚动视图)

EDIT: 编辑:

So I was doing something right. 所以我做对了。 I was trying to use a ViewPager and a pagerAdapter but my application crashes. 我试图使用ViewPager和pagerAdapter,但是我的应用程序崩溃了。 Anything glaringly broken about my code? 我的代码有什么明显的问题吗?

  ViewPager mPager;
            setContentView(R.layout.activity_convo_detail);

            PagerAdapter mAdapter = new PagerAdapter() {

                @Override
                public boolean isViewFromObject(View arg0, Object arg1) {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public int getCount() {
                    // TODO Auto-generated method stub
                    return 3;
                }
            };
            mPager = (ViewPager)findViewById(R.id.pager);
            mPager.setAdapter(mAdapter);

You don't need Fragment s for ViewPager , Have a look : http://androidtrainningcenter.blogspot.com/2012/10/viewpager-example-in-android.html 您不需要FragmentViewPager ,看看: http : //androidtrainningcenter.blogspot.com/2012/10/viewpager-example-in-android.html

And for your second Question, this is how you take 1/3 of space: 对于第二个问题,这就是您如何占用1/3的空间:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<View
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="2" />

<android.support.v4.view.ViewPager
    android:id="@+id/myfivepanelpager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

</LinearLayout>

Fragments aren't that heavy, or complicated. 碎片并不那么重,也不复杂。 I don't think they are overkill for this. 我认为他们对此并不过分。

However, you can use a PagerAdapter without using fragments. 但是,您可以使用PagerAdapter而不使用片段。

A ViewPager can page plain old TextViews if that is what you want. 如果需要,ViewPager可以分页普通的旧TextView。

Basically, you don't have to use FragmentPagerAdapter. 基本上,您不必使用FragmentPagerAdapter。 Your PagerAdapter can take an array of Strings and return just a view for each. 您的PagerAdapter可以采用字符串数组,并且仅返回每个字符串的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM