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, boolean isSuccessive);
//get
T getItem(int position);

@ -136,6 +136,11 @@ public abstract class BaseListAdapter<T, VH extends ViewHolder> extends BaseAdap
mDataManager.removeItems(elements);
}
@Override
public void removeItems(List<T> elements, boolean isSuccessive) {
mDataManager.removeItems(elements, isSuccessive);
}
@Override
public void removeAt(int 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
public void removeAt(int index) {
if (mData != null && mData.size() > index) {

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

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

@ -112,6 +112,11 @@ public abstract class RecyclerAdapter<T, VH extends ViewHolder> extends Recycler
mDataManager.removeItems(elements);
}
@Override
public void removeItems(List<T> elements, boolean isSuccessive) {
mDataManager.removeItems(elements, isSuccessive);
}
@Override
public void removeAt(int 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
public T getItem(int position) {
position = position - getHeaderSize();
@ -241,6 +260,10 @@ final class RecyclerDataManagerImpl<T> implements DataManager<T> {
mAdapter.notifyItemRemoved(index);
}
private void notifyItemRangeRemoved(int index, int size) {
mAdapter.notifyItemRangeRemoved(index, size);
}
void setHeaderSize(HeaderSize headerSize) {
mHeaderSize = headerSize;
}

@ -8,38 +8,6 @@ import com.github.dmstocking.optional.java.util.Optional
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>>) {
@ -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>> {
@ -223,3 +203,37 @@ fun CompletableSubscribeProxy.toResourceLiveData(): LiveData<Resource<Any>> {
)
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>> {

@ -86,7 +86,7 @@ fun <T : Fragment> FragmentManager.findFragmentByTag(clazz: KClass<T>): 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) {
popBackStackImmediate(flag, FragmentManager.POP_BACK_STACK_INCLUSIVE)
} 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) {
if (immediate) {
this.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)

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

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

@ -2,6 +2,18 @@ package com.android.base.kotlin
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)
}
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;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
@ -8,6 +7,7 @@ import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
@ -32,7 +32,7 @@ public class MultiStateView extends FrameLayout {
private LayoutInflater mInflater;
private SparseArray<ViewHolder> mChildren;
private View mContentView;
private boolean mDisableOperationWhenRequesting = true;
private boolean mDisableOperationWhenRequesting = false;
@Nullable
private StateListener mListener;
@ -64,7 +64,7 @@ public class MultiStateView extends FrameLayout {
int emptyViewResId = a.getResourceId(R.styleable.MultiStateView_msv_emptyView, -1);
int errorViewResId = a.getResourceId(R.styleable.MultiStateView_msv_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(REQUESTING, new ViewHolder(requestingViewResId));
@ -73,7 +73,7 @@ public class MultiStateView extends FrameLayout {
mChildren.put(NET_ERROR, new ViewHolder(netErrorViewResId));
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));
@ -272,25 +272,35 @@ public class MultiStateView extends FrameLayout {
View curStateView = ensureStateView(mViewState);
int size = mChildren.size();
for (int i = 0; i < size; i++) {
ViewHolder viewHolder = mChildren.valueAt(i);
if (viewHolder.mView == null) {
continue;
}
if (viewHolder.mView != curStateView) {
if (mViewState == REQUESTING && viewHolder.mView != mContentView) {
viewHolder.mView.setVisibility(GONE);
if (mViewState == REQUESTING && viewHolder.mView == mContentView) {
viewHolder.mView.setVisibility(VISIBLE);
} else {
viewHolder.mView.setVisibility(GONE);
}
}
}
curStateView.setVisibility(VISIBLE);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
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) {
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);
}
@SuppressLint("ClickableViewAccessibility")
private static final OnTouchListener NO_ACTION_TOUCH_LISTENER = (v, event) -> true;
private class ViewHolder {
private View mView;
private int mViewLayoutId;
public ViewHolder(int viewLayoutId) {
ViewHolder(int viewLayoutId) {
this(null, viewLayoutId);
}
public ViewHolder(View view, int viewLayoutId) {
ViewHolder(View view, int viewLayoutId) {
mView = view;
mViewLayoutId = viewLayoutId;
}

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