我有一个标签活动,其中有x
个标签。所有选项卡在第一次加载时都会正确加载。如果我将x+2
选项卡从选项卡中移开,然后再返回,则某些数据和元素会丢失。
我使用了Android Studio自己的Tabbed Activity
生成模板以及选项卡的Fragment模板。
我已经审查了SO和其他一些人,但它们似乎与我的模型并不完全匹配。否则,我看不到。
有什么帮助吗?
这是我的主选项卡活动,其中包含一些相关的导入。
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
public class MasterTabActivity extends AppCompatActivity {
public static Intent newIntent(Context packageContext) {
Intent intent = new Intent(packageContext, MasterTabActivity.class);
Bundle bundle = new Bundle();
intent.putExtras(bundle);
return intent;
}
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_master_tab);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
//get some data
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
}
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);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
// Titles
tabLayout.addTab(tabLayout.newTab().setText("Details"));
tabLayout.addTab(tabLayout.newTab().setText("Photos"));
tabLayout.addTab(tabLayout.newTab().setText("Notes"));
//Add ICONS TO TEXT
tabLayout.getTabAt(0).setIcon(R.drawable.ic_details);
tabLayout.getTabAt(1).setIcon(R.drawable.ic_photos);
tabLayout.getTabAt(2).setIcon(R.drawable.ic_notes);
tabLayout.setTabTextColors(
ContextCompat.getColor(this, R.color.colorPrimary), //unselected
ContextCompat.getColor(this, R.color.colorDarkText) //selected
);
//Set the initial selected icons tab color
tabLayout.getTabAt(0).getIcon().setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.colorDarkText), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(1).getIcon().setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
// tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
tab.getIcon().setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.colorDarkText), PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
refreshData();
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return DetailsFragment.newInstance(createdCallKey);
case 1:
return PhotosFragment.newInstance(createdCallKey);
case 2:
return NotesFragment.newInstance(createdCallKey);
default:
return null;
}
}
@Override
public int getCount() {
return 3;
}
}
}
以及碎片的样本。至此,它们的设置几乎相同,只保留了一些基本的GUI元素。
import android.support.v4.app.Fragment;
public class DetailsFragment extends Fragment {
public DetailsFragment() {
// Required empty public constructor
}
public static DetailsFragment newInstance(String callKey) {
DetailsFragment fragment = new DetailsFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_details, container, false);
//do all the view setup
return rootView;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
}
最佳答案
在我的Fragments
中,我向Tabbed Activity
查询一些数据。当片段移动超过x+2
选项卡时,将调用Fragments onPause
方法(以及其他拆卸方法)。检查lifecycle documents片段的所有阶段。
返回选项卡时,将使用onCreateView
方法(以及其他构建方法,除了1onAttach1和onCreate
。在上面的示例中,该片段在onCreate
中查询Activity。
所以我创建了refreshData方法:
private void refreshData() {
someTextElement.setText(((MasterTabActivity) getActivity()).getCallDetailsDictionary().getString("CALdCallTaken").getString("SomeKey"));
}
并将对
refreshData
的调用移至onStart
,首先检查活动中的数据是否不为空。 @Override
public void onStart() {
super.onStart();
Log.w(TAG, "-------------- onStart");
if (((MasterTabActivity) getActivity()).getCallDetailsDictionary() != null) {
refreshData();
}
}
现在,只要该视图返回视图,它将调用
onStart
并刷新其数据。关于java - FragmentPagerAdapter在重新选择时返回半断开 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49493759/