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

BannerConfig增加一个配置用于是否使用新的方式处理,数据发生变化未刷新适配器导致的崩溃的问题 #1210

Open
wants to merge 2 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
52 changes: 44 additions & 8 deletions banner/src/main/java/com/youth/banner/adapter/BannerAdapter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.youth.banner.adapter;

import android.annotation.SuppressLint;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
Expand All @@ -14,9 +15,6 @@
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


public abstract class BannerAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> implements IViewHolder<T, VH> {
protected List<T> mDatas = new ArrayList<>();
Expand All @@ -33,11 +31,33 @@ public BannerAdapter(List<T> datas) {
*
* @param datas
*/
@SuppressLint("NotifyDataSetChanged")
public void setDatas(List<T> datas) {
if (datas == null) {
datas = new ArrayList<>();
if (BannerConfig.isIsUseBanerdata()) {
setList(datas);
} else {
if (datas == null) {
datas = new ArrayList<>();
}
mDatas = datas;
notifyDataSetChanged();
}

}

/***
* 设置数据
*/
@SuppressLint("NotifyDataSetChanged")
public void setList(List<T> datas) {
if (mDatas == null) {
mDatas = new ArrayList<>();
} else {
mDatas.clear();
}
if (mDatas != null && datas != null) {
mDatas.addAll(datas);
}
mDatas = datas;
notifyDataSetChanged();
}

Expand All @@ -48,7 +68,14 @@ public void setDatas(List<T> datas) {
* @return
*/
public T getData(int position) {
return mDatas.get(position);
if (mDatas == null) {
return null;
} else {
if (mDatas.size() > position&&position!=-1) {
return mDatas.get(position);
}
}
return null;
}

/**
Expand All @@ -58,7 +85,16 @@ public T getData(int position) {
* @return
*/
public T getRealData(int position) {
return mDatas.get(getRealPosition(position));

if (mDatas == null) {
return null;
} else {
int realPosition = getRealPosition(position);
if (mDatas.size() > realPosition&&position!=-1) {
return mDatas.get(realPosition);
}
}
return null;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ public class BannerConfig {
public static final int INDICATOR_HEIGHT = (int) BannerUtils.dp2px(3);
public static final int INDICATOR_RADIUS = (int) BannerUtils.dp2px(3);

public static boolean IS_USE_BANERDATA=false;

public static boolean isIsUseBanerdata() {
return IS_USE_BANERDATA;
}

public static void setIsUseBanerdata(boolean isUseBanerdata) {
IS_USE_BANERDATA = isUseBanerdata;
}
}