當前位置: 首頁>>代碼示例>>Java>>正文


Java RecyclerView.scrollToPosition方法代碼示例

本文整理匯總了Java中android.support.v7.widget.RecyclerView.scrollToPosition方法的典型用法代碼示例。如果您正苦於以下問題:Java RecyclerView.scrollToPosition方法的具體用法?Java RecyclerView.scrollToPosition怎麽用?Java RecyclerView.scrollToPosition使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.support.v7.widget.RecyclerView的用法示例。


在下文中一共展示了RecyclerView.scrollToPosition方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: scrollToRightPositionWhenItemChanged

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
/**
     * 當item位置變換,滾動recycler到正確的位置
     * TODO: 2017/2/21 0021 整理更優雅的寫法  還有scrollToPosition(0)是否必要?
     */
    private void scrollToRightPositionWhenItemChanged(RecyclerView recyclerView, View itemView, int itemPos) {
        final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof ItemTouchHelper.ViewDropHandler) {
            OrientationHelper helper = OrientationHelper.createVerticalHelper(layoutManager);
            int start = helper.getDecoratedStart(itemView);
            int end = helper.getDecoratedEnd(itemView);
            ((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(
                    itemPos, lastItemPos > itemPos ? start : end - itemViewHeight);
//                System.out.println(lastItemPos + "-" + childPos + "OrientationHelperOrientationHelper:"
//                        + height + "==" + itemViewHeight + "=||=" + start + "===" + end + "||||||" + myStart + "===" + itemTargetView.getHeight() );
        }
        if (lastItemPos == 0 || itemPos == 0) {
            recyclerView.scrollToPosition(0);
        }
    }
 
開發者ID:free46000,項目名稱:MultiItem,代碼行數:20,代碼來源:ItemDragHelper.java

示例2: onMoved

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
public void onMoved(RecyclerView recyclerView, ViewHolder viewHolder, int fromPos, ViewHolder target, int toPos, int x, int y) {
    LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof ViewDropHandler) {
        ((ViewDropHandler) layoutManager).prepareForDrop(viewHolder.itemView, target.itemView, x, y);
        return;
    }
    if (layoutManager.canScrollHorizontally()) {
        if (layoutManager.getDecoratedLeft(target.itemView) <= recyclerView.getPaddingLeft()) {
            recyclerView.scrollToPosition(toPos);
        }
        if (layoutManager.getDecoratedRight(target.itemView) >= recyclerView.getWidth() - recyclerView.getPaddingRight()) {
            recyclerView.scrollToPosition(toPos);
        }
    }
    if (layoutManager.canScrollVertically()) {
        if (layoutManager.getDecoratedTop(target.itemView) <= recyclerView.getPaddingTop()) {
            recyclerView.scrollToPosition(toPos);
        }
        if (layoutManager.getDecoratedBottom(target.itemView) >= recyclerView.getHeight() - recyclerView.getPaddingBottom()) {
            recyclerView.scrollToPosition(toPos);
        }
    }
}
 
開發者ID:JackChan1999,項目名稱:letv,代碼行數:24,代碼來源:ItemTouchHelper.java

示例3: syncOffset

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
public static boolean syncOffset(SmoothAppBarLayout smoothAppBarLayout, View target, int verticalOffset, View scroll) {
  boolean isSelected = target == scroll;
  if (scroll instanceof NestedScrollView) {
    NestedScrollView nestedScrollView = (NestedScrollView) scroll;
    if (nestedScrollView.getScrollY() < verticalOffset || (!isSelected && isScrollToTop(target))) {
      nestedScrollView.scrollTo(0, verticalOffset);
    }
    if (isSelected && (nestedScrollView.getScrollY() < verticalOffset || verticalOffset == 0)) {
      nestedScrollView.scrollTo(0, 0);
      smoothAppBarLayout.syncOffset(0);
    }
  } else if (scroll instanceof RecyclerView) {
    RecyclerView recyclerView = (RecyclerView) scroll;
    boolean isAccuracy = recyclerView.getLayoutManager().findViewByPosition(ObservableRecyclerView.HEADER_VIEW_POSITION) != null;
    if (isAccuracy && recyclerView.computeVerticalScrollOffset() < verticalOffset) {
      recyclerView.scrollBy(0, verticalOffset - recyclerView.computeVerticalScrollOffset());
    } else if (!isSelected && isScrollToTop(target)) {
      recyclerView.scrollToPosition(ObservableRecyclerView.HEADER_VIEW_POSITION);
    }
    if (isAccuracy && isSelected && (recyclerView.computeVerticalScrollOffset() < verticalOffset || verticalOffset == 0)) {
      recyclerView.scrollToPosition(ObservableRecyclerView.HEADER_VIEW_POSITION);
      smoothAppBarLayout.syncOffset(0);
    }
  }
  return true;
}
 
開發者ID:PacktPublishing,項目名稱:Expert-Android-Programming,代碼行數:27,代碼來源:Utils.java

示例4: scrollToTop

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
public void scrollToTop(RecyclerView recyclerView) {
    if (null == recyclerView) return;
    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
    int firstVisibleItemPosition = 0;
    if (manager instanceof LinearLayoutManager) {
        firstVisibleItemPosition = getFirstVisibleItemPosition((LinearLayoutManager) manager);
    } else if (manager instanceof StaggeredGridLayoutManager) {
        firstVisibleItemPosition = getFirstVisibleItemPosition((StaggeredGridLayoutManager) manager);
    }
    if (firstVisibleItemPosition > 3) {
        recyclerView.scrollToPosition(0);
    } else {
        backToFinish(recyclerView);
    }
}
 
開發者ID:alphater,項目名稱:garras,代碼行數:16,代碼來源:BaseFragment.java

示例5: onCreateView

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.recyclerview_environmentalsensor,
            container, false);
    rootView.setTag(TAG);

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview_environmentalsensor);

    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.scrollToPosition(0);

    mListAdapter = new EnvironmentalSensorsListAdapter(mSensors,
            new EnvironmentalSensorsListAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(EnvironmentalSensor item) {
            mCallbacks.onItemSelected(item.getAddress());
        }
    });
    // Set CustomAdapter as the adapter for RecyclerView.
    mRecyclerView.setAdapter(mListAdapter);

    mViewModel = EnvironmentalSensorsActivity.obtainViewModel(getActivity());

    return rootView;
}
 
開發者ID:igrow-systems,項目名稱:igrow-android,代碼行數:29,代碼來源:EnvironmentalSensorsFragment.java

示例6: setFooterViewState

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
/**
 * 設置headerAndFooterAdapter的FooterView State
 *
 * @param context      context
 * @param recyclerView  recyclerView
 * @param pageSize      分頁展示時,recyclerView每一頁的數量
 * @param state         FooterView State
 * @param errorListener FooterView處於Error狀態時的點擊事件
 */
public static void setFooterViewState(Context context, RecyclerView recyclerView, int pageSize, LoadingFooter.State state, View.OnClickListener errorListener) {

    RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();

    if (outerAdapter == null || !(outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter)) {
        return;
    }

    HeaderAndFooterRecyclerViewAdapter headerAndFooterAdapter = (HeaderAndFooterRecyclerViewAdapter) outerAdapter;

    //隻有一頁的時候,就別加什麽FooterView了
    if (headerAndFooterAdapter.getInnerAdapter().getItemCount() < pageSize) {
        return;
    }

    LoadingFooter footerView;

    //已經有footerView了
    if (headerAndFooterAdapter.getFooterViewsCount() > 0) {
        footerView = (LoadingFooter) headerAndFooterAdapter.getFooterView();
        footerView.setState(state);

        if (state == LoadingFooter.State.NetWorkError) {
            footerView.setOnClickListener(errorListener);
        }
        recyclerView.scrollToPosition(headerAndFooterAdapter.getItemCount() - 1);
    } else {
        footerView = new LoadingFooter(context);
        footerView.setState(state);

        if (state == LoadingFooter.State.NetWorkError) {
            footerView.setOnClickListener(errorListener);
        }

        headerAndFooterAdapter.addFooterView(footerView);
        recyclerView.scrollToPosition(headerAndFooterAdapter.getItemCount() - 1);
    }
}
 
開發者ID:yinyiliang,項目名稱:RabbitCloud,代碼行數:48,代碼來源:RecyclerViewStateUtils.java

示例7: initView

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
@Override
    public void initView(View rootView) {
        mOnLoadMoreListener = new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                mPresenter.loadMoreDatas();
//                mAdapter.showFooter();
                mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1);
            }
        };
        mSwipRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swiperefreshlayout);
        mSwipRefreshLayout.setColorSchemeResources(R.color.colorArticle, R.color.colorIHistory, R.color.colorMore);
        mSwipRefreshLayout.setOnRefreshListener(this);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (mCurrentState == STATE_MORE_LOADED && RecyclerView.SCROLL_STATE_IDLE == newState && calculateRecyclerViewFirstPosition() == mRecyclerView.getAdapter().getItemCount() - 1 && mOnLoadMoreListener != null) {
                    mOnLoadMoreListener.loadMore();
                    mCurrentState = STATE_MORE_LOADING;
                }
            }
        });

        mCircleLoadingView = (CircleLoadingView) rootView.findViewById(R.id.circle_loading);
        mCircleLoadingView.setDrawableColor(R.color.colorAccent);
        mPresenter = new IPhotoListPresenterImpl(this, mPhotoId, 1);
    }
 
開發者ID:shenhuanet,項目名稱:OpenEyesReading-android,代碼行數:30,代碼來源:PhotoListFrag.java

示例8: onCreateView

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
  Bundle args = getArguments();
  photoSize = args.getInt(IMAGE_SIZE_KEY);
  thumbnail = args.getBoolean(THUMBNAIL_KEY);

  fullRequest = GlideApp.with(this)
      .asDrawable()
      .centerCrop()
      .transition(withNoTransition());

  thumbnailRequest = GlideApp.with(this)
      .asDrawable()
      .diskCacheStrategy(DiskCacheStrategy.DATA)
      .centerCrop()
      .override(Api.SQUARE_THUMB_SIZE)
      .transition(withNoTransition());

  preloadRequest =
      thumbnail ? thumbnailRequest.clone().priority(Priority.HIGH) : fullRequest;

  final View result = inflater.inflate(R.layout.flickr_photo_grid, container, false);

  final int gridMargin = getResources().getDimensionPixelOffset(R.dimen.grid_margin);
  int spanCount = getResources().getDisplayMetrics().widthPixels / (photoSize + (2 * gridMargin));
  grid = (RecyclerView) result.findViewById(R.id.flickr_photo_grid);
  layoutManager = new GridLayoutManager(getActivity(), spanCount);
  grid.setLayoutManager(layoutManager);

  grid.addItemDecoration(new RecyclerView.ItemDecoration() {
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
        RecyclerView.State state) {
      outRect.set(gridMargin, gridMargin, gridMargin, gridMargin);
    }
  });
  grid.setRecyclerListener(new RecyclerView.RecyclerListener() {
    @Override
    public void onViewRecycled(RecyclerView.ViewHolder holder) {
      PhotoViewHolder photoViewHolder = (PhotoViewHolder) holder;
      GlideApp.with(FlickrPhotoGrid.this).clear(photoViewHolder.imageView);
    }
  });

  int heightCount = getResources().getDisplayMetrics().heightPixels / photoSize;
  grid.getRecycledViewPool().setMaxRecycledViews(0, spanCount * heightCount * 2);
  grid.setItemViewCacheSize(0);
  adapter = new PhotoAdapter();
  grid.setAdapter(adapter);

  FixedPreloadSizeProvider<Photo> preloadSizeProvider =
      new FixedPreloadSizeProvider<>(photoSize, photoSize);
  RecyclerViewPreloader<Photo> preloader = new RecyclerViewPreloader<>(Glide.with(this), adapter,
      preloadSizeProvider, args.getInt(PRELOAD_KEY));
  grid.addOnScrollListener(preloader);

  if (currentPhotos != null) {
    adapter.setPhotos(currentPhotos);
  }

  if (savedInstanceState != null) {
    int index = savedInstanceState.getInt(STATE_POSITION_INDEX);
    grid.scrollToPosition(index);
  }

  return result;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:69,代碼來源:FlickrPhotoGrid.java

示例9: onCreateView

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
@Override
public View onCreateView(
    @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  Bundle args = Preconditions.checkNotNull(getArguments());
  photoSize = args.getInt(IMAGE_SIZE_KEY);
  thumbnail = args.getBoolean(THUMBNAIL_KEY);

  fullRequest = GlideApp.with(this)
      .asDrawable()
      .centerCrop();

  thumbnailRequest = GlideApp.with(this)
      .asDrawable()
      .centerCrop()
      .override(Api.SQUARE_THUMB_SIZE);

  preloadRequest =
      thumbnail ? thumbnailRequest.clone().priority(Priority.HIGH) : fullRequest;

  final View result = inflater.inflate(R.layout.flickr_photo_grid, container, false);

  final int gridMargin = getResources().getDimensionPixelOffset(R.dimen.grid_margin);
  int spanCount = getResources().getDisplayMetrics().widthPixels / (photoSize + (2 * gridMargin));
  grid = (RecyclerView) result.findViewById(R.id.flickr_photo_grid);
  layoutManager = new GridLayoutManager(getActivity(), spanCount);
  grid.setLayoutManager(layoutManager);

  grid.addItemDecoration(new RecyclerView.ItemDecoration() {
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
        RecyclerView.State state) {
      outRect.set(gridMargin, gridMargin, gridMargin, gridMargin);
    }
  });
  grid.setRecyclerListener(new RecyclerView.RecyclerListener() {
    @Override
    public void onViewRecycled(RecyclerView.ViewHolder holder) {
      PhotoViewHolder photoViewHolder = (PhotoViewHolder) holder;
      GlideApp.with(FlickrPhotoGrid.this).clear(photoViewHolder.imageView);
    }
  });

  int heightCount = getResources().getDisplayMetrics().heightPixels / photoSize;
  grid.getRecycledViewPool().setMaxRecycledViews(0, spanCount * heightCount * 2);
  grid.setItemViewCacheSize(0);
  adapter = new PhotoAdapter();
  grid.setAdapter(adapter);

  FixedPreloadSizeProvider<Photo> preloadSizeProvider =
      new FixedPreloadSizeProvider<>(photoSize, photoSize);
  RecyclerViewPreloader<Photo> preloader = new RecyclerViewPreloader<>(Glide.with(this), adapter,
      preloadSizeProvider, args.getInt(PRELOAD_KEY));
  grid.addOnScrollListener(preloader);

  if (currentPhotos != null) {
    adapter.setPhotos(currentPhotos);
  }

  if (savedInstanceState != null) {
    int index = savedInstanceState.getInt(STATE_POSITION_INDEX);
    grid.scrollToPosition(index);
  }

  return result;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:66,代碼來源:FlickrPhotoGrid.java

示例10: checkSelectedItemVisibility

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
private void checkSelectedItemVisibility(RecyclerView listView, int position) {
    listView.scrollToPosition(position);
}
 
開發者ID:Tamicer,項目名稱:FilterBar,代碼行數:4,代碼來源:RegionContainer.java

示例11: perform

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
public void perform(UiController uiController, View view) {
    RecyclerView recyclerView = (RecyclerView) view;
    recyclerView.scrollToPosition(this.position);
}
 
開發者ID:graviton57,項目名稱:DOUSalaries,代碼行數:5,代碼來源:TestUtils.java

示例12: clear

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
public void clear(RecyclerView recyclerView) {
    onDataSetChangeListener.onClearAll();
    displayList.clear();
    notifyDataSetChanged();
    recyclerView.scrollToPosition(0);
}
 
開發者ID:TellH,項目名稱:RecyclerStickyHeaderView,代碼行數:7,代碼來源:StickyHeaderViewAdapter.java

示例13: setAdapterOnRecyclerView

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
@Override
protected void setAdapterOnRecyclerView(@NonNull RecyclerView recyclerView) {
    super.setAdapterOnRecyclerView(recyclerView);
    recyclerView.scrollToPosition(positionToScroll);
}
 
開發者ID:wulkanowy,項目名稱:wulkanowy,代碼行數:6,代碼來源:TimetableFragmentTab.java

示例14: onAttachedToRecyclerView

import android.support.v7.widget.RecyclerView; //導入方法依賴的package包/類
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    //-3 for position the last clicked item below to the top of the view
    recyclerView.scrollToPosition(mSelectedPosition >= 3 ? mSelectedPosition - 3 : 0);
}
 
開發者ID:mkeresztes,項目名稱:AndiCar,代碼行數:6,代碼來源:BaseViewAdapter.java


注:本文中的android.support.v7.widget.RecyclerView.scrollToPosition方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。