Search in sources :

Example 1 with LVRecyclerView

use of com.taobao.luaview.view.LVRecyclerView in project LuaViewSDK by alibaba.

the class UDBaseRecyclerView method pinned.

// TODO: 11/15/16 处理itemView之前有spacing的情况
private void pinned(LVRecyclerView lvRecyclerView) {
    if (!mHasPinnedCell)
        return;
    if (mPinnedContainer == null) {
        mPinnedContainer = new FrameLayout(lvRecyclerView.getContext());
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        ViewGroup parent = (ViewGroup) lvRecyclerView.getParent();
        if (parent instanceof LVRefreshRecyclerView) {
            // RefreshCollectionView
            params.leftMargin = (int) parent.getX();
            params.topMargin = (int) parent.getY();
            ((ViewGroup) parent.getParent()).addView(mPinnedContainer, params);
        } else {
            // CollectionView
            params.leftMargin = (int) lvRecyclerView.getX();
            params.topMargin = (int) lvRecyclerView.getY();
            parent.addView(mPinnedContainer, params);
        }
    }
    int firstVisiblePosition = findFirstVisiblePosition(lvRecyclerView.getLayoutManager());
    // 从firstVisiblePosition位置开始递减查找上一个pinned position
    int pinnedViewPosition = findPinnedViewPositionDecrease(firstVisiblePosition);
    if (pinnedViewPosition >= 0 && mCurrentPinnedPosition != pinnedViewPosition) {
        ViewGroup itemView = (ViewGroup) mPinnedPositionHolder.get(pinnedViewPosition).itemView;
        View child = itemView.getChildAt(0);
        if (child != null) {
            // 从itemView移除child之前,先设置其与child一样的宽高占位。
            itemView.getLayoutParams().width = child.getLayoutParams().width;
            itemView.getLayoutParams().height = child.getLayoutParams().height;
            itemView.removeView(child);
            mPinnedContainer.addView(child);
            if (mCurrentPinnedView != null) {
                mCurrentPinnedView.setVisibility(View.GONE);
            }
            mCurrentPinnedView = child;
        } else {
            // 从(pinnedViewPosition + 1)位置开始递增查找下一个pinned position
            int nextPinnedPosition = findPinnedViewPositionIncrease(pinnedViewPosition + 1);
            ViewGroup parentItemView = (ViewGroup) mPinnedPositionHolder.get(nextPinnedPosition).itemView;
            View pinnedView = mPinnedContainer.getChildAt(mPinnedContainer.getChildCount() - 1);
            mPinnedContainer.removeView(pinnedView);
            parentItemView.addView(pinnedView);
            mCurrentPinnedView = mPinnedContainer.getChildAt(mPinnedContainer.getChildCount() - 1);
            mCurrentPinnedView.setVisibility(View.VISIBLE);
        }
        mCurrentPinnedPosition = pinnedViewPosition;
    }
    // 第一个吸顶视图被移除的情况,亦即列表恢复没有吸顶视图的状态。
    if (pinnedViewPosition == -1 && mCurrentPinnedPosition != -1) {
        View subview = mPinnedContainer.getChildAt(mPinnedContainer.getChildCount() - 1);
        mPinnedContainer.removeView(subview);
        // 从position 0开始找第一个pinned标记的itemView,并把最后一个吸顶视图添加回到它的原本位置
        int firstPinnedPosition = findPinnedViewPositionIncrease(0);
        ViewGroup parentItemView = (ViewGroup) mPinnedPositionHolder.get(firstPinnedPosition).itemView;
        parentItemView.addView(subview);
        // 列表恢复没有吸顶视图的状态
        mCurrentPinnedPosition = -1;
        mCurrentPinnedView = null;
    }
    // 处理吸顶视图切换时的位移效果
    if (mPinnedContainer != null && mCurrentPinnedPosition != -1) {
        View targetView = lvRecyclerView.findChildViewUnder(mPinnedContainer.getMeasuredWidth() / 2, mPinnedContainer.getMeasuredHeight() + 1);
        if (targetView != null) {
            boolean isPinned = ((Boolean) targetView.getTag(Constants.RES_LV_TAG_PINNED)).booleanValue();
            if (isPinned && targetView.getTop() > 0) {
                if (pinnedViewPosition != -1) {
                    int deltaY = targetView.getTop() - mPinnedContainer.getMeasuredHeight();
                    if (deltaY < (lvRecyclerView.getMiniSpacing() - mPinnedContainer.getMeasuredHeight())) {
                        // 防止设置了spacing的时候,在这个范围内mPinnedContainer被位移到top之上,而itemView是空白的现象
                        mPinnedContainer.setTranslationY(0);
                    } else {
                        mPinnedContainer.setTranslationY(deltaY);
                    }
                }
            } else {
                mPinnedContainer.setTranslationY(0);
            }
        } else {
            mPinnedContainer.setTranslationY(0);
        }
    }
}
Also used : LVRefreshRecyclerView(com.taobao.luaview.view.LVRefreshRecyclerView) ViewGroup(android.view.ViewGroup) FrameLayout(android.widget.FrameLayout) RelativeLayout(android.widget.RelativeLayout) LVRefreshRecyclerView(com.taobao.luaview.view.LVRefreshRecyclerView) AbsListView(android.widget.AbsListView) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) LVRecyclerView(com.taobao.luaview.view.LVRecyclerView)

Example 2 with LVRecyclerView

use of com.taobao.luaview.view.LVRecyclerView in project LuaViewSDK by alibaba.

the class UDBaseRecyclerView method reload.

/**
     * notify data changed (section, row) in java
     *
     * @param section
     * @param row
     * @return
     */
@Override
public UDBaseRecyclerView reload(Integer section, Integer row) {
    restore();
    final LVRecyclerView recyclerView = getLVRecyclerView();
    if (recyclerView != null) {
        final RecyclerView.Adapter adapter = recyclerView.getLVAdapter();
        if (adapter != null) {
            int diffSectionCount = getDiffSectionCount();
            if (section == null || diffSectionCount != 0) {
                //如果 section无值,或者section数量变动则更新所有
                refreshState(recyclerView);
                adapter.notifyDataSetChanged();
            } else {
                //如果传了section,row,则表示要更新部分数据
                //total count diff
                int diffTotalCount = getDiffTotalCount();
                //数据变更,数量未变更
                boolean isChanged = diffTotalCount == 0;
                //数量增加
                boolean isInserted = diffTotalCount > 0;
                //数量减少
                boolean isRemoved = diffTotalCount < 0;
                if (row == null) {
                    //row is null, notify whole section
                    int start = getPositionBySectionAndRow(section, 0);
                    int currentRowCount = getRowCount(section);
                    if (isChanged) {
                        //更新整个section,count不变,数据变
                        refreshState(recyclerView);
                        adapter.notifyItemRangeChanged(start, currentRowCount);
                    } else if (isInserted) {
                        //更新整个section,count增加
                        int newRowCount = getRawRowCount(section);
                        //新增count
                        int count = Math.abs(newRowCount - currentRowCount);
                        refreshState(recyclerView);
                        adapter.notifyItemRangeInserted(start, count);
                    } else if (isRemoved) {
                        //更新整个section,count减少
                        int newRowCount = getRawRowCount(section);
                        //新增count
                        int count = Math.abs(newRowCount - currentRowCount);
                        refreshState(recyclerView);
                        adapter.notifyItemRangeRemoved(start, count);
                    }
                } else {
                    //row not null, notify row
                    int pos = getPositionBySectionAndRow(section, row);
                    refreshState(recyclerView);
                    if (isChanged) {
                        //更新某个元素
                        adapter.notifyItemChanged(pos);
                    } else if (isInserted) {
                        //插入一个元素
                        adapter.notifyItemInserted(pos);
                    } else if (isRemoved) {
                        //减少一个元素
                        adapter.notifyItemRemoved(pos);
                    }
                }
            }
        }
    }
    return this;
}
Also used : LVRecyclerView(com.taobao.luaview.view.LVRecyclerView) LVRefreshRecyclerView(com.taobao.luaview.view.LVRefreshRecyclerView) RecyclerView(android.support.v7.widget.RecyclerView) LVRecyclerView(com.taobao.luaview.view.LVRecyclerView)

Example 3 with LVRecyclerView

use of com.taobao.luaview.view.LVRecyclerView in project LuaViewSDK by alibaba.

the class UDBaseRecyclerView method initOnScrollCallback.

@Override
public void initOnScrollCallback(final T view) {
    if (view instanceof LVRecyclerView) {
        final LVRecyclerView lvRecyclerView = (LVRecyclerView) view;
        if (LuaUtil.isValid(mCallback) || mLazyLoad) {
            lvRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
                    updateAllChildScrollState(recyclerView, scrollState);
                    if (LuaUtil.isValid(mCallback)) {
                        switch(scrollState) {
                            case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                                {
                                    final int itemPosition = lvRecyclerView.getFirstVisiblePosition();
                                    final int section = getSectionByPosition(itemPosition);
                                    final int row = getRowInSectionByPosition(itemPosition);
                                    LuaUtil.callFunction(LuaUtil.getFunction(mCallback, "ScrollBegin", "scrollBegin"), LuaUtil.toLuaInt(section), LuaUtil.toLuaInt(row));
                                    break;
                                }
                            case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                                break;
                            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                                {
                                    final int itemPosition = lvRecyclerView.getFirstVisiblePosition();
                                    final int section = getSectionByPosition(itemPosition);
                                    final int row = getRowInSectionByPosition(itemPosition);
                                    LuaUtil.callFunction(LuaUtil.getFunction(mCallback, "ScrollEnd", "scrollEnd"), LuaUtil.toLuaInt(section), LuaUtil.toLuaInt(row));
                                    break;
                                }
                        }
                    }
                }

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    if (LuaUtil.isValid(mCallback)) {
                        final int itemPosition = lvRecyclerView.getFirstVisiblePosition();
                        final int section = getSectionByPosition(itemPosition);
                        final int row = getRowInSectionByPosition(itemPosition);
                        LuaUtil.callFunction(LuaUtil.getFunction(mCallback, "Scrolling", "scrolling"), LuaUtil.toLuaInt(section), LuaUtil.toLuaInt(row), valueOf(lvRecyclerView.getVisibleItemCount()));
                    }
                    pinned(lvRecyclerView);
                }
            });
        }
    }
}
Also used : LVRecyclerView(com.taobao.luaview.view.LVRecyclerView) LVRefreshRecyclerView(com.taobao.luaview.view.LVRefreshRecyclerView) RecyclerView(android.support.v7.widget.RecyclerView) LVRecyclerView(com.taobao.luaview.view.LVRecyclerView)

Example 4 with LVRecyclerView

use of com.taobao.luaview.view.LVRecyclerView in project LuaViewSDK by alibaba.

the class UDView method setEnabled.

/**
     * 设置是否有效
     *
     * @param enable
     * @return
     */
public UDView setEnabled(boolean enable) {
    final View view = getView();
    if (view != null) {
        if (view instanceof LVRecyclerView) {
            LVRecyclerView view2 = (LVRecyclerView) view;
            view2.setNestedScrollingEnabled(false);
            return this;
        }
        view.setEnabled(enable);
    }
    return this;
}
Also used : LVRecyclerView(com.taobao.luaview.view.LVRecyclerView) LVImageView(com.taobao.luaview.view.LVImageView) View(android.view.View) LVRecyclerView(com.taobao.luaview.view.LVRecyclerView)

Aggregations

LVRecyclerView (com.taobao.luaview.view.LVRecyclerView)4 RecyclerView (android.support.v7.widget.RecyclerView)3 LVRefreshRecyclerView (com.taobao.luaview.view.LVRefreshRecyclerView)3 View (android.view.View)2 ViewGroup (android.view.ViewGroup)1 AbsListView (android.widget.AbsListView)1 FrameLayout (android.widget.FrameLayout)1 RelativeLayout (android.widget.RelativeLayout)1 LVImageView (com.taobao.luaview.view.LVImageView)1