我想用onClick方法获取嵌套片段。我无法使用自己的xml工作来获取内部片段。执行后我收到以下错误: 找不到片段InnerFragment的id 0x7f0d0086(com.none.myapp:id / menu_fragment)的视图。 有没有正确的方法来使用Viewpager检索内部片段?
FRAGMENT1 - >按钮---> NESTED FRAGMENT
这是我的MainActivity:
public class MainActivity extends AppCompatActivity {
private SectionsPageAdapter mSectionPageAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
//Assigns the ViePager to TabLayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
//Defines the number of tabs by setting appropriate fragment and tab name.
private void setupViewPager(ViewPager viewPager){
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new ScanFragment(),"TEST1");
adapter.addFragment(new FilesFragment(),"TEST2");
adapter.addFragment(new QuizFragment(),"TEST3");
viewPager.setAdapter(adapter);
}}
这是外部碎片:
public class TEST1 extends Fragment{
private Button btnTest1;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.quiz_layout, container, false);
btnTest1 = (Button) view.findViewById(R.id.btn1);
btnTest1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
getChildFragmentManager()
.beginTransaction()
.add(R.id.fragContainer, new InnerFragment())
.commit();
}
});
return view;
}
}
这应该是内部碎片:
public class InnerFragment extends Fragment{
public InnerFragment(){
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.inner_fragment, container, false);
return view;
}}
}
外部xml片段:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragContainer"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="300dp"
android:layout_height="60dp"
android:onClick="buttonOnClick"
android:text="Quiz"
android:textColor="#ffffff"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="207dp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
答案 0 :(得分:1)
您可以尝试通过标记从分页器中获取片段。
查看寻呼机标记片段及其自己的标记,它是android:switcher:viewId:index。
这是执行此操作的简单方法。
private static String getFragmentTag(int viewPagerId, int fragmentIndex) {
return "android:switcher:" + viewPagerId+ ":" + fragmentIndex;
}
如何从viewPager获取片段的示例:
String fragmentTag = getFragmentTag(mViewPager.getViewId(), 0);// 0 - is pos of first fragment
Fragment yourFragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
但据我所知,你想要在片段内部获得片段。 所以基本上你需要提供从TEST1片段返回这个片段的方法。
public class TEST1 extends Fragment{
private Button btnTest1;
private InnerFragment innerFragment = new InnerFragment()
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.quiz_layout, container, false);
btnTest1 = (Button) view.findViewById(R.id.btn1);
btnTest1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
getChildFragmentManager()
.beginTransaction()
.add(R.id.fragContainer, innerFragment )
.commit();
}
});
return view;
}
public InnerFragment getInnerFragment() {
return innerFragment;
}
}