Android:Recyclerview onBindViewHolder和Snaphelper

时间:2018-08-30 04:28:42

标签: android android-recyclerview

我有一个附加了Recyclerview的PagerSnapHelper,当我滚动Recyclerview 时,onBindViewHolder被称为 1次,但是当我滚动Recyclerview时 fast ,onBindViewHolder连续 2次。滚动更改时,如何仅1次使适配器调用 onBindViewHolder ?对不起,我的英语

1 个答案:

答案 0 :(得分:1)

您可以看到我的主要代码:

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
    }
  }
 }

这是我的方法。我不使用recyclerview,而是使用普通视图:imageview,textview等。并让用户可以滚动,我使用onTouch事件自定义滚动。

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是滚动结果的接口。收到滚动结果后,我将调用api,重新加载数据并更新视图。我之所以使用这种方式,是因为我的视图是分页器视图,同时它只显示1页。如果与普通列表一起使用,它将毫无用处。如果您有更好的方法,请与我分享。