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

The following examples show how to use android.support.v7.widget.RecyclerView#post() . 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: ScrollManager.java    From Recyclerview-Gallery with Apache License 2.0 6 votes vote down vote up
/**
 * 垂直滑动
 *
 * @param recyclerView RecyclerView
 * @param dy           int
 */
private void onVerticalScroll(final RecyclerView recyclerView, int dy) {
    mConsumeY += dy;

    // 让RecyclerView测绘完成后再调用,避免GalleryAdapterHelper.mItemHeight的值拿不到
    recyclerView.post(new Runnable() {
        @Override
        public void run() {
            int shouldConsumeY = mGalleryRecyclerView.getDecoration().mItemConsumeY;

            // 位置浮点值(即总消耗距离 / 每一页理论消耗距离 = 一个浮点型的位置值)
            float offset = (float) mConsumeY / (float) shouldConsumeY;
            // 获取当前页移动的百分值
            float percent = offset - ((int) offset);

            mPosition = (int) offset;

            DLog.i(TAG, "ScrollManager offset=" + offset + "; mConsumeY=" + mConsumeY + "; shouldConsumeY=" + mPosition);


            // 设置动画变化
            mGalleryRecyclerView.getAnimManager().setAnimation(recyclerView, mPosition, percent);
        }
    });
}
 
Example 2
Source File: ScrollManager.java    From Recyclerview-Gallery with Apache License 2.0 6 votes vote down vote up
/**
 * 水平滑动
 *
 * @param recyclerView RecyclerView
 * @param dx           int
 */
private void onHorizontalScroll(final RecyclerView recyclerView, final int dx) {
    mConsumeX += dx;

    // 让RecyclerView测绘完成后再调用,避免GalleryAdapterHelper.mItemWidth的值拿不到
    recyclerView.post(new Runnable() {
        @Override
        public void run() {
            int shouldConsumeX = mGalleryRecyclerView.getDecoration().mItemConsumeX;

            // 位置浮点值(即总消耗距离 / 每一页理论消耗距离 = 一个浮点型的位置值)
            float offset = (float) mConsumeX / (float) shouldConsumeX;

            // 获取当前页移动的百分值
            float percent = offset - ((int) offset);

            mPosition = (int) offset;

            DLog.i(TAG, "ScrollManager offset=" + offset + "; percent=" + percent + "; mConsumeX=" + mConsumeX + "; shouldConsumeX=" + shouldConsumeX + "; position=" + mPosition);

            // 设置动画变化
            mGalleryRecyclerView.getAnimManager().setAnimation(recyclerView, mPosition, percent);
        }
    });

}
 
Example 3
Source File: ScrollingUtil.java    From AgentWebX5 with Apache License 2.0 5 votes vote down vote up
public static void scrollToBottom(final RecyclerView recyclerView) {
    if (recyclerView != null) {
        if (recyclerView.getAdapter() != null && recyclerView.getAdapter().getItemCount() > 0) {
            recyclerView.post(new Runnable() {
                @Override
                public void run() {
                    recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount() - 1);
                }
            });
        }
    }
}
 
Example 4
Source File: SheetSongListController.java    From Musicoco with Apache License 2.0 5 votes vote down vote up
public void initViews() {
    random = (ImageView) activity.findViewById(R.id.sheet_detail_songs_icon);
    playAllRandom = (TextView) activity.findViewById(R.id.sheet_detail_songs_play_random);
    line = activity.findViewById(R.id.sheet_detail_songs_line);
    songList = (RecyclerView) activity.findViewById(R.id.sheet_detail_songs_list);
    randomContainer = activity.findViewById(R.id.sheet_detail_random_container);

    sortTip = (TextView) activity.findViewById(R.id.sheet_detail_songs_sort_tip);

    checkContainer = activity.findViewById(R.id.sheet_detail_check_container);
    checkAll = (CheckBox) activity.findViewById(R.id.sheet_detail_check_all);
    checkCount = (TextView) activity.findViewById(R.id.sheet_detail_check_count);
    checkAll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isChecked = checkAll.isChecked();
            if (isChecked) {
                songAdapter.checkAll();
            } else {
                songAdapter.clearAllCheck();
            }
        }
    });

    randomContainer.setOnClickListener(this);
    FloatingActionButton fabPlayAll = (FloatingActionButton) activity.findViewById(R.id.sheet_detail_play_all);
    fabPlayAll.setOnClickListener(this);

    songList.post(new Runnable() {
        @Override
        public void run() {
            calculateRecycleViewHeight();
        }
    });
}
 
Example 5
Source File: GridStyleActivity.java    From ZoomPreviewPicture with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    setContentView(R.layout.activity_recycler);
    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    mRvPostLister = (RecyclerView) findViewById(R.id.rv_post_list);
    final LinearLayoutManager manager = new LinearLayoutManager(this);
    mRvPostLister.setLayoutManager(manager);
    mPostList = new ArrayList<>();
    for (int i = 0; i < 29; i++) {
        List<UserViewInfo> imgUrls = new ArrayList<>();
        UserViewInfo userViewInfo;
        Random ss=new Random();

        for (int j = 0; j <ss.nextInt(9); j++) {
            userViewInfo=new UserViewInfo(     ImageUrlConfig.getUrls().get(j));
            imgUrls.add(userViewInfo);
        }
        Post post = new Post("Am I handsome? Am I handsome? Am I handsome?", imgUrls);
        mPostList.add(post);
    }

    mNineImageAdapter = new PostAdapter(this, mPostList, NineGridImageView.STYLE_GRID);
    mRvPostLister.setAdapter(mNineImageAdapter);
    manager.scrollToPositionWithOffset(5, 0);
    mRvPostLister.post(new Runnable() {
        @Override
        public void run() {
            View view = manager.findViewByPosition(1);
            if (view != null) System.out.println(view.getMeasuredHeight());
        }
    });
}
 
Example 6
Source File: TangramEngine.java    From Tangram-Android with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void refresh(final boolean layoutUpdated) {
    final RecyclerView contentView = getContentView();

    if (contentView == null) {
        return;
    }

    if (contentView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
        // contentView.stopScroll();
    }

    updateRunnable = new Runnable() {
        @Override
        public void run() {
            if (!contentView.isComputingLayout()) {
                //to prevent notify update when recyclerView is in computingLayout  process
                mGroupBasicAdapter.notifyUpdate(layoutUpdated);

                if (mSwipeItemTouchListener != null) {
                    mSwipeItemTouchListener.updateCurrCard();
                }
            }
        }
    };
    contentView.post(updateRunnable);
}
 
Example 7
Source File: TangramEngine.java    From Tangram-Android with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void refresh(final boolean layoutUpdated) {
    final RecyclerView contentView = getContentView();

    if (contentView == null) {
        return;
    }

    if (contentView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
        // contentView.stopScroll();
    }

    updateRunnable = new Runnable() {
        @Override
        public void run() {
            if (!contentView.isComputingLayout()) {
                //to prevent notify update when recyclerView is in computingLayout  process
                mGroupBasicAdapter.notifyUpdate(layoutUpdated);

                if (mSwipeItemTouchListener != null) {
                    mSwipeItemTouchListener.updateCurrCard();
                }
            }
        }
    };
    contentView.post(updateRunnable);
}
 
Example 8
Source File: GalleryItemDecoration.java    From Recyclerview-Gallery with Apache License 2.0 5 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, final View view, final RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);

    DLog.d(TAG, "GalleryItemDecoration getItemOffset() --> position = " + parent.getChildAdapterPosition(view));

    final int position = parent.getChildAdapterPosition(view);
    final int itemCount = parent.getAdapter().getItemCount();

    parent.post(new Runnable() {
        @Override
        public void run() {
            if (((GalleryRecyclerView) parent).getOrientation() == LinearLayoutManager.HORIZONTAL) {
                onSetHorizontalParams(parent, view, position, itemCount);
            } else {
                onSetVerticalParams(parent, view, position, itemCount);
            }
        }
    });

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(v, position);
            }
        }
    });
}
 
Example 9
Source File: GridStyleActivity.java    From NineGridImageView with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    setContentView(R.layout.activity_recycler);
    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

    mRvPostLister = (RecyclerView) findViewById(R.id.rv_post_list);
    final LinearLayoutManager manager = new LinearLayoutManager(this);
    mRvPostLister.setLayoutManager(manager);

    mPostList = new ArrayList<>();
    for (int i = 0; i < 18; i++) {
        List<String> imgUrls = new ArrayList<>();
        imgUrls.addAll(Arrays.asList(IMG_URL_LIST).subList(0, i % 9));
        Post post = new Post("Am I handsome? Am I handsome? Am I handsome?", imgUrls);
        mPostList.add(post);
    }

    mNineImageAdapter = new PostAdapter(this, mPostList, NineGridImageView.STYLE_GRID);
    mRvPostLister.setAdapter(mNineImageAdapter);

    manager.scrollToPositionWithOffset(5, 0);
    mRvPostLister.post(new Runnable() {
        @Override
        public void run() {
            View view = manager.findViewByPosition(1);
            if (view != null) System.out.println(view.getMeasuredHeight());
        }
    });
}
 
Example 10
Source File: ScrollingUtil.java    From TwinklingRefreshLayout with Apache License 2.0 5 votes vote down vote up
public static void scrollToBottom(final RecyclerView recyclerView) {
    if (recyclerView != null) {
        if (recyclerView.getAdapter() != null && recyclerView.getAdapter().getItemCount() > 0) {
            recyclerView.post(new Runnable() {
                @Override
                public void run() {
                    recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount() - 1);
                }
            });
        }
    }
}
 
Example 11
Source File: SpacesItemDecoration.java    From boxing with Apache License 2.0 4 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, final RecyclerView parent, RecyclerView.State state) {
    RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
    final int sumCount = state.getItemCount();
    final int position = params.getViewLayoutPosition();
    final int spanSize;
    final int index;

    if (params instanceof GridLayoutManager.LayoutParams) {
        GridLayoutManager.LayoutParams gridParams = (GridLayoutManager.LayoutParams) params;
        spanSize = gridParams.getSpanSize();
        index = gridParams.getSpanIndex();

        if ((position == 0 || mOldItemCount != sumCount) && mSpanCount > 1) {
            int countInLine = 0;
            int spanIndex;

            for (int tempPosition = sumCount - mSpanCount; tempPosition < sumCount; tempPosition++) {
                spanIndex = ((GridLayoutManager) parent.getLayoutManager()).getSpanSizeLookup().getSpanIndex(tempPosition, mSpanCount);
                countInLine = spanIndex == 0 ? 1 : countInLine + 1;
            }
            mItemCountInLastLine = countInLine;
            if (mOldItemCount != sumCount) {
                mOldItemCount = sumCount;
                if (position != 0) {
                    parent.post(new Runnable() {
                        @Override
                        public void run() {
                            parent.invalidateItemDecorations();
                        }
                    });
                }
            }
        }
    } else if (params instanceof StaggeredGridLayoutManager.LayoutParams) {
        spanSize = ((StaggeredGridLayoutManager.LayoutParams) params).isFullSpan() ? mSpanCount : 1;
        index = ((StaggeredGridLayoutManager.LayoutParams) params).getSpanIndex();
    } else {
        spanSize = 1;
        index = 0;
    }

    if (spanSize < 1 || index < 0 || spanSize > mSpanCount) {
        return;
    }

    outRect.left = mSpace - mRadixX * index;
    outRect.right = mRadixX + mRadixX * (index + spanSize - 1);

    if (mSpanCount == 1 && position == sumCount - 1) {
        outRect.bottom = mSpace;
    } else if (position >= sumCount - mItemCountInLastLine && position < sumCount) {
        outRect.bottom = mSpace;
    }
    outRect.top = mSpace;
}
 
Example 12
Source File: BottomSheetAdapterBuilder.java    From BottomSheetBuilder with Apache License 2.0 4 votes vote down vote up
@SuppressLint("InflateParams")
public View createView(int titleTextColor, int backgroundDrawable, int backgroundColor,
                       int dividerBackground, int itemTextColor, int itemBackground,
                       int tintColor, BottomSheetItemClickListener itemClickListener) {

    if (mFromMenu) {
        mItems = createAdapterItems(dividerBackground, titleTextColor,
                itemTextColor, itemBackground, tintColor);
    }

    LayoutInflater layoutInflater = LayoutInflater.from(mContext);

    View sheet = mMode == BottomSheetBuilder.MODE_GRID ?
            layoutInflater.inflate(R.layout.bottomsheetbuilder_sheet_grid, null)
            : layoutInflater.inflate(R.layout.bottomsheetbuilder_sheet_list, null);

    final RecyclerView recyclerView = (RecyclerView) sheet.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);

    if (backgroundDrawable != 0) {
        sheet.setBackgroundResource(backgroundDrawable);
    } else {
        if (backgroundColor != 0) {
            sheet.setBackgroundColor(backgroundColor);
        }
    }

    // If we only have one title and it's the first item, set it as fixed
    if (mTitles == 1 && mMode == BottomSheetBuilder.MODE_LIST) {
        BottomSheetItem header = mItems.get(0);
        TextView headerTextView = (TextView) sheet.findViewById(R.id.textView);
        if (header instanceof BottomSheetHeader) {
            headerTextView.setVisibility(View.VISIBLE);
            headerTextView.setText(header.getTitle());
            if (titleTextColor != 0) {
                headerTextView.setTextColor(titleTextColor);
            }
            mItems.remove(0);
        }
    }

    final BottomSheetItemAdapter adapter = new BottomSheetItemAdapter(mItems, mMode,
            itemClickListener);

    if (mMode == BottomSheetBuilder.MODE_LIST) {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);
    } else {
        final int columns = mContext.getResources().getInteger(R.integer.bottomsheet_grid_columns);
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, columns);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                float margin = mContext.getResources()
                        .getDimensionPixelSize(R.dimen.bottomsheet_grid_horizontal_margin);
                adapter.setItemWidth((int) ((recyclerView.getWidth() - 2 * margin) / columns));
                recyclerView.setAdapter(adapter);
            }
        });
    }

    return sheet;
}