我有一个带有五个标签的ViewPager。 我想在单击按钮时替换片段。
public class TimelineFragment extends RootFragment {
public TimelineFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.tab_timeline, container, false);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AccountFragment accountFragment = new AccountFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
// Store the Fragment in stack
transaction.replace(R.id.container_pager, accountFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return view;
}
}
问题:单击按钮时,我们可以看到带有textview的新片段,但始终显示旧片段的按钮。 为什么?
我遵循本教程:https://tausiq.wordpress.com/2014/06/06/android-multiple-fragments-stack-in-each-viewpager-tab/
答案 0 :(得分:0)
试试这个;
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View
FragmentManager ft = getSupportFragmentManager();
ft.beginTransaction()
.replace(R.id.container_pager, new accountFragment())
.addToBackStack("Back")
.commit();
}
});