FragmentPagerAdapter returns Reselect 上的视图半中断
FragmentPagerAdapter returns semi broke views on Reselect
我有一个标签 Activity,其中有 x
个标签。所有选项卡在第一次加载时正确加载。如果我离开选项卡 x+2
个选项卡然后返回,则会丢失一些数据和元素。
我使用了 Android Studio 自己的 Tabbed Activity
生成的模板,以及选项卡的片段模板。
我已经查看了 SO 和其他几个,但它们似乎并不完全符合我的模型。或者,即使他们看到了,我也没有看到。
有什么帮助吗?
这是我的 Master Tabbed Activity 和一些相关的导入。
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
个制表符时,将调用片段 onPause
方法(以及其他拆解方法)。检查片段的所有阶段的 lifecycle documents。
当您 return 到选项卡时,onCreateView
方法(以及其他构建方法禁止 1onAttach1 和 onCreate
。在我上面的示例中,片段查询 Activity 在 onCreate
.
所以我创建了一个 refreshData 方法:
private void refreshData() {
someTextElement.setText(((MasterTabActivity) getActivity()).getCallDetailsDictionary().getString("CALdCallTaken").getString("SomeKey"));
}
并将对 refreshData
的调用移至 onStart
,首先检查 activity 中的数据是否为空。
@Override
public void onStart() {
super.onStart();
Log.w(TAG, "-------------- onStart");
if (((MasterTabActivity) getActivity()).getCallDetailsDictionary() != null) {
refreshData();
}
}
现在,每当该视图 return 进入视图时,它都会调用 onStart
并刷新其数据。
我有一个标签 Activity,其中有 x
个标签。所有选项卡在第一次加载时正确加载。如果我离开选项卡 x+2
个选项卡然后返回,则会丢失一些数据和元素。
我使用了 Android Studio 自己的 Tabbed Activity
生成的模板,以及选项卡的片段模板。
我已经查看了 SO 和其他几个,但它们似乎并不完全符合我的模型。或者,即使他们看到了,我也没有看到。
有什么帮助吗?
这是我的 Master Tabbed Activity 和一些相关的导入。
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
个制表符时,将调用片段 onPause
方法(以及其他拆解方法)。检查片段的所有阶段的 lifecycle documents。
当您 return 到选项卡时,onCreateView
方法(以及其他构建方法禁止 1onAttach1 和 onCreate
。在我上面的示例中,片段查询 Activity 在 onCreate
.
所以我创建了一个 refreshData 方法:
private void refreshData() {
someTextElement.setText(((MasterTabActivity) getActivity()).getCallDetailsDictionary().getString("CALdCallTaken").getString("SomeKey"));
}
并将对 refreshData
的调用移至 onStart
,首先检查 activity 中的数据是否为空。
@Override
public void onStart() {
super.onStart();
Log.w(TAG, "-------------- onStart");
if (((MasterTabActivity) getActivity()).getCallDetailsDictionary() != null) {
refreshData();
}
}
现在,每当该视图 return 进入视图时,它都会调用 onStart
并刷新其数据。