我已经为三个片段实现了默认的片段寻呼机适配器。它的工作正常,但我想要的是从这三个片段中的一个内部交换片段(不是来自活动)。我试图搜索ans但无法找到它。
修改 我正在使用带滑块的DEFAULT tablayout活动。我没有使用自定义fragmentPagerAdapter。下面的课程在主要活动中实施。
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 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;
}
}
我想将点击事件转换为另一个片段...
答案 0 :(得分:1)
我有相同的实现,我有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;
}
我省略了一些样板代码,比如膨胀视图,但这样可行。如果您有任何问题,请告诉我。
答案 1 :(得分:0)
我无法记住要执行此操作的确切代码,但这是可能的! 如果您通过Fragment访问片段parent(应该是片段管理器?)然后告诉片段管理器切换到片段x?..
我记得不久前在一个应用程序中这样做了......它不漂亮但它有效! (如果它起作用a傻......或者是它?)
答案 2 :(得分:0)
在这种情况下,片段只能通过viewpager访问(据我所知),并且要对子片段进行viewpager,你必须在MainActivity中使viewpager成为静态。
public static ViewPager viewPager;
访问viewPager
ParentActivity.viewPager.arrowScroll(View.FOCUS_RIGHT);
执行操作
ParentActivity.viewPager.arrowScroll(View.FOCUS_LEFT);