我有一个MainActivty和2个不同的片段(片段A&B)。我想做的是,当用户单击片段A上的某些内容时,它将自动滑动到片段B,并更新片段B中的textView。但是现在的问题是它不会更新片段B中的textView。这是我的代码
MainActivity
public class MainRegisterCompleteInfo extends AppIntro implement FragmentA.OnFragmentInteractionListener
,FragmentB.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addSlide(new FragmentA());
addSlide(new FragmentB());
fragmentB = new FragmentB();
getSupportFragmentManager().beginTransaction().add(fragmentB, "fragmentB").commit();
//int currentPage = pager.getCurrentItem();
//Toast.makeText(this, String.valueOf(currentPage), Toast.LENGTH_SHORT).show();
}
//This is a interface from fragment A
@Override
public void setUniversityName(String universityName) {
pager.setCurrentItem(3);
FragmentB.updateText(universityName);
}
}
片段A
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void setUniversityName(String universityName);
}
片段B
public void updateText(String text){
txtTest.setText(text);
Toast.makeText(getContext(), txtTest.getText().toString(), Toast.LENGTH_SHORT).show();
}
我尝试搜索几个类似的问题,但仍然无法解决。片段B中的textView不会更新文本。谁能告诉我我在做什么错?