在我的计划中,我使用tabs
实施了fragments
。我在Tab2中放置了EditText
。我想在Tab2
onCreateView
的{{1}}()内写fragment
的所有主要代码,以便每次向Tab2
移动时都这样代码被执行。为了测试每次刷到Tab2时是否调用Tab2
(),我在onCreateView
的{{1}}()中写了以下语句:
onCreateView
现在,当我运行应用程序并滑动到Tab2时,我看到'默认'书面。现在,如果我手动更改此文本,然后滑动到Tab1并返回到Tab2-我仍然看到写入相同的文本。这显然意味着当我第二次刷到Tab2时没有再次调用Tab2
()?我该如何解决这个问题?
Tab2.java
text.setText("default");
MainActivity.java
onCreateView
答案 0 :(得分:0)
如果您使用ViewPager
ViewPager viewPager;
尝试在setOnPageChangeListener
MainActivity
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if(mFragmentList.size() > position && mFragmentList.get(position) instanceof Tab2)
{
((Tab2)mFragmentList.get(position)).changeText("changed text")
}
// you can use interface insted
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
Tab2
中的
public void changeText(String string){
text.setText(string);
}
更新 MainActivity.java
public class MainActivity extends AppCompatActivity
{
public static int itemPosition;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
ViewPagerAdapter adapter;
private final List<Fragment> mFragmentLists = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.setOffscreenPageLimit(0);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if(mFragmentList.size() > position && mFragmentList.get(position) instanceof Tab2)
{
((Tab2)mFragmentList.get(position)).changeText("changed text")
}
// you can use interface insted
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void setupViewPager(ViewPager viewPager)
{
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1(), "ONE");
adapter.addFragment(new Tab2(), "TWO");
viewPager.setAdapter(adapter);
mFragmentLists = adapter.getFragements();
}
class ViewPagerAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public List<Fragment> getFragements()
{
return mFragmentList;
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}