在 FragmentPagerAdapter 中交换片段
Swap Fragement in FragmentPagerAdapter
我已经为三个片段实现了默认的片段寻呼机适配器。它工作正常,但我想要的是从这三个片段之一交换片段(不是来自 activity)。我试图搜索 ans 但找不到它。
编辑:
我正在使用 DEFAULT tablayout activity 和 slider.I 我没有使用自定义 fragmentPagerAdapter。下面 class 在 main activity.
中实现
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FragmentTest1();
case 1:
return new FragmentTest2();
case 2:
return new FragmentTest3();
default:
return null;
}}
@Override
public int getCount() {
return 3;
}
MainActivity
下面给出了 main activity 的 onCreate 方法...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slider);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
FragmenTest1
我想从 FragmentTest1 过渡到另一个片段。
public class FragmentTest1 extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_log, container, false);
Button button = rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
return rootView;
}
}
我想将点击事件转换到另一个片段...
我不记得执行此操作的确切代码,但这是可能的!
如果你通过你的片段访问片段父(应该是片段管理器?)然后告诉片段管理器切换到片段x?..
我记得不久前在一个应用程序中这样做过。它不是很漂亮,但很管用! (如果有效,那不是愚蠢的……是吗?)
我有相同的实现,我有一个 ViewPagerFragment
使用 ImageAdapter
在片段之间交换,这是一个 ImageFragment
,所以基本上你需要一个适配器。
ViewPagerFragment:
public class ViewPagerFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_viewpager, container, false);
ButterKnife.bind(this, view);
mAdapter = new ImageAdapter(getChildFragmentManager(), yourObjects);
mViewPager.setAdapter(mAdapter);
return view;
}
ImageAdapter:
public class ImageAdapter extends FragmentStatePagerAdapter {
private ArrayList<YourObject> mYourObjects;
public ImageAdapter(FragmentManager fm, ArrayList<YourObject> yourObjects) {
super(fm);
mYourObjects = yourObjects;
}
@Override
public Fragment getItem(int position) {
return new ImageFragment().newInstance(position, mYourObjects);
}
@Override
public int getCount() {
return mYourObjects.size();
}
}
最后 ImageFragment:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_image, container, false);
ButterKnife.bind(this, view);
mClickedImagePosition = getArguments().getInt(POSITION_KEY);
mYourObject = getArguments().getParcelableArrayList(YOUR_OBJECT_KEY);
// set your objects here
return view;
}
public ImageFragment newInstance(int position, ArrayList<YourObject> yourObjects) {
ImageFragment fragment = new ImageFragment();
Bundle bundle = new Bundle();
bundle.putInt(POSITION_KEY, position);
bundle.putParcelableArrayList(YOUR_OBJECT_KEY, yourObjects);
fragment.setArguments(bundle);
return fragment;
}
我省略了一些样板代码,例如膨胀视图,但这会起作用。如果您有任何问题,请告诉我。
在这种情况下,片段只能通过 viewpager 访问(据我所知),要使 viewpager 成为子片段,您必须在 MainActivity 中将 viewpager 设为静态。
public static ViewPager viewPager;
访问viewPager
ParentActivity.viewPager.arrowScroll(View.FOCUS_RIGHT);
执行操作
ParentActivity.viewPager.arrowScroll(View.FOCUS_LEFT);
参考:
我已经为三个片段实现了默认的片段寻呼机适配器。它工作正常,但我想要的是从这三个片段之一交换片段(不是来自 activity)。我试图搜索 ans 但找不到它。
编辑: 我正在使用 DEFAULT tablayout activity 和 slider.I 我没有使用自定义 fragmentPagerAdapter。下面 class 在 main activity.
中实现public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FragmentTest1();
case 1:
return new FragmentTest2();
case 2:
return new FragmentTest3();
default:
return null;
}}
@Override
public int getCount() {
return 3;
}
MainActivity 下面给出了 main activity 的 onCreate 方法...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slider);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
FragmenTest1 我想从 FragmentTest1 过渡到另一个片段。
public class FragmentTest1 extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_log, container, false);
Button button = rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
return rootView;
}
}
我想将点击事件转换到另一个片段...
我不记得执行此操作的确切代码,但这是可能的! 如果你通过你的片段访问片段父(应该是片段管理器?)然后告诉片段管理器切换到片段x?..
我记得不久前在一个应用程序中这样做过。它不是很漂亮,但很管用! (如果有效,那不是愚蠢的……是吗?)
我有相同的实现,我有一个 ViewPagerFragment
使用 ImageAdapter
在片段之间交换,这是一个 ImageFragment
,所以基本上你需要一个适配器。
ViewPagerFragment:
public class ViewPagerFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_viewpager, container, false);
ButterKnife.bind(this, view);
mAdapter = new ImageAdapter(getChildFragmentManager(), yourObjects);
mViewPager.setAdapter(mAdapter);
return view;
}
ImageAdapter:
public class ImageAdapter extends FragmentStatePagerAdapter {
private ArrayList<YourObject> mYourObjects;
public ImageAdapter(FragmentManager fm, ArrayList<YourObject> yourObjects) {
super(fm);
mYourObjects = yourObjects;
}
@Override
public Fragment getItem(int position) {
return new ImageFragment().newInstance(position, mYourObjects);
}
@Override
public int getCount() {
return mYourObjects.size();
}
}
最后 ImageFragment:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_image, container, false);
ButterKnife.bind(this, view);
mClickedImagePosition = getArguments().getInt(POSITION_KEY);
mYourObject = getArguments().getParcelableArrayList(YOUR_OBJECT_KEY);
// set your objects here
return view;
}
public ImageFragment newInstance(int position, ArrayList<YourObject> yourObjects) {
ImageFragment fragment = new ImageFragment();
Bundle bundle = new Bundle();
bundle.putInt(POSITION_KEY, position);
bundle.putParcelableArrayList(YOUR_OBJECT_KEY, yourObjects);
fragment.setArguments(bundle);
return fragment;
}
我省略了一些样板代码,例如膨胀视图,但这会起作用。如果您有任何问题,请告诉我。
在这种情况下,片段只能通过 viewpager 访问(据我所知),要使 viewpager 成为子片段,您必须在 MainActivity 中将 viewpager 设为静态。
public static ViewPager viewPager;
访问viewPager
ParentActivity.viewPager.arrowScroll(View.FOCUS_RIGHT);
执行操作
ParentActivity.viewPager.arrowScroll(View.FOCUS_LEFT);
参考: