我有一个带有ViewPager的Activity,ViewPager的每个页面都有一个片段。
在我的Screen3Fragment
内,我有一个LinearLayout(lly_fragments
),其中我展示了一些其他片段。我首先展示片段Screen3_1
public class Screen3Fragment extends Fragment {
private FragmentManager manager;
private FragmentTransaction transaction;
public static Screen3Fragment newInstance() {
final Screen3Fragment mf = new Screen3Fragment();
return mf;
}
public Screen3Fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_screen3, container, false);
Screen3_1Fragment frag31 = new Screen3_1Fragment();
manager = getChildFragmentManager();
transaction = manager.beginTransaction();
transaction.add(R.id.lly_fragments,frag31,"frag31");
transaction.addToBackStack("frag31");
transaction.commit();
return v;
}
}
这没问题。问题来自于frag31
(在Screen3Fragment内),我想调用fragt32
,因为我执行以下操作。
public class Screen3_1Fragment extends Fragment {
private ImageButton imgbt_timer;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_screen3_1,container,false);
imgbt_timer = (ImageButton) v.findViewById(R.id.bT_scr31_timer);
imgbt_timer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getChildFragmentManager().beginTransaction()
.replace(R.id.lly_fragments, new Screen3_2Fragment(), "frag32")
.commit();
}
});
return v;
}
}
正如我在其他答案中所读到的那样,行transaction.replace
应该执行此操作,并将同一给定容器frag31
内的给定frag32
替换现有lly_fragments
。< / p>
但是,我得到java.lang.IllegalArgumentException: No view found for id.....
我不确定原因。
答案 0 :(得分:0)
getFragmentManager()
将始终返回父级的属性,大多数情况下是活动。 getChildFragmentManager()
将返回父属性(在您的情况下,Screen3Fragment
属性)。在片段中添加片段时应该使用它。
在您的情况下,应使用Screen3Fragment
添加getFragmentManager()
,并使用Screen3_1Fragment
添加getChildFragmentManager()
因为Screen3_1Fragment
是Screen3Fragment
子项。 Screen3Fragment
是父母。
我要求您始终getFragmentManager()
使用add
方法,而不是replace
,因为您的父母将是相同的。
getChildFragmentManager()
。
答案 1 :(得分:0)
您可以使用回调,如下所示: 对我有用,我希望我的回答可以帮助你
1)创建一个界面
public interface ChangeFragmentListener {
void onChangeFragmentLicked(int fragment);
}
2)在您的活动中实施界面和交易方法:
public class MainActivity extends AppCompatActivity implements ChangeFragmentListener {
FragmentTransaction fragmentTransaction;
Fragment1 fragment1;
Fragment2 fragment2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
fragment1 = new Fragment1();
fragment1.setChangeFragmentListener(this);
fragment2 = new Fragment2();
fragment2.setChangeFragmentListener(this);
initListeners();
}
void changeToFrag1() {
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.activity_main_fragment_container,fragment1, "");
fragmentTransaction.commit();
}
void changeToFrag2() {
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.activity_main_fragment_container, fragment2, "");
fragmentTransaction.commit();
}
@Override
public void onChangeFragmentLicked(int fragment) {
switch (fragment){
case 1:
changeToFrag1();
break;
case 2:
changeToFrag2();
break;
}
}
3)从界面创建对象以处理回调:
public class Fragment1 extends Fragment {
private ChangeFragmentListener changeFragmentListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
view.findViewById(R.id.fragment1_textView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeFragmentListener.onChangeFragmentLicked(2);
}
});
return view;
}
public Fragment1 setChangeFragmentListener(ChangeFragmentListener changeFragmentListener) {
this.changeFragmentListener = changeFragmentListener;
return this;
}
}