Skip to content

Commit

Permalink
Fix a memory leak.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhpanvip committed Dec 9, 2020
1 parent e4e301c commit d035769
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import android.util.SparseArray
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter

import com.example.zhpan.banner.fragment.BaseFragment
import com.example.zhpan.banner.fragment.HomeFragment
import com.example.zhpan.banner.fragment.IndicatorFragment
import com.example.zhpan.banner.fragment.OthersFragment
import com.example.zhpan.banner.fragment.PageFragment
import com.example.zhpan.banner.fragment.*

/**
* <pre>
Expand Down Expand Up @@ -60,7 +55,7 @@ class AdapterFragmentPager(fragmentActivity: FragmentActivity) : FragmentStateAd
}
else -> {
if (fragments.get(PAGE_HOME) == null) {
fragment = HomeFragment.getInstance();
fragment = EmptyFragment.getInstance();
fragments.put(PAGE_HOME, fragment)
} else {
fragment = fragments.get(PAGE_HOME)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.zhpan.banner.fragment;

import android.os.Bundle;
import android.view.View;

import com.example.zhpan.banner.R;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* @author zhangpan
* @date 2020/12/9
*/
public class EmptyFragment extends BaseFragment {

@Override
protected int getLayout() {
return R.layout.fragment_empty;
}

@Override
protected void initTitle() {

}

@Override
protected void initView(@Nullable Bundle savedInstanceState, @NotNull View view) {

}

public static EmptyFragment getInstance() {
return new EmptyFragment();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ private void initRecyclerView(View view) {
DividerItemDecoration.VERTICAL));
articleAdapter = new ArticleAdapter(getActivity(), new ArrayList<>());
recyclerView.setAdapter(articleAdapter);
recyclerView.getHeadAndFootAdapter();
}

private void initRefreshLayout(View view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,42 +83,28 @@ private void initRadioGroup() {
mRadioGroupStyle.setOnCheckedChangeListener((group, checkedId) -> checkedChange(mCheckId = checkedId));
radioButton.performClick();
mRadioGroupMode.setOnCheckedChangeListener((group, checkedId) -> {
switch (checkedId) {
case R.id.rb_normal:
mSlideMode = IndicatorSlideMode.NORMAL;
break;
case R.id.rb_worm:
mSlideMode = IndicatorSlideMode.WORM;
break;
case R.id.rb_smooth:
mSlideMode = IndicatorSlideMode.SMOOTH;
break;
case R.id.rb_color:
mSlideMode = IndicatorSlideMode.COLOR;
break;
case R.id.rb_scale:
mSlideMode = IndicatorSlideMode.SCALE;
break;
default:
break;
if (checkedId == R.id.rb_normal) {
mSlideMode = IndicatorSlideMode.NORMAL;
} else if (checkedId == R.id.rb_worm) {
mSlideMode = IndicatorSlideMode.WORM;
} else if (checkedId == R.id.rb_smooth) {
mSlideMode = IndicatorSlideMode.SMOOTH;
} else if (checkedId == R.id.rb_color) {
mSlideMode = IndicatorSlideMode.COLOR;
} else if (checkedId == R.id.rb_scale) {
mSlideMode = IndicatorSlideMode.SCALE;
}
checkedChange(mCheckId);
});
}

private void checkedChange(int checkedId) {
switch (checkedId) {
case R.id.rb_circle:
setupCircleIndicator();
break;
case R.id.rb_dash:
setupDashIndicator();
break;
case R.id.rb_round_rect:
setupRoundRectIndicator();
break;
default:
break;
if (checkedId == R.id.rb_circle) {
setupCircleIndicator();
} else if (checkedId == R.id.rb_dash) {
setupDashIndicator();
} else if (checkedId == R.id.rb_round_rect) {
setupRoundRectIndicator();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,14 @@ protected void initView(Bundle savedInstanceState, @NonNull View view) {
private void initRadioGroup() {
radioGroupStyle.setVisibility(View.VISIBLE);
radioGroupStyle.setOnCheckedChangeListener((group, checkedId) -> {
switch (checkedId) {
case R.id.rb_indicator_below:
setIndicatorBelowOfBanner();
break;
case R.id.rb_dash:
setupCustomIndicator();
break;
case R.id.rb_drawable:
setDrawableIndicator(getDrawableIndicator());
break;
case R.id.rb_vector_drawable:
setDrawableIndicator(getVectorDrawableIndicator());
break;
default:
break;
if (checkedId == R.id.rb_indicator_below) {
setIndicatorBelowOfBanner();
} else if (checkedId == R.id.rb_dash) {
setupCustomIndicator();
} else if (checkedId == R.id.rb_drawable) {
setDrawableIndicator(getDrawableIndicator());
} else if (checkedId == R.id.rb_vector_drawable) {
setDrawableIndicator(getVectorDrawableIndicator());
}
});
radioButton.performClick();
Expand Down Expand Up @@ -173,21 +166,16 @@ private void updateData() {

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_add_data:
addData();
break;
case R.id.tv_remove_item:
removeItem();
break;
case R.id.tv_insert_item:
insertItem();
break;
default:
int position = new Random().nextInt(5);
mViewPager.setCurrentItem(position, true);
ToastUtils.showShort("Jump to position:" + position);
break;
if (view.getId() == R.id.tv_add_data) {
addData();
} else if (view.getId() == R.id.tv_remove_item) {
removeItem();
} else if (view.getId() == R.id.tv_insert_item) {
insertItem();
} else {
int position = new Random().nextInt(5);
mViewPager.setCurrentItem(position, true);
ToastUtils.showShort("Jump to position:" + position);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,18 @@ private void pageClick(View view, int position) {

private void initRadioGroup() {
mRadioGroupPageStyle.setOnCheckedChangeListener((group, checkedId) -> {
switch (checkedId) {
case R.id.rb_multi_page:
setupMultiPageBanner();
break;
case R.id.rb_right_page_reveal:
setupRightPageReveal();
break;
case R.id.rb_multi_page_scale:
setupBanner(PageStyle.MULTI_PAGE_SCALE);
break;
case R.id.rb_multi_page_overlap:
setupBanner(PageStyle.MULTI_PAGE_OVERLAP);
break;
case R.id.rb_qq_music_style:
setQQMusicStyle();
break;
case R.id.rb_netease_music_style:
setNetEaseMusicStyle();
break;
default:
break;
if (checkedId == R.id.rb_multi_page) {
setupMultiPageBanner();
} else if (checkedId == R.id.rb_right_page_reveal) {
setupRightPageReveal();
} else if (checkedId == R.id.rb_multi_page_scale) {
setupBanner(PageStyle.MULTI_PAGE_SCALE);
} else if (checkedId == R.id.rb_multi_page_overlap) {
setupBanner(PageStyle.MULTI_PAGE_OVERLAP);
} else if (checkedId == R.id.rb_netease_music_style) {
setNetEaseMusicStyle();
} else if (checkedId == R.id.rb_qq_music_style) {
setQQMusicStyle();
}
});
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/fragment_empty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -994,14 +994,19 @@ public BannerViewPager<T, VH> setLifecycleRegistry(Lifecycle lifecycleRegistry)

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
stopLoop();
stopLoop();
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
startLoop();
}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
stopLoop();
}

/**
* 设置是否允许BVP对事件进行拦截,用于解决CoordinatorLayout+CollapsingToolbarLayout
* 在嵌套BVP时引起的滑动冲突问题。
Expand Down

0 comments on commit d035769

Please sign in to comment.