Skip to content

Commit

Permalink
Indicator添加setOnIndicatorItemClickListener方法用于拦截处理点击事件,在OnItemSelecte…
Browse files Browse the repository at this point in the history
…dListener监听之前触发OnIndicatorItemClickListener监听
  • Loading branch information
LuckyJayce committed Jun 18, 2017
1 parent 499f7ab commit 7767b94
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 33 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.novoda:bintray-release:0.3.4'
}
}
Expand Down
7 changes: 4 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 23
versionCode 16
versionName "1.1.5"
versionCode 17
versionName "1.1.6"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

lintOptions {
abortOnError false
}
Expand All @@ -35,7 +36,7 @@ publish {
userOrg = 'luckyjayce'//bintray.com用户名
groupId = 'com.shizhefei'//jcenter上的路径
artifactId = 'ViewPagerIndicator'//项目名称
publishVersion = '1.1.5'//版本号
publishVersion = '1.1.6'//版本号
desc = '实现滑动tab,引导页等效果'//描述,不重要
website = 'https://github.com/LuckyJayce/ViewPagerIndicator'//网站,不重要;尽量模拟github上的地址,例如我这样的;当然你有地址最好了
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class FixedIndicatorView extends LinearLayout implements Indicator {

private OnItemSelectedListener onItemSelectedListener;

private OnIndicatorItemClickListener onIndicatorItemClickListener;

private int mSelectedTabIndex = -1;

public static final int SPLITMETHOD_EQUALS = 0;
Expand Down Expand Up @@ -229,9 +231,12 @@ public void onClick(View v) {
if (itemClickable) {
int i = (Integer) v.getTag();
ViewGroup parent = (ViewGroup) v;
setCurrentItem(i);
if (onItemSelectedListener != null) {
onItemSelectedListener.onItemSelected(parent.getChildAt(0), i, mPreSelectedTabIndex);
View itemView = parent.getChildAt(0);
if (onIndicatorItemClickListener == null || !onIndicatorItemClickListener.onItemClick(itemView, i)) {
setCurrentItem(i);
if (onItemSelectedListener != null) {
onItemSelectedListener.onItemSelected(parent.getChildAt(0), i, mPreSelectedTabIndex);
}
}
}
}
Expand Down Expand Up @@ -607,8 +612,8 @@ public void setOnTransitionListener(OnTransitionListener onTransitionListener) {

@Override
public View getItemView(int position) {
if(mAdapter == null){
return null;
if (mAdapter == null) {
return null;
}
if (position < 0 || position > mAdapter.getCount() - 1) {
return null;
Expand Down Expand Up @@ -683,6 +688,14 @@ public OnItemSelectedListener getOnItemSelectListener() {
return onItemSelectedListener;
}

public void setOnIndicatorItemClickListener(OnIndicatorItemClickListener onIndicatorItemClickListener) {
this.onIndicatorItemClickListener = onIndicatorItemClickListener;
}

public OnIndicatorItemClickListener getOnIndicatorItemClickListener() {
return onIndicatorItemClickListener;
}

@Override
public OnTransitionListener getOnTransitionListener() {
return onTransitionListener;
Expand Down
67 changes: 44 additions & 23 deletions library/src/main/java/com/shizhefei/view/indicator/Indicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,29 @@ public interface Indicator {
/**
* 设置适配器
*/
public void setAdapter(IndicatorAdapter adapter);
void setAdapter(IndicatorAdapter adapter);

/**
* 设置选中监听
*
* @param onItemSelectedListener
*/
public void setOnItemSelectListener(OnItemSelectedListener onItemSelectedListener);
void setOnItemSelectListener(OnItemSelectedListener onItemSelectedListener);

/**
* 获取选中监听
*
* @return OnItemSelectedListener
*/
public OnItemSelectedListener getOnItemSelectListener();
OnItemSelectedListener getOnItemSelectListener();

/**
* 设置Indicator tab项的点击事件,在Indicator 的 onItemSelectListener前触发和拦截处理
* @param onIndicatorItemClickListener
*/
void setOnIndicatorItemClickListener(OnIndicatorItemClickListener onIndicatorItemClickListener);

OnIndicatorItemClickListener getOnIndicatorItemClickListener();

/**
* 设置滑动变化的转换监听,tab在切换过程中会调用此监听。<br>
Expand All @@ -54,9 +62,9 @@ public interface Indicator {
*
* @param onTransitionListener
*/
public void setOnTransitionListener(OnTransitionListener onTransitionListener);
void setOnTransitionListener(OnTransitionListener onTransitionListener);

public OnTransitionListener getOnTransitionListener();
OnTransitionListener getOnTransitionListener();

/**
* 设置滑动块<br>
Expand All @@ -67,41 +75,41 @@ public interface Indicator {
*
* @param scrollBar
*/
public void setScrollBar(ScrollBar scrollBar);
void setScrollBar(ScrollBar scrollBar);

public IndicatorAdapter getIndicatorAdapter();
IndicatorAdapter getIndicatorAdapter();

/**
* 设置当前项.<br>
* 如果使用IndicatorViewPager则使用IndicatorViewPager.setCurrentItem而不是在调用该方法
*
* @param item
*/
public void setCurrentItem(int item);
void setCurrentItem(int item);

public void setCurrentItem(int item, boolean anim);
void setCurrentItem(int item, boolean anim);

/**
* 获取每一项的tab
*
* @param item 索引
* @return ItemView
*/
public View getItemView(int item);
View getItemView(int item);

/**
* 获取当前选中项
*
* @return current
*/
public int getCurrentItem();
int getCurrentItem();

/**
* 获取之前选中的项,可能返回-1,表示之前没有选中
*
* @return PreSelectItem
*/
public int getPreSelectItem();
int getPreSelectItem();

/**
* ViewPager切换变化的函数
Expand All @@ -110,12 +118,12 @@ public interface Indicator {
* @param positionOffset
* @param positionOffsetPixels
*/
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);
void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);

/**
* 适配器
*/
public static abstract class IndicatorAdapter {
abstract class IndicatorAdapter {
private boolean isLoop;

boolean isLoop() {
Expand Down Expand Up @@ -151,14 +159,14 @@ public void unRegistDataSetObserver(DataSetObserver observer) {
/**
* 数据源观察者
*/
static interface DataSetObserver {
public void onChange();
interface DataSetObserver {
void onChange();
}

/**
* tab项选中监听
*/
public static interface OnItemSelectedListener {
interface OnItemSelectedListener {
/**
* 注意 preItem 可能为 -1。表示之前没有选中过,每次adapter.notifyDataSetChanged也会将preItem
* 设置为-1;
Expand All @@ -167,7 +175,20 @@ public static interface OnItemSelectedListener {
* @param select 当前选中项的索引
* @param preSelect 之前选中项的索引
*/
public void onItemSelected(View selectItemView, int select, int preSelect);
void onItemSelected(View selectItemView, int select, int preSelect);
}

/**
* tab项点击监听和拦截
*/
interface OnIndicatorItemClickListener {
/**
* @param clickItemView
* @param position 点击的tab的position
* @return 返回true表示 拦截点击事件,不会继续触发Indicator的setCurrent和调用OnItemSelectedListener
* 返回false 按照Indicator原有的处理方式
*/
boolean onItemClick(View clickItemView, int position);
}

/**
Expand All @@ -176,13 +197,13 @@ public static interface OnItemSelectedListener {
* 目前提供的子类
* {@link com.shizhefei.view.indicator.transition.OnTransitionTextListener}
*/
public static interface OnTransitionListener {
public void onTransition(View view, int position, float selectPercent);
interface OnTransitionListener {
void onTransition(View view, int position, float selectPercent);
}

public void onPageScrollStateChanged(int state);
void onPageScrollStateChanged(int state);

public void setItemClickable(boolean clickable);
void setItemClickable(boolean clickable);

public boolean isItemClickable();
boolean isItemClickable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ public void setIndicatorScrollBar(ScrollBar scrollBar) {
indicatorView.setScrollBar(scrollBar);
}

/**
* 设置Indicator tab项的点击事件,在Indicator 的 onItemSelectListener前触发和拦截处理
*
* @param onIndicatorItemClickListener
*/
public void setOnIndicatorItemClickListener(Indicator.OnIndicatorItemClickListener onIndicatorItemClickListener) {
indicatorView.setOnIndicatorItemClickListener(onIndicatorItemClickListener);
}

/**
* @return
*/
public Indicator.OnIndicatorItemClickListener getOnIndicatorItemClickListener() {
return indicatorView.getOnIndicatorItemClickListener();
}

/**
* 设置缓存界面的个数,左右两边缓存界面的个数,不会被重新创建。<br>
* 默认是1,表示左右两边 相连的1个界面和当前界面都会被缓存住,比如切换到左边的一个界面,那个界面是不会重新创建的。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class RecyclerIndicatorView extends RecyclerView implements Indicator {
private float positionOffset;
private int positionOffsetPixels;
private int unScrollPosition = -1;
private OnIndicatorItemClickListener onIndicatorItemClickListener;

public RecyclerIndicatorView(Context context) {
super(context);
Expand Down Expand Up @@ -346,7 +347,9 @@ public int getItemCount() {
public void onClick(View v) {
if (itemClickable) {
int position = (Integer) v.getTag();
setCurrentItem(position, true);
if (onIndicatorItemClickListener == null || !onIndicatorItemClickListener.onItemClick(getItemView(position), position)) {
setCurrentItem(position, true);
}
}
}
};
Expand Down Expand Up @@ -442,6 +445,14 @@ private int measureScrollBar(int position, float selectPercent, boolean needChan
return scrollBar.getSlideView().getWidth();
}

public void setOnIndicatorItemClickListener(OnIndicatorItemClickListener onIndicatorItemClickListener) {
this.onIndicatorItemClickListener = onIndicatorItemClickListener;
}

public OnIndicatorItemClickListener getOnIndicatorItemClickListener() {
return onIndicatorItemClickListener;
}

private int selectItem;
private int preSelectItem;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ public OnItemSelectedListener getOnItemSelectListener() {
return proxyOnItemSelectListener.getOnItemSelectedListener();
}

public void setOnIndicatorItemClickListener(OnIndicatorItemClickListener onIndicatorItemClickListener) {
fixedIndicatorView.setOnIndicatorItemClickListener(onIndicatorItemClickListener);
}

public OnIndicatorItemClickListener getOnIndicatorItemClickListener() {
return fixedIndicatorView.getOnIndicatorItemClickListener();
}

@Override
public void setOnTransitionListener(OnTransitionListener onPageScrollListener) {
fixedIndicatorView.setOnTransitionListener(onPageScrollListener);
Expand Down

0 comments on commit 7767b94

Please sign in to comment.