Skip to content

Commit

Permalink
fix for multiple type item add or insertDataObject
Browse files Browse the repository at this point in the history
  • Loading branch information
binhbt committed Dec 25, 2017
1 parent fd8d763 commit 415e146
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private void processOnMore() {
&& !isLoadingMore) {

isLoadingMore = true;
if (mOnMoreListener != null) {
if (mOnMoreListener != null && totalItemCount >0) {
mMoreProgress.setVisibility(View.VISIBLE);
mOnMoreListener.onMoreAsked(mRecycler.getAdapter().getItemCount(), ITEM_LEFT_TO_LOAD_MORE, lastVisibleItemPosition);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vn.fa.adapter.multipleviewtype;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.ViewGroup;

/**
Expand All @@ -12,14 +13,20 @@ abstract public class DataBindAdapter extends RecyclerView.Adapter<RecyclerView.

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Log.e("onCreateViewHolder", "viewType "+viewType);
return getDataBinder(viewType).newViewHolder(parent);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
int binderPosition = getBinderPosition(position);
getDataBinder(viewHolder.getItemViewType()).bindViewHolder(viewHolder, binderPosition);
//getDataBinder(viewHolder.getItemViewType()).bindViewHolder(viewHolder, binderPosition);
//getDataBinder(position).bindViewHolder(viewHolder, position);
//Log.e("onBindViewHolder", "viewType "+viewHolder.getItemViewType());
getDataBinderByPosition(position).bindViewHolder(viewHolder, binderPosition);

}
public abstract <T extends DataBinder> T getDataBinderByPosition(int position);

@Override
public abstract int getItemCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ public final void notifyBinderItemRangeRemoved(int positionStart, int itemCount)
mDataBindAdapter.notifyBinderItemRangeRemoved(this, positionStart, itemCount);
}
*/
public abstract int getItemViewType();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vn.fa.adapter.multipleviewtype;

import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -27,24 +29,36 @@ public int size(){
}
@Override
public int getItemViewType(int position) {
int itemCount = 0;
/* int itemCount = 0;
for (int viewType = 0, size = mBinderList.size(); viewType < size; viewType++) {
itemCount += mBinderList.get(viewType).getItemCount();
if (position < itemCount) {
return viewType;
}
}
throw new IllegalArgumentException("arg position is invalid");
throw new IllegalArgumentException("arg position is invalid");*/
return mBinderList.get(position).getItemViewType();
}
public <T extends DataBinder> T getDataBinderByPosition(int position){
return (T) mBinderList.get(position);
}

@Override
public <T extends DataBinder> T getDataBinder(int viewType) {
return (T) mBinderList.get(viewType);
//Log.e("size", "data size "+ mBinderList.size());
for (DataBinder dataBinder:mBinderList
) {
if (dataBinder.getItemViewType() == viewType){
return (T)dataBinder;
}
}
return (T) mBinderList.get(0);
//return (T) mBinderList.get(viewType);
}

@Override
public int getPosition(DataBinder binder, int binderPosition) {
int viewType = mBinderList.indexOf(binder);
/* int viewType = mBinderList.indexOf(binder);
if (viewType < 0) {
throw new IllegalStateException("binder does not exist in adapter");
}
Expand All @@ -54,7 +68,8 @@ public int getPosition(DataBinder binder, int binderPosition) {
position += mBinderList.get(i).getItemCount();
}
return position;
return position;*/
return binderPosition;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ public VegaBindAdapter listData(List<DataBinder> dataSet){
this.mBinderList = dataSet;
return this;
}
// public void addBinderData(List<DataBinder> dataSet){
// if (dataSet != null){
// addAllBinder(dataSet);
// notifyDataSetChanged();
// }
// }

public List<DataBinder> getBinderList(){
return this.mBinderList;
}
Expand Down Expand Up @@ -56,6 +51,21 @@ public void addAllDataObject(List<IViewBinder> dataset, boolean isAdapterAttache
addAll(convertDataToViewBinder(dataset, isAdapterAttached));
}
}
public void insertDataObject(IViewBinder object, int position){
VegaDataBinder iViewBinder = (VegaDataBinder) object.getViewBinder();
if (object != null){
insert(iViewBinder,position);
}
}
public void insertDataObject(IViewBinder object, int position, boolean isAdapterAttached){
VegaDataBinder iViewBinder = (VegaDataBinder) object.getViewBinder();
if (isAdapterAttached){
iViewBinder.adapter(this);
}
if (object != null){
insert(iViewBinder,position);
}
}
public void add(DataBinder binder) {
insert(binder, mBinderList.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
abstract public class VegaDataBinder<T> extends DataBinder<BinderViewHolder> implements IViewBinder{
protected T data;
protected DataBindAdapter mDataBindAdapter;
protected int itemViewType =0;
public VegaDataBinder(T data){
this.data = data;
}
Expand Down Expand Up @@ -45,5 +46,12 @@ public DataBinder getViewBinder(){
return this;
}


@Override
public int getItemViewType(){
return itemViewType;
}
public VegaDataBinder itemViewType(int itemViewType){
this.itemViewType = itemViewType;
return this;
}
}
13 changes: 8 additions & 5 deletions library/src/main/java/com/vn/fa/widget/RecyclerViewWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ private void transferAttrsToSuperRecyclerView(boolean mClipToPadding, int mPaddi
mMoreProgressId, mProgressId);

}
public void setProgressInCenter(boolean isShow){
isProgressInCenter = isShow;
}
public boolean isProgressInCenter(){
return isProgressInCenter;
}
public void disableRefresh(){
mRecycler.disableRefresh();
}
Expand Down Expand Up @@ -383,10 +389,7 @@ public void setHorizontalScrollBarEnabled(boolean isEnabled){
public void setVerticalScrollBarEnabled(boolean isEnabled){
getRecyclerView().setVerticalScrollBarEnabled(isEnabled);
}
public void setProgressInCenter(boolean isShow){
isProgressInCenter = isShow;
}
public boolean isProgressInCenter(){
return isProgressInCenter;
public void showEmptyView(boolean isShow){
mRecycler.getEmptyStub().setVisibility(isShow?View.VISIBLE:View.GONE);
}
}

0 comments on commit 415e146

Please sign in to comment.