简体   繁体   English

ViewPager和Fragments:为什么我总是看到相同的东西?

[英]ViewPager and Fragments: Why do I always see the same thing?

I have created a ViewPager with three "pages". 我创建了具有三个“页面”的ViewPager。 The code is this 代码是这个

MainActivity.java MainActivity.java

public class MainActivity extends FragmentActivity {

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


        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_tab_strip);
        FragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(
                getSupportFragmentManager());

        viewPager.setAdapter(fragmentPagerAdapter);
        pagerTabStrip.setDrawFullUnderline(true);
        pagerTabStrip.setTabIndicatorColor(Color.DKGRAY);
    }

}

MyFragmentPageAdapter.java MyFragmentPageAdapter.java

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private String[] pageTitle = {
            "Page1", "Page2", "Page3"
    };

    public MyFragmentPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = new PageFragment();
        Bundle arguments = new Bundle();
        arguments.putString("pageIndex", Integer.toString(position + 1));
        fragment.setArguments(arguments);
        return fragment;
    }

    @Override
    public int getCount() {
        return pageTitle.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return pageTitle[position];
    }

}

PageFragment.java PageFragment.java

public class PageFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page, null);

    return view;

    }
}

activity_main.xml activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_tab_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33B5E5"
        android:textColor="#FFFFFF"
        android:paddingTop="10dp"
        android:paddingBottom="10dp" />

    </android.support.v4.view.ViewPager>

</RelativeLayout>

fragment_page.xml fragment_page.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:textSize="16sp"
        android:textStyle="italic"
        android:gravity="center_horizontal"
        android:textColor="@color/red"
        android:text="@string/inf" />

        <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="60dp"
        android:textSize="28sp"
        android:gravity="center_horizontal"
        android:textStyle="bold"
        android:text="@string/ben" />

                <TextView
        android:id="@+id/textView3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="130dp"
        android:gravity="center_horizontal"
        android:textSize="18sp"
        android:text="Prova"
         />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_centerInParent="true"
            android:text="@string/verifica" />

</RelativeLayout>

But now i visualize in all three pages the same thing. 但是现在我在所有三个页面中都看到了同一件事。 If I for example on page 2 I want a TextView with the text "This is the 2 page" and on the third page a TextView with the text "This is the page 3" and in the first page two TextView with the button ... how can I? 例如,如果我在第2页上,我想要一个文本为“ This is the 2 page”的TextView,在第三页上是一个文本为“ This is the page 3”的TextView,而在第一页中则是两个带有按钮..的TextView。 。 我怎样才能? I'm going crazy, please let pass me the code to do this thing. 我快疯了,请让我通过代码来完成此操作。 Please. 请。

Once you inflate PageFragment 's layout you need to get a reference of the TextView so you can display the position on it via the Bundle you are passing using setArguments() . PageFragment的布局后,您需要获取TextView的引用,以便可以通过setArguments()传递的Bundle在其上显示位置。 Use your view variable inside onCreateView() to get a reference of the TextView . onCreateView()使用view变量获取TextView的引用。 (ie view.findViewById() ). (即view.findViewById() )。 Then use getArguments() in your PageFragment to retrieve the Bundle with that has position, and set the TextView to that value. 然后,在您的PageFragment使用getArguments()来检索具有位置的Bundle ,并将TextView设置为该值。

是您想要的一个很好的例子。

Just create a function in your page fragment class to configure elements and modify onCreateView to attach childs. 只需在页面片段类中创建一个函数即可配置元素,并修改onCreateView以附加子级。

public class PageFragment extends Fragment {

        TextView tv1 ;
        // ...

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page, null);
            /// Attach your childs
            tv1 = view.findViewById(R.id.textView1);

        return view;

        }

        public void configure(int position) {
            tv1 .setText("This is "+ position);
        }
    }

Then just call the function configure in getItem function 然后只需调用getItem函数中的configure函数

  @Override
        public Fragment getItem(int position) {
            PageFragment fr = new PageFragment();
            fr.configure(position + 1);         
            return fr;
        }

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

相关问题 为什么我总是在View.OnClickListener中看到最后一个ID? - Why do i always see the last id in View.OnClickListener? ViewPager 同时加载 2 个片段 - ViewPager loads 2 fragments at the same time 三元和 If/Else 编译成同样的东西,为什么? - Do Ternary and If/Else compile to the same thing, why? 如何在ViewPager的每个部分中使用片段 - How do I use fragments in each section of ViewPager 如何为ViewPager提供许多片段并避免错误代码? - How do I supply many Fragments to ViewPager and avoid bad code? 如何使用 viewpager2 和 fragmentstateadapter 动态添加和删除片段? - How do I dynamically add and delete fragments with viewpager2 and fragmentstateadapter? 我有一个相同片段的 ViewPager RecyclerView 只是在 ViewPager 的一个片段中加载项目 - I have a ViewPager by same fragments RecyclerView just load items in one fragment of ViewPager 我不明白我的输出。 为什么我两次得到相同的东西? - I do not understand my output. Why am I getting the same thing twice? 当用户到达ViewPager的末尾时,如何动态添加片段以启动ViewPager? - How do I dynamically add fragments to start of ViewPager as user reaches the end of the ViewPager? 为什么我在 eclipse 中看到两次相同的 class - Why do I see same class twice in eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM