Skip to content

Commit

Permalink
getCurrentBean延时使用会主动抛出空指针异常,防止被错误的滥用
Browse files Browse the repository at this point in the history
  • Loading branch information
weimingjue committed Jan 18, 2021
1 parent d5b2bde commit 9c6df10
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public MyAdapter() {
```

## 导入方式
你的build.gradle要有jitpack.io,大致如下
你的build.gradle要有jitpack.io,大致如下
```
allprojects {
repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
default:
ItemAdapterPositionInfo info = getItemPositionInfo(position);
IContainerItemAdapter itemAdapter = info.mItemAdapter;
//noinspection unchecked
itemAdapter.setCurrentBean(getList().get(info.mListPosition));
itemAdapter.bindViewHolder(holder, info.mItemPosition);
//noinspection ConstantConditions,unchecked,置null便于检查错误
itemAdapter.setCurrentBean(null);
break;
}
}
Expand Down Expand Up @@ -190,7 +194,11 @@ public int getItemViewType(int position) {

ItemAdapterPositionInfo info = getItemPositionInfo(position);
IContainerItemAdapter itemAdapter = info.mItemAdapter;
//noinspection unchecked
itemAdapter.setCurrentBean(getList().get(info.mListPosition));
int itemType = itemAdapter.getItemViewType(info.mItemPosition);
//noinspection ConstantConditions,unchecked,置null便于检查错误
itemAdapter.setCurrentBean(null);
if (itemType < TYPE_MIN || itemType >= TYPE_MAX) {
throw new RuntimeException("你的adapter" + itemAdapter.getClass() + "的type必须在" + TYPE_MIN + "~" + TYPE_MAX + "之间,type:" + itemType);
}
Expand All @@ -212,6 +220,8 @@ public int getItemCount() {
//noinspection unchecked 如果出现ClassCastException,请检查你list里的bean对象和adapter的bean是否一致
itemAdapter.setCurrentBean(bean);
count += itemAdapter.getItemCount();
//noinspection ConstantConditions,unchecked,置null便于检查错误
itemAdapter.setCurrentBean(null);
}
return count;
}
Expand All @@ -232,7 +242,7 @@ protected void checkLayoutManager() {

/**
* 根据绝对position获取子adapter的相关信息
* 并且已经做过{@link IContainerItemAdapter#setCurrentBean}{@link IContainerItemAdapter#setCurrentPositionInfo}
* 已经做过{@link IContainerItemAdapter#setCurrentPositionInfo}
*
* @param position 绝对position
*/
Expand All @@ -248,6 +258,9 @@ protected ItemAdapterPositionInfo getItemPositionInfo(int position) {
//noinspection unchecked 如果出现ClassCastException,请检查你list里的bean对象和adapter的bean是否一致
itemAdapter.setCurrentBean(bean);
int itemCount = itemAdapter.getItemCount();
//noinspection ConstantConditions,unchecked,置null便于检查错误
itemAdapter.setCurrentBean(null);

int nextStartPosition = itemStartPosition + itemCount;
//下一个adapter的位置比position大说明当前type就在这个adapter中
if (nextStartPosition > position) {
Expand Down Expand Up @@ -457,13 +470,17 @@ public int getAbsPosition(IContainerBean bean, int itemAdapterPosition) {
//noinspection unchecked 如果出现ClassCastException,请检查你list里的bean对象和adapter的bean是否一致
itemAdapter.setCurrentBean(listBean);
position += itemAdapter.getItemCount();
//noinspection ConstantConditions,unchecked,置null便于检查错误
itemAdapter.setCurrentBean(null);
}
}
throw new RuntimeException("在list中没有找到传入的bean对象" + bean);
}

/**
* 根据绝对position获取对应adapter的额外信息
*
* @return 这个对象是复用的,一次性消费
*/
@MainThread
public ItemAdapterPositionInfo getItemAdapterPositionInfo(int absPosition) {
Expand All @@ -474,6 +491,7 @@ public ItemAdapterPositionInfo getItemAdapterPositionInfo(int absPosition) {
* 根据bean和相对position获取对应adapter的额外信息
*
* @param itemAdapterPosition 相对potion
* @return 这个对象是复用的,一次性消费
*/
@MainThread
public ItemAdapterPositionInfo getItemAdapterPositionInfo(IContainerBean bean, int itemAdapterPosition) {
Expand Down Expand Up @@ -527,7 +545,12 @@ public int getSpanSize(int position) {
}
ItemAdapterPositionInfo info = getItemPositionInfo(position);
IContainerItemAdapter itemAdapter = info.mItemAdapter;
return itemAdapter.getSpanSize(info.mItemPosition);
//noinspection unchecked
itemAdapter.setCurrentBean(getList().get(info.mListPosition));
int spanSize = itemAdapter.getSpanSize(info.mItemPosition);
//noinspection ConstantConditions,unchecked,置null便于检查错误
itemAdapter.setCurrentBean(null);
return spanSize;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,17 @@ public OnItemClickListener<BEAN> getListener() {

/**
* @return 当前的bean,每次想用的时候get就对了
* 在回调里请用这个{@link OnItemClickListener#getCurrentBean}
* 用的的地方:{@link #getItemCount}{@link #onCreateViewHolder}{@link #onBindViewHolder}{@link #getSpanSize}{@link #getItemViewType}...
* 用的的地方:{@link #getItemCount}{@link #onBindViewHolder}{@link #getSpanSize}{@link #getItemViewType}...
* <p>
* 注意:不能延时后调用,如onClickListener,请使用{@link OnItemClickListener#getCurrentBean}或get后声明为final
* @throws NullPointerException 延时调用的才会抛出,为了便于检查错误
*/
@NonNull
@Override
public final BEAN getCurrentBean() {
if (mCurrentBean == null) {
throw new NullPointerException("请注意调用时机,使用OnItemClickListener#getCurrentBean或get后声明为final");
}
return mCurrentBean;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ default void notifyItemRemoved(int position, @NonNull BEAN bean) {
void attachContainer(@NonNull BaseContainerAdapter containerAdapter);

/**
* 没有position,不能使用getCurrent相关方法
*
* @param viewType 该adapter自己的type
*/
@NonNull
Expand Down Expand Up @@ -99,12 +101,15 @@ default int getItemViewType(int position) {
*
* @throws ClassCastException 请检查你list里的bean对象和adapter的bean是否一致
*/
@SuppressWarnings("NullableProblems")
void setCurrentBean(@NonNull BEAN bean);

/**
* @return 当前的bean,每次想用的时候get就对了
* 在回调里请用这个{@link OnItemClickListener#getCurrentBean}
* 用的的地方:{@link #getItemCount}{@link #createViewHolder}{@link #bindViewHolder}{@link #getSpanSize}{@link #getItemViewType}...
* 用的的地方:{@link #getItemCount}{@link #bindViewHolder}{@link #getSpanSize}{@link #getItemViewType}...
* <p>
* 注意:不能延时后调用,如onClickListener,请使用{@link OnItemClickListener#getCurrentBean}或get后声明为final
* @throws NullPointerException 延时调用的才会抛出,为了便于检查错误
*/
@NonNull
BEAN getCurrentBean();
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Wed Mar 25 16:17:30 CST 2020
#Sat Dec 26 11:42:22 CST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down

0 comments on commit 9c6df10

Please sign in to comment.