Refresh ViewPager, restrict preload, cache all, and cache viewpager
[Framework ]:
Public part: Left menu, TitleBar, and RadioGroup (three RadioButton: X, Y, and Z)
Select X page: Indicator + ViewPager
[Effects to be achieved ]:
(1) Select A on the left side to go to the X page. On the X1 online refresh page, disable X2 pre-loading. Move to the X2 page to refresh X2 online-> X3-> X4;
(2) When sliding from X4 to X3, X2, and X1, you do not need to refresh X1, X2, and X3 again. If you stay on the X2 page;
(3) Click "B" on the left, or click "Y", then "B", and then "X". At this time, the X2 network is refreshed;
(4) The X2 page slides to the X1, X3, and X4 pages. The X1, X3, and X4 are refreshed and cached again.
[Several Problems to be Solved and their description ]:
Solution:
(1) Fragment refresh of ViewPager
(2) Disable pre-loading of ViewPager
(3) cache after loading, unless requests are made online again
Note:
Because BaseFragment and LoadingPage are extracted, the code structure is a bit complicated. Here we only extract the Code related to this article.
[1] refresh questions
InitData initialization:
FManager = getChildFragmentManager ();
Adapter = new MyViewPagerAdapter (fManager, fenleiFragments );
VpSearch. setAdapter (adapter );
VpSearch. setOffscreenPageLimit (fenleiFragments. size ()-1); // set cache all
FragmentPagerAdapter:
Class MyViewPagerAdapter extends FragmentPagerAdapter {
Private ArrayList <Fragment> fragments;
Private FragmentManager manager;
Private int mChildCount = 0;
Public MyViewPagerAdapter (FragmentManager fm, ArrayList <Fragment> fenleiFragments ){
Super (fm );
This. manager = fm;
This. fragments = fenleiFragments;
}
@ Override
Public void notifyDataSetChanged (){
MChildCount = getCount ();
Super. notifyDataSetChanged ();
}
@ Override
Public Fragment getItem (int position ){
Return fenleiFragments. get (position );
}
@ Override
Public int getCount (){
Return fenleiFragments = null? 0: fenleiFragments. size ();
}
@ Override
Public CharSequence getPageTitle (int position ){
Return fenlei_names.get (position );
}
@ Override
Public void destroyItem (ViewGroup container, int position, Object object Object ){
Super. destroyItem (container, position, object );
}
@ Override
Public int getItemPosition (Object object ){
// Method 1:
If (mChildCount> 0 ){
MChildCount --;
Return POSITION_NONE;
}
Return super. getItemPosition (object );
// Method 2:
// Return POSITION_NONE;
/* Remarks: method 1 and method 2 have the same effect when there are only three Fragment entries. If there are more than three Fragment entries, they are not verified */
}
}
Receive broadcast refresh
Choose "change" from the left menu, choose "change" from the refresh menu, and so on. Here we will use the broadcast method to notify you to refresh.
// Register as the broadcast Receiver
Private LocalBroadcastManager lbm;
Private BroadcastReceiver selectChangedReceiver = new BroadcastReceiver (){
@ Override
Public void onReceive (Context context, Intent intent ){
// Update page (adapter)
/*----------------------------------------------------------------*/
If (vpSearch. getAdapter ()! = Null ){
FragmentManager cfm = getChildFragmentManager ();
FragmentTransaction ft = cfm. beginTransaction ();
List <Fragment> fragments = cfm. getFragments ();
If (fragments! = Null & fragments. size ()> 0 ){
For (int I = 0; I <fragments. size (); I ++ ){
Ft. remove (fragments. get (I ));
}
}
Ft. commit ();
}
FenleiFragments. clear ();
FenleiFragments. add (new CategoryFragment (Urls. getFenleiUrl (0 )));
FenleiFragments. add (new BrandFragment (Urls. getFenleiUrl (1 )));
FenleiFragments. add (new DongtaiFragment (Urls. getFenleiUrl (2 )));
Adapter. notifyDataSetChanged ();
/*----------------------------------------------------------------*/
}
};
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Register the broadcast Receiver
Lbm = LocalBroadcastManager. getInstance (getActivity ());
Lbm. registerReceiver (selectChangedReceiver, new IntentFilter (BroadcastVar. SELECT_CHANGED ));
}
@ Override
Public void onDestroy (){
Super. onDestroy ();
If (lbm! = Null ){
Lbm. unregisterReceiver (selectChangedReceiver );
SelectChangedReceiver = null;
}
}
[2] canceling ViewPager pre-loading
Modify BaseFragment
Add 2 Properties
Private int isLoad = 0; // whether it has been loaded
Private boolean isVisable; // whether it is visible
Modify the getMyUrl () method of updateLoadingPage
Public String getMyUrl (){
If (TextUtils. isEmpty (getUrl () {// you do not need to connect to the Internet.
IsLoad = 1;
Return getUrl ();
} Else {
If (isVisable) {// networking required, visible
IsLoad = 2;
Return getUrl ();
} Else {// networking required, invisible
IsLoad = 3;
Return null;
}
}
}
Override the setUserVisibleHint Method
Public void setUserVisibleHint (boolean isVisibleToUser ){
Super. setUserVisibleHint (isVisibleToUser );
IsVisable = isVisibleToUser;
If (isVisibleToUser & isLoad = 3 ){
// At this time, loadingPage must not be null, because isLoad = 3 indicates that it has been initialized
LoadingPage. show ();
}
}
Handle the json parsing of initData
JSONObject jsonObject = JSON. parseObject (content );
If (jsonObject = null) {// to prevent the url from being manually set to null during ViewPager pre-loading, check
Return;
}
[3] cache all Fragment
VpSearch. setOffscreenPageLimit (fenleiFragments. size ()-1 );
In addition, FragmentPagerAdapter must follow the above method.