我正在尝试创建一个非常简单的Android应用程序,它使用FragmentPagerAdapter在三个片段之间滑动。每个片段都包含一个EditText,并且具有完全相同的代码。
期望的行为是,当用户更新EditText时,新值将保存到应用程序实例。然后,一旦选择了新片段,该新片段应显示已保存的值。由于某些原因,这不起作用。
我还希望焦点片段在应用程序恢复时显示已保存的数据(从后台返回)。这也行不通。
我真的很困惑为什么这么简单的东西如此困难!
到目前为止,这是我的代码:
StackOverflowDemoApplication.java:
public class StackOverflowDemoApplication extends Application {
private ApplicationData applicationData;
// the index of the last fragment that was displayed
private int lastItem = 0;
@Override
public void onCreate() {
applicationData = new ApplicationData();
}
public ApplicationData getApplicationData() {
return applicationData;
}
public int getLastItem() {
return lastItem;
}
public void setLastItem(int lastItem) {
this.lastItem = lastItem;
}
}
MainActivity.java
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
private static final String TAG = "MainActivity";
// the application instance
private StackOverflowDemoApplication application;
// the pager adapter
private SectionsPagerAdapter pagerAdapter;
// the view pager
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Save the application instance.
application = (StackOverflowDemoApplication) getApplication();
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(pagerAdapter);
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < pagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(pagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
pagerAdapter.notifyDataSetChanged();
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private int NUM_ITEMS = 3;
public SectionsPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
// get the application data instance
ApplicationData data = application.getApplicationData();
switch (position) {
case 0:
return SecondFragment.newInstance(data);
case 1:
return FirstFragment.newInstance(data);
case 2:
return ThirdFragment.newInstance(data);
default:
return null;
}
}
@Override
public int getCount() {
return NUM_ITEMS;
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "FIRST FRAGMENT";
case 1:
return "SECOND FRAGMENT";
case 2:
return "THIRD FRAGMENT";
default:
return null;
}
}
}
@Override
public void onResume() {
// load the previous fragment
viewPager.setCurrentItem(application.getLastItem());
super.onResume();
}
@Override
public void onPause() {
// save the last fragment we used
application.setLastItem(viewPager.getCurrentItem());
super.onPause();
}
}
FirstFragment.java
public class FirstFragment extends Fragment {
private static final String TAG = "FirstFragment";
// the activity reference
private Activity activity;
// the application data
private ApplicationData data;
// the edit text
private EditText editText;
// are we currently loading data for this fragment?
private boolean loadingData = false;
public FirstFragment(ApplicationData data) {
super();
this.data = data;
}
public static FirstFragment newInstance(ApplicationData data) {
Log.e(TAG, "New instance called");
FirstFragment fragment = new FirstFragment(data);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e(TAG, "Creating FirstFragment view");
// inflate the view
View view = inflater.inflate(R.layout.first_fragment_layout, container, false);
// get the activity instance
activity = getActivity();
// the textview
editText = (EditText) view.findViewById(R.id.textView);
editText.addTextChangedListener(textWatcher);
// update the ui from the data
updateUIFromData();
return view;
}
public void updateUIFromData() {
// we have started loading the data
loadingData = true;
// if there is data
if (null != data) {
// set the value
if (null != data.getStringValue()) {
editText.setText(data.getStringValue());
}
}
// done loading the data
loadingData = false;
}
private void updateDataFromUi() {
data.setStringValue(editText.getText().toString());
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// if we are not loading data
if (!loadingData) {
// update the data from the ui
updateDataFromUi();
}
}
@Override
public void afterTextChanged(Editable s) {
}
};
}
答案 0 :(得分:1)
尝试在片段对用户可见时更新视图,如下所示:
public class MyFragment extends Fragment
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (visible) {
**//Get new data and update views here**
}
}
答案 1 :(得分:0)
查看本教程,它可能对您有所帮助: http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
您不必在tabSelected回调中调用notifyDataSetChanged()。
答案 2 :(得分:0)
您应该在 onResumer()中更新片段数据,例如 TextView 或 EditText ......等等。请参阅示例:
Oncreate....
TextView mtextview = rootview.findViewById....
@Override
public void onResume() {
if(do some thing){
mtextview.setText("It did something");
}
super.onResume();
}