Skip to content

Commit

Permalink
优化BaseViewHolder,减少异常信息
Browse files Browse the repository at this point in the history
ListCycleAdapter可设置是否循环
点击事件优化,防止嵌套时抢占事件
demo举例完善
  • Loading branch information
weimingjue committed Aug 23, 2020
1 parent 3272ee4 commit 9cbc286
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 70 deletions.
53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,61 @@ BaseAdapterRvList<AdapterMainListBinding, String> adapter = BaseAdapterRvList.cr
@NonNull
@Override
public BaseViewHolder<AdapterMainListBinding> onCreateListViewHolder(@NonNull ViewGroup parent) {
BaseViewHolder<AdapterMainListBinding> holder = super.onCreateListViewHolder(parent);
BaseViewHolder<AdapterMainListBinding> holder = super.onCreateListViewHolder(parent);//也可以不用super
holder.itemView.setBackgroundColor(0xffeeeeee);
return holder;
}
}
```
adapter里有个button,点完后还要写个回调给Activity???反正我是不喜欢:
```
public void onBindListViewHolder(@NonNull BaseViewHolder<AdapterMainListBinding> holder, int listPosition, TestBean bean) {
setItemViewClick(holder.getBinding().btButton, holder);
}
...
adapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(@NonNull View view, int listPosition) {
switch (view.getId()) {
case R.id.bt_button:
toast("没想到吧,还能这样玩:" + listPosition);
break;
default:
toast("你点击了整个条目:" + listPosition);
break;
}
}
});
```
adapter里又套了一个RecyclerView,简直是回调地狱啊...完全受不了:
```
public void onBindListViewHolder(@NonNull BaseViewHolder<AdapterMainListBinding> holder, int listPosition, TestBean bean) {
//需提前setAdapter(在bind或者每次create时)
setItemRvData(holder.getBinding().rvItemList, holder, bean.itemTextList);
...
adapter.setOnItemClickListener(new OnItemItemClickListener() {
@Override
public void onParentItemClick(@NonNull View view, int parentPosition) {
toast("你点击了外层:" + parentPosition);
}
@Override
public void onChildItemClick(@NonNull View view, int parentPosition, int childPosition) {
toast("你点击了外层:" + parentPosition + ",内层:" + childPosition);
}
@Override
public void onParentHeaderClick(@NonNull View view) {
toast("你点击了外层:header");
}
@Override
public void onChildHeaderClick(@NonNull View view, int parentPosition) {
toast("你点击了外层:" + parentPosition + ",内层:header");
}
//...footer、longClick等都有,需要多看看
});
```
ViewPager的Fragment更简单
```
mVp.setAdapter(new BaseFragmentPagerAdapter(getSupportFragmentManager(), mFrags));
Expand Down Expand Up @@ -131,7 +180,7 @@ allprojects {

不需要layoutId的混淆要求:
```
#框架特殊要求
# 框架特殊要求
# 根据泛型获取res资源需要
-keep class * extends androidx.databinding.ViewDataBinding
```
2 changes: 1 addition & 1 deletion adapters/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ dependencies {
implementation 'com.zhy:flowlayout-lib:1.0.3'

//自己的适用于超复杂列表样式,如:聊天、首页等
api 'com.github.weimingjue:BaseContainerAdapter:3.0.2'
api 'com.github.weimingjue:BaseContainerAdapter:3.0.3'
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ public final BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int vi
}

public final void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
//设置点击事件,不判断会顶掉lv的itemClick事件
if (mListener != null) {
holder.itemView.setOnClickListener(mListener);
holder.itemView.setOnLongClickListener(mListener);
}
//设置点击事件
holder.itemView.setOnClickListener(mListener);
holder.itemView.setOnLongClickListener(mListener);
//防止null时抢占事件
holder.itemView.setClickable(mListener != null);
holder.itemView.setLongClickable(mListener != null);
holder.setLvPosition(position);//设置position
onBindViewHolder2(holder, position);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import com.wang.adapters.helper.ListAdapterHelper;
import com.wang.adapters.interfaces.OnItemClickListener;
import com.wang.adapters.utils.ArrayUtils;
import com.wang.container.holder.BaseViewHolder;
import com.wang.container.interfaces.IListAdapter;

Expand All @@ -23,6 +24,8 @@ public abstract class BaseAdapterLvsListCycle<DB extends ViewDataBinding, BEAN>

private final ListAdapterHelper<DB, BEAN> mHelper;

private boolean mIsCycle = true;

/**
* 资源id已经不是必须的了
* <p>
Expand All @@ -45,7 +48,10 @@ public BaseAdapterLvsListCycle(@LayoutRes int layoutId, @Nullable List<BEAN> lis

@Override
public final int getItemCount() {
return getList().isEmpty() ? 0 : Integer.MAX_VALUE;
if (ArrayUtils.isEmpty(getList())) {
return 0;
}
return isCycle() ? Integer.MAX_VALUE : size();
}

@Override
Expand Down Expand Up @@ -123,4 +129,18 @@ public BEAN get(int position) {
public BaseViewHolder<DB> onCreateListViewHolder(@NonNull ViewGroup parent) {
return mHelper.onCreateDefaultViewHolder(parent, BaseAdapterLvsListCycle.class, getClass());
}

/**
* @param isCycle true 默认值,可循环滑动
*/
public void setCycle(boolean isCycle) {
if (isCycle != mIsCycle) {
mIsCycle = isCycle;
notifyDataSetChanged();
}
}

public boolean isCycle() {
return mIsCycle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.wang.adapters.R;
Expand Down Expand Up @@ -37,9 +36,12 @@ public final BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int vi

@Override
public final void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
//创建点击事件
//设置点击事件
holder.itemView.setOnClickListener(mListener);
holder.itemView.setOnLongClickListener(mListener);
//防止null时抢占事件
holder.itemView.setClickable(mListener != null);
holder.itemView.setLongClickable(mListener != null);
onBindViewHolder2(holder, position);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public BaseViewHolder<DB> onCreateListViewHolder(@NonNull ViewGroup parent) {
}

/**
* @param view null表示删除
* @param view null表示删除,view的parent为FrameLayout,默认match、wrap
*/
public void setHeaderView(@Nullable View view) {
mHelper.setHeaderView(view);
Expand All @@ -112,7 +112,7 @@ public View getHeaderView() {
}

/**
* @param view null表示删除
* @param view null表示删除,view的parent为FrameLayout,默认match、wrap
*/
@Override
public void setFooterView(@Nullable View view) {
Expand All @@ -130,17 +130,26 @@ public View getFooterView() {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
* 懒得不能再懒了的写法
* 就资源id,数据在xml里修改
*
* @param layoutId create时的资源id
* 就资源id,使用dataBinding绑定数据
*/
public static <BEAN> BaseAdapterRvList<?, BEAN> createAdapter(@LayoutRes final int layoutId) {
return createAdapter(null, layoutId, null);
return createAdapter(null, layoutId);
}

/**
* 就资源id,使用dataBinding绑定数据
*/
public static <BEAN> BaseAdapterRvList<?, BEAN> createAdapter(@Nullable List<BEAN> list, @LayoutRes final int layoutId) {
return createAdapter(list, layoutId, null);
}

/**
* 资源id+回调
*
* @param layoutId 资源id,必须有
*/
public static <DB extends ViewDataBinding, BEAN> BaseAdapterRvList<DB, BEAN> createAdapter
(@Nullable List<BEAN> list, @LayoutRes final int layoutId, @Nullable final OnAdapterBindListener<DB, BEAN> listener) {
(@Nullable List<BEAN> list, @LayoutRes final int layoutId, @Nullable final OnAdapterBindListener<DB, BEAN> listener) {
return new BaseAdapterRvList<DB, BEAN>(layoutId, list) {

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import com.wang.adapters.helper.ListAdapterHelper;
import com.wang.adapters.interfaces.OnItemClickListener;
import com.wang.adapters.utils.ArrayUtils;
import com.wang.container.holder.BaseViewHolder;
import com.wang.container.interfaces.IListAdapter;

Expand All @@ -23,6 +24,8 @@ public abstract class BaseAdapterRvListCycle<DB extends ViewDataBinding, BEAN> e

private final ListAdapterHelper<DB, BEAN> mHelper;

private boolean mIsCycle = true;

/**
* 资源id已经不是必须的了
* <p>
Expand All @@ -45,7 +48,10 @@ public BaseAdapterRvListCycle(@LayoutRes int layoutId, @Nullable List<BEAN> list

@Override
public final int getItemCount() {
return getList().isEmpty() ? 0 : Integer.MAX_VALUE;
if (ArrayUtils.isEmpty(getList())) {
return 0;
}
return isCycle() ? Integer.MAX_VALUE : size();
}

@Override
Expand Down Expand Up @@ -123,4 +129,18 @@ public BEAN get(int position) {
public BaseViewHolder<DB> onCreateListViewHolder(@NonNull ViewGroup parent) {
return mHelper.onCreateDefaultViewHolder(parent, BaseAdapterLvsListCycle.class, getClass());
}

/**
* @param isCycle true 默认值,可循环滑动
*/
public void setCycle(boolean isCycle) {
if (isCycle != mIsCycle) {
mIsCycle = isCycle;
notifyDataSetChanged();
}
}

public boolean isCycle() {
return mIsCycle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ default boolean onLongClick(@NonNull View view) {
/**
* 获取当前view所保存的position
*/
@Deprecated//不需要重写
@Deprecated//不需要重写,可以使用
default IAdapter getAdapter(@NonNull View view) {
return (IAdapter) view.getTag(R.id.tag_view_adapter);
}

/**
* 获取当前view所在的ViewHolder
*/
@Deprecated//不需要重写
@Deprecated//不需要重写,可以使用
default BaseViewHolder getViewHolder(@NonNull View view) {
return (BaseViewHolder) view.getTag(R.id.tag_view_holder);
}
Expand All @@ -72,7 +72,7 @@ default BaseViewHolder getViewHolder(@NonNull View view) {
*
* @return {@link #POSITION_HEADER}{@link #POSITION_FOOTER}或者0-list.size
*/
@Deprecated//不需要重写
@Deprecated//不需要重写,可以使用
default int getFormatPosition(IAdapter adapter, int position) {
if (adapter instanceof IListAdapter) {
//listAdapter有header、footer事件
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ default boolean performClick(View view, @ClickType int clickType, int listPositi
/**
* 外层的position需要遍历
*/
@Deprecated//不需要重写
@Deprecated//不需要重写,可以使用
default IAdapter getParentAdapter(@NonNull View view) {
ViewParent parent = view.getParent();
while (parent != null) {
Expand All @@ -147,7 +147,7 @@ default IAdapter getParentAdapter(@NonNull View view) {
/**
* 获取当前view所在的ViewHolder
*/
@Deprecated//不需要重写
@Deprecated//不需要重写,可以使用
default BaseViewHolder getParentViewHolder(@NonNull View view) {
ViewParent parent = view.getParent();
while (parent != null) {
Expand Down
Loading

0 comments on commit 9cbc286

Please sign in to comment.