-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1735001
commit 29f95fa
Showing
16 changed files
with
445 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.test; | ||
|
||
import android.os.Bundle; | ||
import android.support.v4.app.Fragment; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
public abstract class LazyFragment extends Fragment { | ||
|
||
/** | ||
* 是否可见状态 | ||
*/ | ||
private boolean isVisible; | ||
/** | ||
* 标志位,View已经初始化完成。 | ||
*/ | ||
private boolean isPrepared; | ||
/** | ||
* 是否第一次加载 | ||
*/ | ||
private boolean isFirstLoad = true; | ||
private boolean isLoad = false; | ||
|
||
@Override | ||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ||
isFirstLoad = true; | ||
View view = initViews(inflater, container, savedInstanceState); | ||
isPrepared = true; | ||
lazyLoad(); | ||
return view; | ||
} | ||
|
||
/** | ||
* 如果是与ViewPager一起使用,调用的是setUserVisibleHint | ||
* | ||
* @param isVisibleToUser 是否显示出来了 | ||
*/ | ||
@Override | ||
public void setUserVisibleHint(boolean isVisibleToUser) { | ||
super.setUserVisibleHint(isVisibleToUser); | ||
if (getUserVisibleHint()) { | ||
isVisible = true; | ||
if (isPrepared) { | ||
onFragmentVisible(); | ||
} | ||
isLoad = true; | ||
lazyLoad(); | ||
} else { | ||
isVisible = false; | ||
onFragmentUnVisible(); | ||
isLoad = false; | ||
} | ||
} | ||
|
||
@Override | ||
public void onResume() { | ||
super.onResume(); | ||
|
||
if (isLoad) { | ||
onFragmentVisible(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onPause() { | ||
super.onPause(); | ||
|
||
if (isLoad) { | ||
onFragmentUnVisible(); | ||
} | ||
} | ||
|
||
/** | ||
* 要实现延迟加载Fragment内容,需要在 onCreateView | ||
* isPrepared = true; | ||
*/ | ||
private void lazyLoad() { | ||
if (!isPrepared || !isVisible || !isFirstLoad) { | ||
return; | ||
} | ||
isFirstLoad = false; | ||
initData(); | ||
} | ||
|
||
protected void onFragmentVisible() { | ||
|
||
} | ||
|
||
protected void onFragmentUnVisible() { | ||
|
||
} | ||
|
||
protected abstract View initViews(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState); | ||
|
||
protected abstract void initData(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.test.demo; | ||
|
||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import com.ms.banner.Banner; | ||
import com.test.LazyFragment; | ||
import com.test.R; | ||
import com.test.ui.CustomViewHolder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class HomeFragment extends LazyFragment { | ||
|
||
private Banner mBanner; | ||
|
||
@Override | ||
protected View initViews(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ||
View view = inflater.inflate(R.layout.fragment_home, null); | ||
init(view); | ||
return view; | ||
} | ||
|
||
private void init(View view) { | ||
mBanner = view.findViewById(R.id.banner); | ||
view.findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
startActivity(new Intent(getActivity(), BannerLocalActivity.class)); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected void initData() { | ||
String[] urls = getResources().getStringArray(R.array.url2); | ||
List arrList = new ArrayList(Arrays.asList(urls)); | ||
mBanner.setAutoPlay(true) | ||
.setPages(arrList, new CustomViewHolder()) | ||
.start(); | ||
} | ||
|
||
@Override | ||
protected void onFragmentVisible() { | ||
if (mBanner != null && !mBanner.isStart() && mBanner.isPrepare()) { | ||
mBanner.startAutoPlay(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onFragmentUnVisible() { | ||
if (mBanner != null && mBanner.isStart() && mBanner.isPrepare()) { | ||
mBanner.stopAutoPlay(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.test.demo; | ||
|
||
import android.os.Bundle; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import com.ms.banner.Banner; | ||
import com.test.LazyFragment; | ||
import com.test.R; | ||
import com.test.ui.CustomViewHolder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class MoreFragment extends LazyFragment { | ||
|
||
private Banner mBanner; | ||
|
||
@Override | ||
protected View initViews(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ||
View view = inflater.inflate(R.layout.fragment_more, null); | ||
init(view); | ||
return view; | ||
} | ||
|
||
private void init(View view) { | ||
mBanner = view.findViewById(R.id.banner); | ||
} | ||
|
||
@Override | ||
protected void initData() { | ||
String[] urls = getResources().getStringArray(R.array.url2); | ||
List arrList = new ArrayList(Arrays.asList(urls)); | ||
mBanner.setAutoPlay(true) | ||
.setPages(arrList, new CustomViewHolder()) | ||
.start(); | ||
} | ||
|
||
@Override | ||
protected void onFragmentVisible() { | ||
if (mBanner != null && !mBanner.isStart() && mBanner.isPrepare()) { | ||
mBanner.startAutoPlay(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onFragmentUnVisible() { | ||
if (mBanner != null && mBanner.isStart() && mBanner.isPrepare()) { | ||
mBanner.stopAutoPlay(); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
app/src/main/java/com/test/demo/SimpleFragmentActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.test.demo; | ||
|
||
import android.os.Bundle; | ||
import android.support.v4.app.Fragment; | ||
import android.support.v4.app.FragmentManager; | ||
import android.support.v4.app.FragmentPagerAdapter; | ||
import android.support.v4.view.ViewPager; | ||
import android.support.v7.app.AppCompatActivity; | ||
|
||
import com.bottom.PageNavigationView; | ||
import com.test.LazyFragment; | ||
import com.test.R; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class SimpleFragmentActivity extends AppCompatActivity { | ||
|
||
private String[] mTitles = {"首页", "更多"}; | ||
private ArrayList<LazyFragment> mFragments = new ArrayList<>(); | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_fragment); | ||
|
||
ViewPager viewPager = findViewById(R.id.vp); | ||
PageNavigationView bottomTabLayout = findViewById(R.id.tab); | ||
PageNavigationView.MaterialBuilder material = bottomTabLayout.material(); | ||
for (String title : mTitles) { | ||
material.addItem(android.R.drawable.ic_menu_more, title); | ||
} | ||
material.build().setupWithViewPager(viewPager); | ||
|
||
mFragments.add(new HomeFragment()); | ||
mFragments.add(new MoreFragment()); | ||
|
||
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager())); | ||
} | ||
|
||
private class MyPagerAdapter extends FragmentPagerAdapter { | ||
|
||
public MyPagerAdapter(FragmentManager fm) { | ||
super(fm); | ||
} | ||
|
||
@Override | ||
public int getCount() { | ||
return mFragments.size(); | ||
} | ||
|
||
@Override | ||
public CharSequence getPageTitle(int position) { | ||
return mTitles[position]; | ||
} | ||
|
||
@Override | ||
public Fragment getItem(int position) { | ||
return mFragments.get(position); | ||
} | ||
} | ||
} |
Oops, something went wrong.