我有HostActivity
使用ViewPager
和TabLayout
在多个片段之间切换。当我在选项卡之间切换时,Fragments实例会获取更新的数据。我还在onCreateView
实例的Fragment
中看到了更新的数据,但TextView.setText
未更新。当我检查Fragment
的可见性时,它始终显示Invisible。切换选项卡时如何使片段可见,以便使用新数据更新视图?片段/活动生命周期中是否缺少某些内容?我是第一次实施ViewPager
因此,了解我是否遗漏了某些内容会很有帮助。
片段类:
public class StepFragment extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getArguments() != null) {
step = getArguments().getParcelable(SELECTED_STEP);
mDescription = step.getDescription();
}
View view = inflater.inflate(R.layout.step_fragment, container, false);
ButterKnife.bind(this,view);
Log.e(TAG, "onCreateView: "+mDescription); **// THIS GETS UPDATED DATA**
tvStepDescription.setText(mDescription);
}
return view;
}
}
这是我的主持人活动:
public class StepActivity extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_step);
fragmentSelectAdapter = new StepFragmentSelectAdapter(getSupportFragmentManager(),this,steps,recipe);
mViewPager.setAdapter(fragmentSelectAdapter);
mTabLayout.setupWithViewPager(mViewPager);
stepFragment = (StepFragment) getSupportFragmentManager().findFragmentById(R.id.step_container);
if(stepFragment == null) {
stepFragment = StepFragment.newInstance(step, recipe);
stepFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.add(R.id.step_container, stepFragment)
.commit();
} else {
stepFragment = StepFragment.newInstance(step, recipe);
stepFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.step_container, stepFragment)
.commit();
}
}
}
这是我的FragmentPagerAdapter
,它似乎是根据getItem方法中的标签位置获取正确的数据:
public class StepFragmentSelectAdapter extends FragmentPagerAdapter {
...
@Override
public Fragment getItem(int position) {
**// THIS GETS UPDATED DATA**
Log.e(TAG, "getItem: \nDecr: "+steps.get(position).getDescription()+"\nVideo: "+steps.get(position).getVideoURL()+"\nImage: "+steps.get(position).getThumbnailURL());
return StepFragment.newInstance(steps.get(position),recipe);
}
@Override
public int getCount() {
if (steps == null){
return 0;
}
return steps.size();
}
...
}
答案 0 :(得分:0)
据我所知,你在那里遇到的问题,我认为你应该在你的onResume
中实现一个StepFragment
函数,它将从某些来源获取更新的数据并显示这个在TextView
。但是,我可以想到StepFragmentSelectAdapter
中的潜在问题。每次切换选项卡时都在创建新实例。
您之前应创建Fragment
个实例,如果您要在片段之间传递数据,则可以考虑通过实现BroadcasReceiver
来实现interface
或侦听器功能。
所以PagerAdapter
看起来应该是这样的。
public class StepFragmentSelectAdapter extends FragmentPagerAdapter {
public ArrayList<StepFragment> stepFragments;
public StepFragmentSelectAdapter(ArrayList<Step> steps) {
stepFragments = new ArrayList<>();
for(int i = 0; i < steps.size(); i++) {
stepFragments.add(StepFragment.newInstance(steps.get(position),recipe));
}
}
...
@Override
public Fragment getItem(int position) {
**// THIS GETS UPDATED DATA**
Log.e(TAG, "getItem: \nDecr: "+steps.get(position).getDescription()+"\nVideo: "+steps.get(position).getVideoURL()+"\nImage: "+steps.get(position).getThumbnailURL());
return stepFragments.get(position);
}
@Override
public int getCount() {
if (steps == null){
return 0;
}
return steps.size();
}
...
}
答案 1 :(得分:0)
感谢您的提示。我通过将ActionBar
替换为带有ToolBar
的自定义ImageButton
并使用单击侦听器返回到调用活动来解决此问题。
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getActivity().onBackPressed();
}
});