Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Relex committed Jul 31, 2019
1 parent 504f265 commit 010e850
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 36 deletions.
2 changes: 0 additions & 2 deletions LoopingViewPager/LoopingViewPager.iml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/check_manifest_result" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/compile_library_classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/compile_only_not_namespaced_r_class_jar" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/consumer_proguard_file" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/intermediate-jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javac" />
Expand All @@ -103,7 +102,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/library_java_res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/library_manifest" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint_jar" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint_publish_jar" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_jni_libs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_shaders" />
Expand Down
4 changes: 2 additions & 2 deletions circleindicator/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
apply plugin: 'com.android.library'

version = "1.3.0"
version = "1.3.1"

android {
compileSdkVersion 28

defaultConfig {
minSdkVersion 14
targetSdkVersion 28
versionCode 130
versionCode 131
versionName version
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
Expand Down Expand Up @@ -148,46 +147,65 @@ protected Animator createAnimatorIn(Config config) {
}

public void createIndicators(int count, int currentPosition) {
int orientation = getOrientation();
if (mImmediateAnimatorOut.isRunning()) {
mImmediateAnimatorOut.end();
mImmediateAnimatorOut.cancel();
}

if (mImmediateAnimatorIn.isRunning()) {
mImmediateAnimatorIn.end();
mImmediateAnimatorIn.cancel();
}

// Diff View
int childViewCount = getChildCount();
if (count < childViewCount) {
removeViews(count, childViewCount - count);
} else if (count > childViewCount) {
int addCount = count - childViewCount;
int orientation = getOrientation();
for (int i = 0; i < addCount; i++) {
addIndicator(orientation);
}
}

// Bind Style
View indicator;
for (int i = 0; i < count; i++) {
indicator = getChildAt(i);
if (currentPosition == i) {
indicator =
addIndicator(orientation, mIndicatorBackgroundResId, mImmediateAnimatorOut);
indicator.setBackgroundResource(mIndicatorBackgroundResId);
mImmediateAnimatorOut.setTarget(indicator);
mImmediateAnimatorOut.start();
mImmediateAnimatorOut.end();
} else {
indicator = addIndicator(orientation, mIndicatorUnselectedBackgroundResId,
mImmediateAnimatorIn);
indicator.setBackgroundResource(mIndicatorUnselectedBackgroundResId);
mImmediateAnimatorIn.setTarget(indicator);
mImmediateAnimatorIn.start();
mImmediateAnimatorIn.end();
}

if (mIndicatorCreatedListener != null) {
mIndicatorCreatedListener.onIndicatorCreated(indicator, i);
}
}

mLastPosition = currentPosition;
}

protected View addIndicator(int orientation, @DrawableRes int backgroundDrawableId,
Animator animator) {
if (animator.isRunning()) {
animator.end();
animator.cancel();
}
protected void addIndicator(int orientation) {
View indicator = new View(getContext());
indicator.setBackgroundResource(backgroundDrawableId);
addView(indicator, mIndicatorWidth, mIndicatorHeight);
LayoutParams lp = (LayoutParams) indicator.getLayoutParams();

final LayoutParams params = generateDefaultLayoutParams();
params.width = mIndicatorWidth;
params.height = mIndicatorHeight;
if (orientation == HORIZONTAL) {
lp.leftMargin = mIndicatorMargin;
lp.rightMargin = mIndicatorMargin;
params.leftMargin = mIndicatorMargin;
params.rightMargin = mIndicatorMargin;
} else {
lp.topMargin = mIndicatorMargin;
lp.bottomMargin = mIndicatorMargin;
params.topMargin = mIndicatorMargin;
params.bottomMargin = mIndicatorMargin;
}

indicator.setLayoutParams(lp);
animator.setTarget(indicator);
animator.start();

return indicator;
addView(indicator, params);
}

public void animatePageSelected(int position) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ public void setViewPager(@Nullable ViewPager viewPager) {
}

private void createIndicators() {
removeAllViews();
PagerAdapter adapter = mViewpager.getAdapter();
int count;
if (adapter == null || (count = adapter.getCount()) <= 0) {
return;
if (adapter == null) {
count = 0;
} else {
count = adapter.getCount();
}
createIndicators(count, mViewpager.getCurrentItem());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ public void attachToRecyclerView(@NonNull RecyclerView recyclerView,
}

private void createIndicators() {
removeAllViews();
RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
int count;
if (adapter == null || (count = adapter.getItemCount()) <= 0) {
return;
if (adapter == null) {
count = 0;
} else {
count = adapter.getItemCount();
}

createIndicators(count, getSnapPosition(mRecyclerView.getLayoutManager()));
}

Expand Down

0 comments on commit 010e850

Please sign in to comment.