Java Code Examples for android.support.v7.widget.LinearLayoutManager#smoothScrollToPosition()

The following examples show how to use android.support.v7.widget.LinearLayoutManager#smoothScrollToPosition() . 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: RecyclerViewScrollActivity.java    From AndroidDemo with MIT License 6 votes vote down vote up
private void moveToPosition(int position) {
    if (position > data.size())
        return;
    LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    int first = layoutManager.findFirstVisibleItemPosition();
    int end = layoutManager.findLastVisibleItemPosition();
    if (first == -1 || end == -1)
        return;
    if (position <= first) {
        layoutManager.scrollToPosition(position);
    } else if (position >= end) {
        isMove = true;
        scrollPosition = position;
        layoutManager.smoothScrollToPosition(recyclerView, null, position);
    } else {//中间部分
        int n = position - layoutManager.findFirstVisibleItemPosition();
        if (n > 0 && n < data.size()) {
            int top = layoutManager.findViewByPosition(position).getTop();
            recyclerView.scrollBy(0, top);
        }
    }
}
 
Example 2
Source File: WidgetsRecyclerView.java    From Trebuchet with GNU General Public License v3.0 6 votes vote down vote up
@Override
public String scrollToSection(String sectionName) {
    // Skip early if widgets are not bound.
    if (mWidgets == null) {
        return "";
    }

    // Skip early if there are no widgets.
    int rowCount = mWidgets.getPackageSize();
    if (rowCount == 0) {
        return "";
    }
    for (int i = 0; i < rowCount; i++) {
        PackageItemInfo packageItemInfo = mWidgets.getPackageItemInfo(i);
        if (packageItemInfo != null && !TextUtils.isEmpty(packageItemInfo.titleSectionName) &&
                packageItemInfo.titleSectionName.equals(sectionName)) {
            LinearLayoutManager layoutManager = ((LinearLayoutManager) getLayoutManager());
            layoutManager.smoothScrollToPosition(this, null, i);
            return packageItemInfo.titleSectionName;
        }
    }
    return null;
}