当我向后滑动时,我的片段并不令人耳目一新,所以当我向后滑动时,我想知道如何刷新它。我在网上看到了很多不同的答案,但我不知道为什么他们不能在我的情况下工作。如果有人能帮助我,我将非常感谢,谢谢。
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Profile tab1 = new Profile();
return tab1;
case 1:
Puzzles tab2 = new Puzzles();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile";
case 1:
return "Puzzles";
}
return null;
}
}
}
我尝试setOffscreenPageLimit(0)
,使用onTabSelected
,FragmentStatePagerAdapter
,甚至
notifyDataSetChanged()
中的附加片段
public int getItemPosition(Object object) {
return POSITION_NONE;
}
这也不起作用。所以我希望有人可以提供帮助。
答案 0 :(得分:2)
如果要刷新特定Fragment
中的数据,则必须执行两项操作,
Activity
onResume()
Fragment
中显示更新后的数据
醇>
如果您希望在删除应用后保存数据,请将其存储在SharedPreference
中。希望它有所帮助!
答案 1 :(得分:0)
在你的片段类中添加它。
{{1}}
希望它有所帮助。