Java Code Examples for android.support.v7.widget.RecyclerView#getParent()

The following examples show how to use android.support.v7.widget.RecyclerView#getParent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RecyclerViewHeader.java    From Android-Application-ZJB with Apache License 2.0 7 votes vote down vote up
private void setupAlignment(RecyclerView recycler) {
    if (!mAlreadyAligned) {
        //setting alignment of header
        ViewGroup.LayoutParams currentParams = getLayoutParams();
        FrameLayout.LayoutParams newHeaderParams;
        int width = ViewGroup.LayoutParams.WRAP_CONTENT;
        int height = ViewGroup.LayoutParams.WRAP_CONTENT;
        int gravity = (mReversed ? Gravity.BOTTOM : Gravity.TOP) | Gravity.CENTER_HORIZONTAL;
        if (currentParams != null) {
            newHeaderParams = new FrameLayout.LayoutParams(getLayoutParams()); //to copy all the margins
            newHeaderParams.width = width;
            newHeaderParams.height = height;
            newHeaderParams.gravity = gravity;
        } else {
            newHeaderParams = new FrameLayout.LayoutParams(width, height, gravity);
        }
        RecyclerViewHeader.this.setLayoutParams(newHeaderParams);

        //setting alignment of recycler
        FrameLayout newRootParent = new FrameLayout(recycler.getContext());
        newRootParent.setLayoutParams(recycler.getLayoutParams());
        ViewParent currentParent = recycler.getParent();
        if (currentParent instanceof ViewGroup) {
            int indexWithinParent = ((ViewGroup) currentParent).indexOfChild(recycler);

            ((ViewGroup) currentParent).removeViewAt(indexWithinParent);
            recycler.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            newRootParent.addView(recycler);
            newRootParent.addView(RecyclerViewHeader.this);
            ((ViewGroup) currentParent).addView(newRootParent, indexWithinParent);
        }
    }
}
 
Example 2
Source File: RecyclerViewAdapter.java    From MVVM-JueJin with MIT License 5 votes vote down vote up
/**
 * (伪)双向 databinding: 自动调用 {@link TwoWayListVM#getLoadTask()},
 *      并自动触发 {@link TwoWayListVM#setData(ObservableArrayList)}
 *      然后自动更新 RecyclerView
 *
 * @param container
 * @param vm
 * @param datas
 * @param <T>
 */
@BindingAdapter({"vm", "data"})
public static <T> void setDataTwoWay(final RecyclerView container, final ListVM<T> vm, List<T> datas){
    if(vm == null){
        return ;
    }
    setData(container, vm, datas);

    if(vm instanceof TwoWayListVM) {
        boolean isInited = container.getTag(R.id.db_inited) != null;
        if (!isInited) {
            container.setTag(R.id.db_inited, true);

            final TwoWayListVM<T> _vm = ((TwoWayListVM<T>) vm);

            loadData(container, _vm, null, null);
            // 若 parent 可下拉刷新,设置回调
            ViewParent parent = container.getParent();
            if (parent != null && parent instanceof TwoWayListVM.Refreshable) {
                final TwoWayListVM.Refreshable refreshable = (TwoWayListVM.Refreshable) parent;
                ((TwoWayListVM.Refreshable) parent).setOnRefresh(new TwoWayListVM.Refreshable.CallBack() {
                    @Override
                    public void onRefresh() {
                        loadData(container, _vm, null, refreshable);
                    }

                    @Override
                    public void onLoadMore() {
                        List<T> data = _vm.getData();
                        if (data.size() - 1 >= 0) {
                            loadData(container, _vm, data.get(data.size() - 1), refreshable);
                        }
                    }
                });
            }
        }
    }
}
 
Example 3
Source File: RecyclerViewHeader.java    From Android-Application-ZJB with Apache License 2.0 5 votes vote down vote up
private void validateRecycler(RecyclerView recycler, boolean headerAlreadyAligned) {
    RecyclerView.LayoutManager layoutManager = recycler.getLayoutManager();
    if (layoutManager == null) {
        throw new IllegalStateException("Be sure to call RecyclerViewHeader constructor after setting your RecyclerView's LayoutManager.");
    } else if (layoutManager.getClass() != LinearLayoutManager.class    //not using instanceof on purpose
            && layoutManager.getClass() != GridLayoutManager.class
            && !(layoutManager instanceof StaggeredGridLayoutManager)) {
        throw new IllegalArgumentException("Currently RecyclerViewHeader supports only LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager.");
    }

    if (layoutManager instanceof LinearLayoutManager) {
        if (((LinearLayoutManager) layoutManager).getOrientation() != LinearLayoutManager.VERTICAL) {
            throw new IllegalArgumentException("Currently RecyclerViewHeader supports only VERTICAL orientation LayoutManagers.");
        }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        if (((StaggeredGridLayoutManager) layoutManager).getOrientation() != StaggeredGridLayoutManager.VERTICAL) {
            throw new IllegalArgumentException("Currently RecyclerViewHeader supports only VERTICAL orientation StaggeredGridLayoutManagers.");
        }
    }

    if (!headerAlreadyAligned) {
        ViewParent parent = recycler.getParent();
        if (parent != null &&
                !(parent instanceof LinearLayout) &&
                !(parent instanceof FrameLayout) &&
                !(parent instanceof RelativeLayout)) {
            throw new IllegalStateException("Currently, NOT already aligned RecyclerViewHeader " +
                    "can only be used for RecyclerView with a parent of one of types: LinearLayout, FrameLayout, RelativeLayout.");
        }
    }
}
 
Example 4
Source File: BoardView.java    From fingerpoetry-android with Apache License 2.0 5 votes vote down vote up
private int getCurrentColumn(float posX) {
    for (int i = 0; i < mLists.size(); i++) {
        RecyclerView list = mLists.get(i);
        View parent = (View) list.getParent();
        if (parent.getLeft() <= posX && parent.getRight() > posX) {
            return i;
        }
    }
    return 0;
}