Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding custom last indicator functionality #130

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class BaseCircleIndicator extends LinearLayout {
protected int mIndicatorBackgroundResId;
protected int mIndicatorUnselectedBackgroundResId;

protected int mLastIndicatorBackgroundResId;
protected int mLastIndicatorUnselectedBackgroundResId;

protected Animator mAnimatorOut;
protected Animator mAnimatorIn;
protected Animator mImmediateAnimatorOut;
Expand All @@ -51,7 +54,7 @@ public BaseCircleIndicator(Context context, AttributeSet attrs, int defStyleAttr

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public BaseCircleIndicator(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
Expand Down Expand Up @@ -90,6 +93,8 @@ private Config handleTypedArray(Context context, AttributeSet attrs) {
config.backgroundResId);
config.orientation = typedArray.getInt(R.styleable.BaseCircleIndicator_ci_orientation, -1);
config.gravity = typedArray.getInt(R.styleable.BaseCircleIndicator_ci_gravity, -1);
config.backgroundLastResID = typedArray.getResourceId(R.styleable.BaseCircleIndicator_ci_drawable_last, 0);
config.unselectedBackgroundLastId = typedArray.getResourceId(R.styleable.BaseCircleIndicator_ci_drawable_last_unselected, 0);
typedArray.recycle();

return config;
Expand All @@ -115,6 +120,12 @@ public void initialize(Config config) {
mIndicatorUnselectedBackgroundResId =
(config.unselectedBackgroundId == 0) ? config.backgroundResId
: config.unselectedBackgroundId;
mLastIndicatorBackgroundResId = (config.backgroundLastResID == 0) ? config.backgroundResId : config.backgroundLastResID;
mLastIndicatorUnselectedBackgroundResId =
(config.unselectedBackgroundLastId == 0) ?
(config.unselectedBackgroundId == 0) ?
config.backgroundResId : config.unselectedBackgroundId :
config.unselectedBackgroundLastId;

setOrientation(config.orientation == VERTICAL ? VERTICAL : HORIZONTAL);
setGravity(config.gravity >= 0 ? config.gravity : Gravity.CENTER);
Expand Down Expand Up @@ -182,8 +193,14 @@ public void createIndicators(int count, int currentPosition) {
mImmediateAnimatorOut.setTarget(indicator);
mImmediateAnimatorOut.start();
mImmediateAnimatorOut.end();
if(i == count - 1 ) {
indicator.setBackgroundResource(mLastIndicatorBackgroundResId);
}
} else {
indicator.setBackgroundResource(mIndicatorUnselectedBackgroundResId);
if(i == count - 1 ) {
indicator.setBackgroundResource(mLastIndicatorUnselectedBackgroundResId);
}
mImmediateAnimatorIn.setTarget(indicator);
mImmediateAnimatorIn.start();
mImmediateAnimatorIn.end();
Expand Down Expand Up @@ -231,13 +248,19 @@ public void animatePageSelected(int position) {
View currentIndicator;
if (mLastPosition >= 0 && (currentIndicator = getChildAt(mLastPosition)) != null) {
currentIndicator.setBackgroundResource(mIndicatorUnselectedBackgroundResId);
if(currentIndicator == getChildAt(getChildCount()-1)) {
currentIndicator.setBackgroundResource(mLastIndicatorUnselectedBackgroundResId);
}
mAnimatorIn.setTarget(currentIndicator);
mAnimatorIn.start();
}

View selectedIndicator = getChildAt(position);
if (selectedIndicator != null) {
selectedIndicator.setBackgroundResource(mIndicatorBackgroundResId);
if(selectedIndicator == getChildAt(getChildCount()-1)) {
selectedIndicator.setBackgroundResource(mLastIndicatorBackgroundResId);
}
mAnimatorOut.setTarget(selectedIndicator);
mAnimatorOut.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public class Config {
@AnimatorRes int animatorResId = R.animator.scale_with_alpha;
@AnimatorRes int animatorReverseResId = 0;
@DrawableRes int backgroundResId = R.drawable.white_radius;
@DrawableRes int backgroundLastResID = R.drawable.white_radius;
@DrawableRes int unselectedBackgroundId;
@DrawableRes int unselectedBackgroundLastId;
int orientation = LinearLayout.HORIZONTAL;
int gravity = Gravity.CENTER;

Expand Down Expand Up @@ -58,6 +60,11 @@ public Builder drawable(@DrawableRes int backgroundResId) {
return this;
}

public Builder drawableLast(@DrawableRes int backgroundLastResId) {
mConfig.backgroundLastResID = backgroundLastResId;
return this;
}

public Builder drawableUnselected(@DrawableRes int unselectedBackgroundId) {
mConfig.unselectedBackgroundId = unselectedBackgroundId;
return this;
Expand Down
2 changes: 2 additions & 0 deletions circleindicator/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<attr name="ci_animator_reverse" format="reference"/>
<attr name="ci_drawable" format="reference"/>
<attr name="ci_drawable_unselected" format="reference"/>
<attr name="ci_drawable_last" format="reference"/>
<attr name="ci_drawable_last_unselected" format="reference"/>

<attr name="ci_orientation" format="enum">
<!-- Defines an horizontal widget. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import me.relex.circleindicator.sample.fragment.ChangeColorFragment;
import me.relex.circleindicator.sample.fragment.CustomAnimationFragment;
import me.relex.circleindicator.sample.fragment.CustomLastItemFragment;
import me.relex.circleindicator.sample.fragment.DefaultFragment;
import me.relex.circleindicator.sample.fragment.DynamicAdapterFragment;
import me.relex.circleindicator.sample.fragment.LoopRecyclerViewFragment;
Expand Down Expand Up @@ -75,6 +76,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
recyclerView.setAdapter(adapter);

adapter.add(new SampleInfo("Default", DefaultFragment.class.getName()));
adapter.add(new SampleInfo("Custom Last Item", CustomLastItemFragment.class.getName()));
adapter.add(
new SampleInfo("Custom Animation", CustomAnimationFragment.class.getName()));
adapter.add(new SampleInfo("Change Color", ChangeColorFragment.class.getName()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.relex.circleindicator.sample.fragment;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import me.relex.circleindicator.CircleIndicator;
import me.relex.circleindicator.sample.R;
import me.relex.circleindicator.sample.SamplePagerAdapter;

public class CustomLastItemFragment extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_custom_last_item, container, false);
}

@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
ViewPager viewpager = view.findViewById(R.id.viewpager);
CircleIndicator indicator = view.findViewById(R.id.indicator);
viewpager.setAdapter(new SamplePagerAdapter());
indicator.setViewPager(viewpager);
viewpager.setCurrentItem(2);
}
}
Binary file added sample/src/main/res/drawable/add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/src/main/res/drawable/add_unselected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions sample/src/main/res/layout/fragment_custom_last_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.viewpager.widget.ViewPager
android:id="@id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<me.relex.circleindicator.CircleIndicator
android:id="@id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="bottom"
android:layout_marginBottom="48dp"
app:ci_width="8dp"
app:ci_height="8dp"
app:ci_drawable_last="@drawable/add"
app:ci_drawable_last_unselected="@drawable/add_unselected"/>

</FrameLayout>