如何在片段 class 的 viewpager 中显示片段?

How to display fragment in viewpager, in fragment class?

我有一个 activity,其中包含导航抽屉,因为 class 我的导航项指向片段 classes,在其中一个片段 classes我正在使用动态视图寻呼机来显示项目,但是我如何使用片段 class 的片段管理器作为视图寻呼机的参数,它需要片段 activity 或 activity 的片段参数,需要一些解决方案。

我现在正在做类似的事情。我只有导航抽屉下面的 ViewPager,但如果你有更复杂的布局,同样的原则应该可以工作。

首先,我有 activity 布局(其中大部分是由 Android Studio 生成的)

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.you.yourapp.YourActivity">

    <FrameLayout android:id="@+id/your_container" android:layout_width="match_parent"
    android:layout_height="match_parent" />

    <fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.example.you.yourapp.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

activity 使用 FragmentManager 将另一个包含 ViewPager 的片段交换到 FrameLayout

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_your);

    ...

    Fragment fragment = YourFragment.newInstance(Bundle args);
    getSupportFragmentManager()
            .beginTransaction().replace(R.id.your_container, fragment)
            .commit();
}

有ViewPager的Fragment很简单

<android.support.v4.view.ViewPager android:id="@+id/your_view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

在 YourFragment.java 中实现你的 FragmentPagerAdapter(你可以将其作为内部 class,但我 运行 遇到静态分配问题,而且这更干净)

public class YourFragment extends android.support.v4.app.Fragment {

    private ViewPager mPager;

    public YourFragment() {

}

public static TaskFragment newInstance(Uri uri, String outlineText) {
    TaskFragment fragment = new YourFragment();

    Bundle args = new Bundle();
    //set args
    fragment.setArguments(args);

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                          Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_your, null);
    mPager = (ViewPager) rootView.findViewById(R.id.your_view_pager);

    TaskPagerAdapter adapter = new TaskPagerAdapter(getFragmentManager());
    mPager.setAdapter(adapter);

    return rootView;
}

...

private class YourPagerAdapter extends FragmentStatePagerAdapter {

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

    @Override
    public int getCount() {
        return count; //however you get the count
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        //set the arguments for your page
        return PageFragment.newInstance(args);
    }

}

}

预计到达时间: 所以现在为你的导航抽屉回调实现一个自定义接口

...
private NavigationDrawerCallbacks mCallbacks

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mCallbacks = (NavigationDrawerCallbacks) activity;
}

...

@Override
public void onClick(View view) { //whatever your click listener is
    Fragment fragment = getFragmentSomehow(); //get your fragment
    mCallbacks.onNavigationItemSelected(fragment);
}

...

public static interface NavigationDrawerCallbacks {
    void onNavigationDrawerItemSelected(Fragment fragment);
}

现在让 YourActivity 实现这个接口

public class YourFragment extends Activity
        implements NavigationDrawerCallbacks
{

...

    @Override
    public void onNavigationDrawerItemSelected(Fragment fragment) {
        getSupportFragmentManeger()
            .beginTransaction()
            .replace(R.id.you_container, fragment)
            .commit();
    }
...
}

我想这就是你的意思。 希望这有帮助