【问题标题】:ViewPager and Fragments: Why do I always see the same thing?ViewPager 和 Fragments:为什么我总是看到相同的东西?
【发布时间】:2013-09-20 17:07:42
【问题描述】:

我创建了一个包含三个“页面”的 ViewPager。代码是这样的

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

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

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

<?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

<?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>

但现在我在所有三个页面中都看到了相同的东西。例如,如果我在第 2 页上我想要一个带有文本“这是第 2 页”的 TextView,在第三页上是一个带有文本“这是第 3 页”的 TextView,在第一页中是两个带有按钮的 TextView.. 。 我怎样才能?我快疯了,请把代码传给我来做这件事。请。

【问题讨论】:

    标签: java android xml android-fragments


    【解决方案1】:

    膨胀PageFragment 的布局后,您需要获取TextView 的引用,这样您就可以通过Bundle 显示其位置,您使用setArguments() 传递。使用onCreateView() 中的view 变量来获取TextView 的引用。 (即view.findViewById())。然后在您的PageFragment 中使用getArguments() 来检索具有该位置的Bundle,并将TextView 设置为该值。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        只需在页面片段类中创建一个函数来配置元素并修改 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);
                }
            }
        

        然后在getItem函数中调用configure函数

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

        【讨论】:

        • 如果我想添加其他内容怎么办?例如按钮等。我不应该为每个页面xml创建一个布局?
        • 什么意思?每页不同的布局?还是相同的布局?此外,我刚刚注意到我的答案是错误的。 Emmanuel 答案更好。你应该通过 bundle 传递参数。
        • 是的,我的意思是每个页面都有不同的布局。你是怎样做的?我应该改变什么?
        • 如果布局明显不同,您将不得不创建多个布局。如果您只想更改 TextView 上的文本,则不需要另一个 xml。如果您确实需要显着不同的布局,只需创建一个新的 Fragment 并在 onCreateView() 中扩展不同的布局
        • 你已经在这样做了。只需创建一个扩展 Fragment 的新 Class 并扩展另一个布局,就像你正在做的那样。然后在getView()position 周围放置一个switch 语句,并根据position 值实例化您想要的特定Fragment
        猜你喜欢
        • 2015-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-13
        • 2014-11-19
        • 1970-01-01
        相关资源
        最近更新 更多