Skip to content

Commit

Permalink
添加 demo
Browse files Browse the repository at this point in the history
  • Loading branch information
wenchaosong committed Apr 18, 2019
1 parent 1735001 commit 29f95fa
Show file tree
Hide file tree
Showing 16 changed files with 445 additions and 137 deletions.
26 changes: 11 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ repositories {
}
dependencies{
implementation 'com.github.wenchaosong:Banner:2.3.8'
implementation 'com.github.wenchaosong:Banner:2.3.9'
// 或者
implementation 'com.ms:banner:latest'
}
Expand All @@ -68,12 +68,7 @@ compile project(':rollbanner')

#### Step 3.设置布局
```
.setPages(list, new HolderCreator<BannerViewHolder>() {
@Override
public BannerViewHolder createViewHolder() {
return new CustomViewHolder();
}
})
.setPages(arrList, new CustomViewHolder())
class CustomViewHolder implements BannerViewHolder<String> {
Expand Down Expand Up @@ -108,12 +103,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
Banner banner = (Banner) findViewById(R.id.banner);
banner.setAutoPlay(true)
.setPages(list, new HolderCreator<BannerViewHolder>() {
@Override
public BannerViewHolder createViewHolder() {
return new CustomViewHolder();
}
})
.setPages(arrList, new CustomViewHolder())
.start();
}
```
Expand All @@ -124,14 +114,18 @@ protected void onCreate(Bundle savedInstanceState) {
protected void onStart() {
super.onStart();
//开始轮播
banner.startAutoPlay();
if (banner != null && !banner.isStart() && banner.isPrepare()) {
banner.startAutoPlay();
}
}
@Override
protected void onStop() {
super.onStop();
//结束轮播
banner.stopAutoPlay();
if (banner != null && banner.isStart() && banner.isPrepare()) {
banner.stopAutoPlay();
}
}
```

Expand Down Expand Up @@ -184,6 +178,8 @@ setCurrentPage 设置当前页
update 刷新
updateBannerStyle 刷新样式
start 开始使用
isPrepare 是否加载完成
isStart 是否轮播中
setIndicatorRes 设置指示器资源
startAutoPlay 开始自动轮播
stopAutoPlay 停止轮播
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ dependencies {
implementation 'com.github.bumptech.glide:glide:4.0.0'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.github.wenchaosong:BottomBar:3.0.7'
implementation project(':banner')
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<activity android:name=".demo.IndicatorPositionActivity" />
<activity android:name=".demo.CustomBannerActivity" />
<activity android:name=".demo.BannerLocalActivity" />
<activity android:name=".demo.SimpleFragmentActivity" />
<activity
android:name=".demo.CustomViewPagerActivity"
android:screenOrientation="portrait" />
Expand Down
97 changes: 97 additions & 0 deletions app/src/main/java/com/test/LazyFragment.java
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();
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/test/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.test.demo.CustomBannerActivity;
import com.test.demo.CustomViewPagerActivity;
import com.test.demo.IndicatorPositionActivity;
import com.test.demo.SimpleFragmentActivity;
import com.test.ui.CustomViewHolder;

import java.util.ArrayList;
Expand Down Expand Up @@ -124,6 +125,9 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
case 6:
startActivity(new Intent(this, CustomViewPagerActivity.class));
break;
case 7:
startActivity(new Intent(this, SimpleFragmentActivity.class));
break;
}
}

Expand Down
61 changes: 61 additions & 0 deletions app/src/main/java/com/test/demo/HomeFragment.java
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();
}
}
}
54 changes: 54 additions & 0 deletions app/src/main/java/com/test/demo/MoreFragment.java
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 app/src/main/java/com/test/demo/SimpleFragmentActivity.java
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);
}
}
}
Loading

0 comments on commit 29f95fa

Please sign in to comment.