简体   繁体   中英

Android: Recyclerview onBindViewHolder and Snaphelper

I have a Recyclerview is attached PagerSnapHelper, when i scroll Recyclerview slow , onBindViewHolder is called 1 time , but when i scroll Recyclerview fast , onBindViewHolder is called 2 time consecutive. How can i make Adapter calls onBindViewHolder only 1 time when scrolling changed?. Sorry for my english

You can see my primary code:

public class DetailActiveVoucherAdapter extends RecyclerView.Adapter<DetailActiveVoucherAdapter.DetailViewHolder> {

// Constructor

// Var
private DetailViewHolder detailHolder; // i put viewholder here for using it after call api

private DataHolder<EvoucherDetailModel> dataHolderDetail;
private DataHolder<EVoucherSummary> dataHolderSummary;

public DetailActiveVoucherAdapter(...) {
    // set constructor
}

@Override
public DetailViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(activity).inflate(R.layout.row_detail_active_voucher, parent, false);
    return new DetailViewHolder(view);
}

@Override
public void onBindViewHolder(final DetailViewHolder holder, final int position) {
    detailHolder = (DetailViewHolder) holder;
    currentPosition = position;
    holder.bind(); /**TODO: this is my problem */
/* problem is onBindViewHolder called 2 times successive. when i call holder.bind() i will call api, after calling api done, i show data on View
 * But second onBindViewHolder is called, and current View is view of second position and first view has nothing
 */
}

...

private void loadData(long id, final long pos) {
    DataLoader.getCashBoughtEvoucherDetail((LixiActivity) activity, new ApiCallBack() {
        @Override
        public void handleCallback(boolean isSuccess, Object object) {
            if (isSuccess) {
                detailHolder.evoucherModel = (EvoucherDetailModel) object;
                loadTotalandPriceVoucher(pos);
            } else {
                Helper.showErrorDialog((LixiActivity) activity, (String) object);
            }
        }
    }, id);
}

private void loadTotalandPriceVoucher(final long pos) {
    DataLoader.getEVoucherSummary((LixiActivity) activity, new ApiCallBack() {
        @Override
        public void handleCallback(boolean isSuccess, Object object) {
            if (onLoadingListener != null){
                onLoadingListener.onEnd();
            }
            if (isSuccess) {
                detailHolder.eVoucherSummary = (EVoucherSummary) object;
                showData((int) pos);
            }
        }
    }, detailHolder.evoucherModel.getMerchant_info().getId());
}

private void showData(int pos) {
    // show data
}

public class DetailViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    // View

// this is 2 object i get from api
    EvoucherDetailModel evoucherModel;
    EVoucherSummary eVoucherSummary;

    public DetailViewHolder(View itemView) {
        super(itemView);
        // reference view
    }

    public void bind(){
        load();
        // set event
    }
  }
 }

And this is my method. I don't use recyclerview, i just use a normal view: imageview, textview, ect. And let user can scroll, i custom the scrolling with onTouch event.

linearDrag.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (canSwipe) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        cx = getWidth() / 2 - linearDrag.getWidth() / 2;
                        privotXLeft = getWidth() / 4 - linearDrag.getWidth() / 2;
                        privotXRight = (getWidth() / 4) * 3 - linearDrag.getWidth() / 2;
                        privotXCenter = linearDrag.getX();
                        x = motionEvent.getX();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        changeX = motionEvent.getX();
                        linearDrag.setX(linearDrag.getX() + changeX - x);
                        break;
                    default:
                        // back to center
                        if ((linearDrag.getX() >= privotXCenter && linearDrag.getX() <= privotXRight) || (linearDrag.getX() >= privotXLeft && linearDrag.getX() <= privotXCenter)) {
                            linearDrag.setX(cx);
                            break;
                        }

                        // to next position
                        if (linearDrag.getX() <= privotXLeft) {
                            if (swipeNext) {
                                linearDrag.setX(-1 * getWidth());
                                if (onActionChangedListener != null) {
                                    onActionChangedListener.onChanged(NEXT);
                                    return true;
                                }
                            } else {
                                linearDrag.setX(cx);
                            }
                            break;
                        }

                        // to previous position
                        if (linearDrag.getX() >= privotXRight) {
                            if (swipePrev) {
                                linearDrag.setX(getWidth());
                                if (onActionChangedListener != null) {
                                    onActionChangedListener.onChanged(PREV);
                                    return true;
                                }
                            } else {
                                linearDrag.setX(cx);
                            }
                            break;
                        }
                        break;
                }
                return true;
            } else {
                return false;
            }
        }
    });

onActionChangedListener is interface for scroll result. After i received scroll result i will call api, reload data and update my view. I use this way because my view is pager view, at the same time it only display 1 page. If you use with normal list, it will be useless. If you have a better method please share with me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM