如何处理片段内的网络呼叫 [英] How to handle network calls inside a Fragment
问题描述
我有以下情况:
我有一个托管了 ViewPager
的 Activity
,并且我有4个 Fragments
;
I have an Activity
that hosts a ViewPager
, and I have 4 Fragments
;
开头的 ViewPager
包含片段A ,
当用户在 ViewPager
片段B 上滑动时,进入 ViewPager
,然后依次是 Fragment C 和片段D ...等等...
when the user swipes on the ViewPager
Fragment B goes into the ViewPager
, then Fragment C and Fragment D ...etc...
现在,实例化 FragmentPagerAdapter
时,至少会创建至少两个Fragments.
Now as soon as the FragmentPagerAdapter
is instantiated at least 2 of the Fragments are created.
这带来了2个问题:
- 每个
Fragment
都需要执行网络呼叫,但是我不想做不必要的呼叫(如果用户从不刷卡到Fragment,我不想对Fragment B进行网络呼叫.B ); - 类似于1.),当一个Fragment执行网络调用时,我需要显示一个
ProgessDialog
,但是如果用户从未去过它,我不想显示来自Fragment B的对话框...
- Every
Fragment
needs to perform network calls, but I do not want to do unnecessary ones (I do not want to make network calls for Fragment B, if the user never swipes to Fragment B ); - similar to 1.), I need to show a
ProgessDialog
when a Fragment perform network calls, but I do not want to show dialogs from Fragment B if the user never goes to it...
在这种情况下,我应该使用哪种模式?
Please what kind of pattern should I use in such a circumstance?
活动
public class PagerActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_layout);
ViewPager pager=(ViewPager)findViewById(R.id.pager);
TabPageIndicator tabs=(TabPageIndicator)findViewById(R.id.titles);
pager.setAdapter(buildAdapter());
tabs.setViewPager(pager);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
FragmentPagerAdapter
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
@Override
public int getCount() {
return (4);
}
@Override
public Fragment getItem(int position) {
if (position == 1) {
if (dashbardFragment == null)
dashbardFragment = DashBoardFragment.newInstance(position);
return dashbardFragment;
}
if (position == 0) {
if (listOfParticipantFragment == null)
listOfParticipantFragment = ListOfParicipantsFragment
.newInstance(position);
return listOfParticipantFragment;
}
}
1个片段
public class ListOfParicipantsFragment extends Fragment {
public static ListOfParicipantsFragment newInstance(int position) {
ListOfParicipantsFragment frag = new ListOfParicipantsFragment();
return (frag);
}
public static String getTitle(Context ctxt, int position) {
return myApplication.getContext().getResources().getString(R.string.list_of_participants_fragment_title);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View result = inflater.inflate(R.layout.guest_list_fragment_layout,
container, false);
return (result);
}
推荐答案
在下面的每个 fragment
override
方法中尝试此操作,并在可见时调用函数:
Try this, in each fragment
override
below method and call your function when it is visible:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisible()){
if(isVisibleToUser){
Log.d("MyTag","My Fragment is visible");
}else{
Log.d("MyTag","My Fragment is not visible");
}
}
}
编辑
注意:仅当使用 FragmentPagerAdapter
或 FragmentStatePagerAdapter
这篇关于如何处理片段内的网络呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!