Skip to content

Commit

Permalink
Merge pull request #2616 from passin95/master
Browse files Browse the repository at this point in the history
支持在一个方法中添加多个view的事件
  • Loading branch information
AllenCoder authored Jan 12, 2019
2 parents 9be1fcb + a2dc3da commit 64e7986
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import android.util.Log;
import android.view.View;

import com.chad.baserecyclerviewadapterhelper.R;
import com.chad.baserecyclerviewadapterhelper.entity.Level0Item;
import com.chad.baserecyclerviewadapterhelper.entity.Level1Item;
import com.chad.baserecyclerviewadapterhelper.entity.Person;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.IExpandable;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.List;

/**
Expand Down Expand Up @@ -96,7 +95,15 @@ public void onClick(View v) {
@Override
public boolean onLongClick(View v) {
int pos = holder.getAdapterPosition();
// 先获取到当前 item 的父 positon,再移除自己
int positionAtAll = getParentPositionInAll(pos);
remove(pos);
if (positionAtAll != -1) {
IExpandable multiItemEntity = (IExpandable) getData().get(positionAtAll);
if (!hasSubItems(multiItemEntity)) {
remove(positionAtAll);
}
}
return true;
}
});
Expand All @@ -108,7 +115,15 @@ public boolean onLongClick(View v) {
@Override
public void onClick(View view) {
int pos = holder.getAdapterPosition();
// 先获取到当前 item 的父 positon,再移除自己
int positionAtAll = getParentPositionInAll(pos);
remove(pos);
if (positionAtAll != -1) {
IExpandable multiItemEntity = (IExpandable) getData().get(positionAtAll);
if (!hasSubItems(multiItemEntity)) {
remove(positionAtAll);
}
}
}
});
break;
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
repositories {
jcenter()
google()
jcenter()
mavenLocal()
mavenCentral()

Expand All @@ -13,8 +13,8 @@ buildscript {

allprojects {
repositories {
jcenter()
google()
jcenter()
mavenCentral()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import android.support.annotation.IntRange;
import android.support.annotation.LayoutRes;
import android.util.Log;
import android.util.SparseIntArray;
import android.view.ViewGroup;

import com.chad.library.adapter.base.entity.IExpandable;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.List;

/**
Expand Down Expand Up @@ -108,6 +105,40 @@ protected void removeDataFromParent(T child) {
parent.getSubItems().remove(child);
}
}

/**
* 该方法用于 IExpandable 树形列表。
* 如果不存在 Parent,则 return -1。
*
* @param position 所处列表的位置
* @return 父 position 在数据列表中的位置
*/
public int getParentPositionInAll(int position) {
List<T> data = getData();
MultiItemEntity multiItemEntity = getItem(position);

if (isExpandable(multiItemEntity)) {
IExpandable IExpandable = (IExpandable) multiItemEntity;
for (int i = position - 1; i >= 0; i--) {
MultiItemEntity entity = data.get(i);
if (isExpandable(entity) && IExpandable.getLevel() > ((IExpandable) entity).getLevel()) {
return i;
}
}
} else {
for (int i = position - 1; i >= 0; i--) {
MultiItemEntity entity = data.get(i);
if (isExpandable(entity)) {
return i;
}
}
}
return -1;
}

public boolean isExpandable(MultiItemEntity item) {
return item != null && item instanceof IExpandable;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.chad.library.adapter.base;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

import android.animation.Animator;
import android.content.Context;
import android.support.annotation.IdRes;
Expand All @@ -35,7 +38,6 @@
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.animation.AlphaInAnimation;
import com.chad.library.adapter.base.animation.BaseAnimation;
import com.chad.library.adapter.base.animation.ScaleInAnimation;
Expand All @@ -46,7 +48,6 @@
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.chad.library.adapter.base.loadmore.SimpleLoadMoreView;
import com.chad.library.adapter.base.util.MultiTypeDelegate;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
Expand All @@ -58,9 +59,6 @@
import java.util.Collection;
import java.util.List;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;


/**
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper
Expand Down Expand Up @@ -1861,7 +1859,7 @@ private int getItemPosition(T item) {
return item != null && mData != null && !mData.isEmpty() ? mData.indexOf(item) : -1;
}

private boolean hasSubItems(IExpandable item) {
public boolean hasSubItems(IExpandable item) {
if (item == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,71 +369,76 @@ public BaseViewHolder setOnClickListener(@IdRes int viewId, View.OnClickListener
/**
* add childView id
*
* @param viewId add the child view id can support childview click
* @param viewIds add the child views id can support childview click
* @return if you use adapter bind listener
* @link {(adapter.setOnItemChildClickListener(listener))}
* <p>
* or if you can use recyclerView.addOnItemTouch(listerer) wo also support this menthod
*/
@SuppressWarnings("unchecked")
public BaseViewHolder addOnClickListener(@IdRes final int viewId) {
childClickViewIds.add(viewId);
final View view = getView(viewId);
if (view != null) {
if (!view.isClickable()) {
view.setClickable(true);
}
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapter.getOnItemChildClickListener() != null) {
adapter.getOnItemChildClickListener().onItemChildClick(adapter, v, getClickPosition());
}
public BaseViewHolder addOnClickListener(@IdRes final int ...viewIds) {
for (int viewId : viewIds) {
childClickViewIds.add(viewId);
final View view = getView(viewId);
if (view != null) {
if (!view.isClickable()) {
view.setClickable(true);
}
});
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapter.getOnItemChildClickListener() != null) {
adapter.getOnItemChildClickListener().onItemChildClick(adapter, v, getClickPosition());
}
}
});
}
}

return this;
}


/**
* set nestview id
*
* @param viewId add the child view id can support childview click
* @param viewIds add the child views id can support childview click
* @return
*/
public BaseViewHolder setNestView(@IdRes int viewId) {
addOnClickListener(viewId);
addOnLongClickListener(viewId);
nestViews.add(viewId);
public BaseViewHolder setNestView(@IdRes int ... viewIds) {
for (int viewId : viewIds) {
nestViews.add(viewId);
}
addOnClickListener(viewIds);
addOnLongClickListener(viewIds);
return this;
}

/**
* add long click view id
*
* @param viewId
* @param viewIds
* @return if you use adapter bind listener
* @link {(adapter.setOnItemChildLongClickListener(listener))}
* <p>
* or if you can use recyclerView.addOnItemTouch(listerer) wo also support this menthod
*/
@SuppressWarnings("unchecked")
public BaseViewHolder addOnLongClickListener(@IdRes final int viewId) {
itemChildLongClickViewIds.add(viewId);
final View view = getView(viewId);
if (view != null) {
if (!view.isLongClickable()) {
view.setLongClickable(true);
}
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return adapter.getOnItemChildLongClickListener() != null &&
adapter.getOnItemChildLongClickListener().onItemChildLongClick(adapter, v, getClickPosition());
public BaseViewHolder addOnLongClickListener(@IdRes final int ... viewIds) {
for (int viewId : viewIds) {
itemChildLongClickViewIds.add(viewId);
final View view = getView(viewId);
if (view != null) {
if (!view.isLongClickable()) {
view.setLongClickable(true);
}
});
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return adapter.getOnItemChildLongClickListener() != null &&
adapter.getOnItemChildLongClickListener().onItemChildLongClick(adapter, v, getClickPosition());
}
});
}
}
return this;
}
Expand All @@ -459,7 +464,7 @@ public BaseViewHolder setOnTouchListener(@IdRes int viewId, View.OnTouchListener
* @param viewId The view id.
* @param listener The on long click listener;
* @return The BaseViewHolder for chaining.
* Please use {@link #addOnLongClickListener(int)} (adapter.setOnItemChildLongClickListener(listener))}
* Please use {@link #addOnLongClickListener} (adapter.setOnItemChildLongClickListener(listener))}
*/
@Deprecated
public BaseViewHolder setOnLongClickListener(@IdRes int viewId, View.OnLongClickListener listener) {
Expand All @@ -474,7 +479,7 @@ public BaseViewHolder setOnLongClickListener(@IdRes int viewId, View.OnLongClick
* @param viewId The view id.
* @param listener The item on click listener;
* @return The BaseViewHolder for chaining.
* Please use {@link #addOnClickListener(int)} (int)} (adapter.setOnItemChildClickListener(listener))}
* Please use {@link #addOnClickListener} (int)} (adapter.setOnItemChildClickListener(listener))}
*/
@Deprecated
public BaseViewHolder setOnItemClickListener(@IdRes int viewId, AdapterView.OnItemClickListener listener) {
Expand Down

0 comments on commit 64e7986

Please sign in to comment.