android - 将 FragmentPagerAdapter 设置为 ViewPager 以编程方式添加 fragment

标签 android android-fragments

这是我的 Activity xml 布局,我在 relativeLayout 下方放置了一个用于显示选项卡的 viewPager

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <RelativeLayout
                android:id="@+id/WordContainer"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            <!-- Titolo -->
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:id="@+id/txtParola"/>
            <!-- tipo -->
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                    android:id="@+id/txtTipo"/>
    </RelativeLayout>
    <android.support.v4.view.ViewPager
                                       android:id="@+id/pager"
                                       android:layout_width="match_parent"
                                       android:layout_height="match_parent" />

这是我的 FragmentPagerAdapter 实现

public class TabsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int index) {
        return null;
    }

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

我想以编程方式将 fragment 添加到 viewPager,但如何从我的 main_activity 中执行此操作?

(我无法使用 FragmentActivity 扩展 main_activity,因为我需要一个简单的 Activity 实现来访问 TextView)

最佳答案

我假设您正在尝试使用操作栏选项卡实现滑动 View 。以下是您需要执行的基本步骤:

  1. 在您的主要 Activity 中,扩充布局 xml 文件并获取对 xml 中定义的 ViewPager 的引用。

    mViewPager = (ViewPager) findViewById(R.id.pager);

  2. 在您的主要 Activity 中,创建一个 TabsPagerAdapater,并使用 TabsPagerAdapter 设置 ViewPager 的适配器

    mPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager()); mViewPager.setAdapter(mPagerAdapter);

  3. 每个选项卡 View 实际上由一个在 TabsPagerAdapter 的 getItem() 中创建的 fragment 呈现。以下是您在程序中的做法:

    公共(public)类 TabsPagerAdapter 扩展 FragmentPagerAdapter { 我的 fragment 我的 fragment ; String[] mTitles = {"Tab1", "Tab2", "Tab3"}

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    
    @Override
    public Fragment getItem(int index) {
    
        // Create the fragment here. Assuming you already have a fragment class
        // called "MyFragment", you just need to instantiate an 
        // instance of MyFragment and return it
    
        myFragment = new MyFragment();
        return myFragment;
    }
    
    @Override
    public int getCount() {
        // Return the number of tabs, assuming you have 3 tabs here.
        return mTitles.length;
    }
    
    @Override 
    public CharSequence getPageTitle(int position) 
        // Return the title of each tab
        // Assuming title string are stored in mTitle array
        // 
        return mTitles[position];
    }
    

希望对您有所帮助。

关于android - 将 FragmentPagerAdapter 设置为 ViewPager 以编程方式添加 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22363490/

相关文章:

java - 在 Fragment 内调用 getActivity() 返回 null

android - 如何在android中包含active X?

android - 如何从 adb 设置指纹锁屏?

java - Android View ,带选项卡,无需 Activity

android - 并非 ViewPager 的 fragment 中的所有 View 都会更新

android - onCreate 方法在方向更改时调用两次

android - 如何创建一个只有 1 个广播接收器的 android 应用程序?

android - 在不同 fragment 上使用相同的 View 模型

android - 触摸外部时关闭对话框

android - 按下后仅删除 fragment