fix multi state view, optimize adapter

androidx
Zhanty 5 years ago
parent 8d8768a06b
commit 1210fee50b
  1. 2
      lib_base/src/main/java/com/android/base/adapter/DataManager.java
  2. 5
      lib_base/src/main/java/com/android/base/adapter/list/BaseListAdapter.java
  3. 5
      lib_base/src/main/java/com/android/base/adapter/list/ListDataManagerImpl.java
  4. 5
      lib_base/src/main/java/com/android/base/adapter/recycler/DiffRecyclerAdapter.java
  5. 5
      lib_base/src/main/java/com/android/base/adapter/recycler/MultiTypeAdapter.java
  6. 5
      lib_base/src/main/java/com/android/base/adapter/recycler/RecyclerAdapter.java
  7. 23
      lib_base/src/main/java/com/android/base/adapter/recycler/RecyclerDataManagerImpl.java
  8. 78
      lib_base/src/main/java/com/android/base/app/aac/AutoDisposeLiveData.kt
  9. 12
      lib_base/src/main/java/com/android/base/app/aac/ResourceLiveData.kt
  10. 10
      lib_base/src/main/java/com/android/base/app/fragment/Fragments.kt
  11. 2
      lib_base/src/main/java/com/android/base/app/fragment/RefreshableStateLayoutImpl.java
  12. 10
      lib_base/src/main/java/com/android/base/app/ui/UIEx.kt
  13. 14
      lib_base/src/main/java/com/android/base/kotlin/Collections.kt
  14. 44
      lib_base/src/main/java/com/android/base/widget/MultiStateView.java
  15. 2
      lib_base/src/main/res/values/base_ids.xml

@ -56,6 +56,8 @@ public interface DataManager<T> {
void removeItems(List<T> elements); void removeItems(List<T> elements);
void removeItems(List<T> elements, boolean isSuccessive);
//get //get
T getItem(int position); T getItem(int position);

@ -136,6 +136,11 @@ public abstract class BaseListAdapter<T, VH extends ViewHolder> extends BaseAdap
mDataManager.removeItems(elements); mDataManager.removeItems(elements);
} }
@Override
public void removeItems(List<T> elements, boolean isSuccessive) {
mDataManager.removeItems(elements, isSuccessive);
}
@Override @Override
public void removeAt(int index) { public void removeAt(int index) {
mDataManager.removeAt(index); mDataManager.removeAt(index);

@ -123,6 +123,11 @@ final class ListDataManagerImpl<T> implements DataManager<T> {
} }
} }
@Override
public void removeItems(List<T> elements, boolean isSuccessive) {
removeItems(elements);
}
@Override @Override
public void removeAt(int index) { public void removeAt(int index) {
if (mData != null && mData.size() > index) { if (mData != null && mData.size() > index) {

@ -195,6 +195,11 @@ public abstract class DiffRecyclerAdapter<T, VH extends ViewHolder> extends Recy
mAsyncListDiffer.submitList(newList); mAsyncListDiffer.submitList(newList);
} }
@Override
public void removeItems(List<T> elements, boolean isSuccessive) {
removeItems(elements);
}
@Override @Override
public void removeAt(int index) { public void removeAt(int index) {
if (getDataSize() > index) { if (getDataSize() > index) {

@ -120,6 +120,11 @@ public class MultiTypeAdapter extends me.drakeet.multitype.MultiTypeAdapter impl
mRecyclerDataManager.removeItems(elements); mRecyclerDataManager.removeItems(elements);
} }
@Override
public void removeItems(List<Object> elements, boolean isSuccessive) {
mRecyclerDataManager.removeItems(elements, isSuccessive);
}
@Override @Override
public Object getItem(int position) { public Object getItem(int position) {
return mRecyclerDataManager.getItem(position); return mRecyclerDataManager.getItem(position);

@ -112,6 +112,11 @@ public abstract class RecyclerAdapter<T, VH extends ViewHolder> extends Recycler
mDataManager.removeItems(elements); mDataManager.removeItems(elements);
} }
@Override
public void removeItems(List<T> elements, boolean isSuccessive) {
mDataManager.removeItems(elements, isSuccessive);
}
@Override @Override
public void removeAt(int index) { public void removeAt(int index) {
mDataManager.removeAt(index); mDataManager.removeAt(index);

@ -170,6 +170,25 @@ final class RecyclerDataManagerImpl<T> implements DataManager<T> {
} }
} }
@Override
public void removeItems(List<T> elements, boolean isSuccessive) {
if (Checker.isEmpty(elements) || Checker.isEmpty(mData)) {
return;
}
if (!isSuccessive) {
removeItems(elements);
return;
}
T data = elements.get(0);
int index = mData.indexOf(data);
if (index == -1) {
removeItems(elements);
} else {
mData.removeAll(elements);
notifyItemRangeRemoved(index, elements.size());
}
}
@Override @Override
public T getItem(int position) { public T getItem(int position) {
position = position - getHeaderSize(); position = position - getHeaderSize();
@ -241,6 +260,10 @@ final class RecyclerDataManagerImpl<T> implements DataManager<T> {
mAdapter.notifyItemRemoved(index); mAdapter.notifyItemRemoved(index);
} }
private void notifyItemRangeRemoved(int index, int size) {
mAdapter.notifyItemRangeRemoved(index, size);
}
void setHeaderSize(HeaderSize headerSize) { void setHeaderSize(HeaderSize headerSize) {
mHeaderSize = headerSize; mHeaderSize = headerSize;
} }

@ -8,38 +8,6 @@ import com.github.dmstocking.optional.java.util.Optional
import com.uber.autodispose.* import com.uber.autodispose.*
fun <T> ObservableSubscribeProxy<T>.toLiveData(): LiveData<T> {
val liveData = MutableLiveData<T>()
this.subscribeIgnoreError {
liveData.postValue(it)
}
return liveData
}
fun <T> FlowableSubscribeProxy<T>.toLiveData(): LiveData<T> {
val liveData = MutableLiveData<T>()
this.subscribeIgnoreError {
liveData.postValue(it)
}
return liveData
}
fun <T> SingleSubscribeProxy<T>.toLiveData(): LiveData<T> {
val liveData = MutableLiveData<T>()
this.subscribeIgnoreError {
liveData.postValue(it)
}
return liveData
}
fun <T> MaybeSubscribeProxy<T>.toLiveData(): LiveData<T> {
val liveData = MutableLiveData<T>()
this.subscribeIgnoreError {
liveData.postValue(it)
}
return liveData
}
//----------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------
fun <T> ObservableSubscribeProxy<T>.subscribeWithLiveData(liveData: MutableLiveData<Resource<T>>) { fun <T> ObservableSubscribeProxy<T>.subscribeWithLiveData(liveData: MutableLiveData<Resource<T>>) {
@ -152,6 +120,18 @@ fun CompletableSubscribeProxy.subscribeWithLiveData(liveData: MutableLiveData<Re
) )
} }
fun <T> CompletableSubscribeProxy.subscribeWithLiveData(liveData: MutableLiveData<Resource<T>>, provider: () -> T) {
liveData.postValue(Resource.loading())
this.subscribe(
{
liveData.postValue(Resource.success(provider()))
},
{
liveData.postValue(Resource.error(it))
}
)
}
//----------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------
fun <T> ObservableSubscribeProxy<T>.toResourceLiveData(): LiveData<Resource<T>> { fun <T> ObservableSubscribeProxy<T>.toResourceLiveData(): LiveData<Resource<T>> {
@ -223,3 +203,37 @@ fun CompletableSubscribeProxy.toResourceLiveData(): LiveData<Resource<Any>> {
) )
return mutableLiveData return mutableLiveData
} }
//-----------------------------------------------------------------------------------------
fun <T> ObservableSubscribeProxy<T>.toLiveData(): LiveData<T> {
val liveData = MutableLiveData<T>()
this.subscribeIgnoreError {
liveData.postValue(it)
}
return liveData
}
fun <T> FlowableSubscribeProxy<T>.toLiveData(): LiveData<T> {
val liveData = MutableLiveData<T>()
this.subscribeIgnoreError {
liveData.postValue(it)
}
return liveData
}
fun <T> SingleSubscribeProxy<T>.toLiveData(): LiveData<T> {
val liveData = MutableLiveData<T>()
this.subscribeIgnoreError {
liveData.postValue(it)
}
return liveData
}
fun <T> MaybeSubscribeProxy<T>.toLiveData(): LiveData<T> {
val liveData = MutableLiveData<T>()
this.subscribeIgnoreError {
liveData.postValue(it)
}
return liveData
}

@ -120,6 +120,18 @@ fun Completable.subscribeWithLiveData(liveData: MutableLiveData<Resource<Any>>)
) )
} }
fun <T> Completable.subscribeWithLiveData(liveData: MutableLiveData<Resource<T>>, provider: () -> T) {
liveData.postValue(Resource.loading())
this.subscribe(
{
liveData.postValue(Resource.success(provider()))
},
{
liveData.postValue(Resource.error(it))
}
)
}
//----------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------
fun <T> Observable<T>.toResourceLiveData(): LiveData<Resource<T>> { fun <T> Observable<T>.toResourceLiveData(): LiveData<Resource<T>> {

@ -86,7 +86,7 @@ fun <T : Fragment> FragmentManager.findFragmentByTag(clazz: KClass<T>): T? {
return findFragmentByTag(clazz.java.name) as? T return findFragmentByTag(clazz.java.name) as? T
} }
fun FragmentManager.popBackTo(flag: String, immediate: Boolean = false) { fun FragmentManager.popBackUntil(flag: String, immediate: Boolean = false) {
if (immediate) { if (immediate) {
popBackStackImmediate(flag, FragmentManager.POP_BACK_STACK_INCLUSIVE) popBackStackImmediate(flag, FragmentManager.POP_BACK_STACK_INCLUSIVE)
} else { } else {
@ -94,6 +94,14 @@ fun FragmentManager.popBackTo(flag: String, immediate: Boolean = false) {
} }
} }
fun FragmentManager.popBackTo(flag: String, immediate: Boolean = false) {
if (immediate) {
popBackStackImmediate(flag, 0)
} else {
popBackStack(flag,0)
}
}
fun FragmentManager.clearBackStack(immediate: Boolean = false) { fun FragmentManager.clearBackStack(immediate: Boolean = false) {
if (immediate) { if (immediate) {
this.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE) this.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)

@ -120,7 +120,7 @@ final class RefreshableStateLayoutImpl implements RefreshStateLayout, StateLayou
@Override @Override
public void showServerErrorLayout() { public void showServerErrorLayout() {
refreshCompleted(); refreshCompleted();
checkMultiStateView().showNetErrorLayout(); checkMultiStateView().showServerErrorLayout();
} }
@Override @Override

@ -148,8 +148,14 @@ fun RefreshStateLayout.processErrorWithStatus(throwable: Throwable?) {
val errorTypeClassifier = BaseKit.get().errorClassifier() val errorTypeClassifier = BaseKit.get().errorClassifier()
if (errorTypeClassifier != null) { if (errorTypeClassifier != null) {
when { when {
errorTypeClassifier.isNetworkError(throwable) -> showNetErrorLayout() errorTypeClassifier.isNetworkError(throwable) -> {
errorTypeClassifier.isServerError(throwable) -> showServerErrorLayout() Timber.d("isNetworkError showNetErrorLayout")
showNetErrorLayout()
}
errorTypeClassifier.isServerError(throwable) -> {
Timber.d("isServerError showServerErrorLayout")
showServerErrorLayout()
}
else -> showErrorLayout() else -> showErrorLayout()
} }
} else { } else {

@ -2,6 +2,18 @@ package com.android.base.kotlin
import com.android.base.utils.common.CollectionUtils import com.android.base.utils.common.CollectionUtils
fun <T> List<T>?.toArrayList(): ArrayList<T> { fun <E> List<E>?.toArrayList(): ArrayList<E> {
return CollectionUtils.toArrayList(this) return CollectionUtils.toArrayList(this)
} }
fun <E> MutableList<E>.removeWhich(filter: (E) -> Boolean): Boolean {
var removed = false
val each = iterator()
while (each.hasNext()) {
if (filter(each.next())) {
each.remove()
removed = true
}
}
return removed
}

@ -1,6 +1,5 @@
package com.android.base.widget; package com.android.base.widget;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -8,6 +7,7 @@ import android.support.annotation.Nullable;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.SparseArray; import android.util.SparseArray;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.FrameLayout; import android.widget.FrameLayout;
@ -32,7 +32,7 @@ public class MultiStateView extends FrameLayout {
private LayoutInflater mInflater; private LayoutInflater mInflater;
private SparseArray<ViewHolder> mChildren; private SparseArray<ViewHolder> mChildren;
private View mContentView; private View mContentView;
private boolean mDisableOperationWhenRequesting = true; private boolean mDisableOperationWhenRequesting = false;
@Nullable @Nullable
private StateListener mListener; private StateListener mListener;
@ -64,7 +64,7 @@ public class MultiStateView extends FrameLayout {
int emptyViewResId = a.getResourceId(R.styleable.MultiStateView_msv_emptyView, -1); int emptyViewResId = a.getResourceId(R.styleable.MultiStateView_msv_emptyView, -1);
int errorViewResId = a.getResourceId(R.styleable.MultiStateView_msv_errorView, -1); int errorViewResId = a.getResourceId(R.styleable.MultiStateView_msv_errorView, -1);
int netErrorViewResId = a.getResourceId(R.styleable.MultiStateView_msv_net_errorView, -1); int netErrorViewResId = a.getResourceId(R.styleable.MultiStateView_msv_net_errorView, -1);
int serverErrorViewResId = a.getResourceId(R.styleable.MultiStateView_msv_net_errorView, -1); int serverErrorViewResId = a.getResourceId(R.styleable.MultiStateView_msv_server_errorView, -1);
mChildren.put(LOADING, new ViewHolder(loadingViewResId)); mChildren.put(LOADING, new ViewHolder(loadingViewResId));
mChildren.put(REQUESTING, new ViewHolder(requestingViewResId)); mChildren.put(REQUESTING, new ViewHolder(requestingViewResId));
@ -73,7 +73,7 @@ public class MultiStateView extends FrameLayout {
mChildren.put(NET_ERROR, new ViewHolder(netErrorViewResId)); mChildren.put(NET_ERROR, new ViewHolder(netErrorViewResId));
mChildren.put(SERVER_ERROR, new ViewHolder(serverErrorViewResId)); mChildren.put(SERVER_ERROR, new ViewHolder(serverErrorViewResId));
mDisableOperationWhenRequesting = a.getBoolean(R.styleable.MultiStateView_msv_disable_when_requesting, true); mDisableOperationWhenRequesting = a.getBoolean(R.styleable.MultiStateView_msv_disable_when_requesting, false);
ensureInitState(a.getInt(R.styleable.MultiStateView_msv_viewState, CONTENT)); ensureInitState(a.getInt(R.styleable.MultiStateView_msv_viewState, CONTENT));
@ -272,25 +272,35 @@ public class MultiStateView extends FrameLayout {
View curStateView = ensureStateView(mViewState); View curStateView = ensureStateView(mViewState);
int size = mChildren.size(); int size = mChildren.size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
ViewHolder viewHolder = mChildren.valueAt(i); ViewHolder viewHolder = mChildren.valueAt(i);
if (viewHolder.mView == null) { if (viewHolder.mView == null) {
continue; continue;
} }
if (viewHolder.mView != curStateView) { if (viewHolder.mView != curStateView) {
if (mViewState == REQUESTING && viewHolder.mView != mContentView) { if (mViewState == REQUESTING && viewHolder.mView == mContentView) {
viewHolder.mView.setVisibility(GONE); viewHolder.mView.setVisibility(VISIBLE);
} else { } else {
viewHolder.mView.setVisibility(GONE); viewHolder.mView.setVisibility(GONE);
} }
} }
} }
curStateView.setVisibility(VISIBLE); curStateView.setVisibility(VISIBLE);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
if (mViewState == REQUESTING) { if (mViewState == REQUESTING) {
curStateView.setOnTouchListener(mDisableOperationWhenRequesting ? NO_ACTION_TOUCH_LISTENER : null); return mDisableOperationWhenRequesting;
} else {
return false;
} }
}
return super.onInterceptTouchEvent(ev);
} }
/** /**
@ -322,17 +332,6 @@ public class MultiStateView extends FrameLayout {
public void setDisableOperationWhenRequesting(boolean disableOperationWhenRequesting) { public void setDisableOperationWhenRequesting(boolean disableOperationWhenRequesting) {
mDisableOperationWhenRequesting = disableOperationWhenRequesting; mDisableOperationWhenRequesting = disableOperationWhenRequesting;
ViewHolder viewHolder = mChildren.get(LOADING);
if (viewHolder == null || viewHolder.mView == null) {
return;
}
if (disableOperationWhenRequesting) {
if (mViewState == REQUESTING) {
viewHolder.mView.setOnTouchListener(NO_ACTION_TOUCH_LISTENER);
}
} else {
viewHolder.mView.setOnTouchListener(null);
}
} }
/** /**
@ -361,18 +360,15 @@ public class MultiStateView extends FrameLayout {
void onStateInflated(@ViewState int viewState, @NonNull View view); void onStateInflated(@ViewState int viewState, @NonNull View view);
} }
@SuppressLint("ClickableViewAccessibility")
private static final OnTouchListener NO_ACTION_TOUCH_LISTENER = (v, event) -> true;
private class ViewHolder { private class ViewHolder {
private View mView; private View mView;
private int mViewLayoutId; private int mViewLayoutId;
public ViewHolder(int viewLayoutId) { ViewHolder(int viewLayoutId) {
this(null, viewLayoutId); this(null, viewLayoutId);
} }
public ViewHolder(View view, int viewLayoutId) { ViewHolder(View view, int viewLayoutId) {
mView = view; mView = view;
mViewLayoutId = viewLayoutId; mViewLayoutId = viewLayoutId;
} }

@ -4,7 +4,6 @@
<item name="base_item_tag_view_id" type="id"/> <item name="base_item_tag_view_id" type="id"/>
<item name="base_tag_multi_state_view" type="id"/> <item name="base_tag_multi_state_view" type="id"/>
<item name="base_retry_tv" type="id"/><!--重试text--> <item name="base_retry_tv" type="id"/><!--重试text-->
<item name="base_retry_icon" type="id"/><!--重试icon--> <item name="base_retry_icon" type="id"/><!--重试icon-->
<item name="base_retry_btn" type="id"/><!--重试btn--> <item name="base_retry_btn" type="id"/><!--重试btn-->
@ -14,5 +13,4 @@
<item name="base_state_layout" type="id"/><!--状态布局控件,如multiStatusView--> <item name="base_state_layout" type="id"/><!--状态布局控件,如multiStatusView-->
<item name="common_toolbar" type="id"/><!--toolbar--> <item name="common_toolbar" type="id"/><!--toolbar-->
</resources> </resources>
Loading…
Cancel
Save