Android:Recyclerview onBindViewHolder和Snaphelper

问题描述 投票:0回答:1

我有一个Recyclerview附加了PagerSnapHelper,当我滚动Recyclerview慢,onBindViewHolder被调用1次,但是当我快速滚动Recyclerview时,onBindViewHolder被连续2次调用。当滚动更改时,如何才能在StickViewHolder上调用适配器调用一次?对不起我的英语不好

android android-recyclerview
1个回答
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页。如果你使用普通列表,它将是无用的。如果您有更好的方法,请与我分享。

© www.soinside.com 2019 - 2024. All rights reserved.