public override void OnScrolled(RecyclerView recyclerView, int dx, int dy) { base.OnScrolled(recyclerView, dx, dy); //var visibleItemCount = recyclerView.ChildCount; //var totalItemCount = recyclerView.GetAdapter().ItemCount; //var pastVisiblesItems = LayoutManager.FindFirstVisibleItemPosition(); //if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) //{ // LoadMoreEvent(this, null); // loading = true; //} totalItemCount = linearLayoutManager.ItemCount; lastVisibleItem = linearLayoutManager.FindLastVisibleItemPosition(); //if (position >= getItemCount() - 1 && isMoreDataAvailable && !isLoading && loadMoreListener != null) //{ // isLoading = true; // loadMoreListener.onLoadMore(); //} if (LoadMoreEvent != null && isMoreDataAvailable && !loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) { LoadMoreEvent(this, null); loading = true; } }
public override void OnScrolled(RecyclerView recyclerView, int dx, int dy) { base.OnScrolled(recyclerView, dx, dy); LinearLayoutManager linearLayoutManager = (LinearLayoutManager)recyclerView.GetLayoutManager(); if (!currentActivity.loading && linearLayoutManager.ItemCount == (linearLayoutManager.FindLastVisibleItemPosition() + 1)) { new Handler().PostDelayed(() => { if (currentActivity.loadTimes <= 5) { currentActivity.adapter.RemoveFooter(); currentActivity.loading = false; currentActivity.adapter.AddItems(currentActivity.data); currentActivity.adapter.AddFooter(); currentActivity.loadTimes++; } else { currentActivity.adapter.RemoveFooter(); var snackbar = Snackbar.Make(currentActivity.mRecyclerView, currentActivity.GetString(Resource.String.no_more_data), Snackbar.LengthShort); snackbar.AddCallback(new MyCallback(currentActivity)); } }, 1500); currentActivity.loading = true; } }
public static bool CanRecyclerViewScroll(View view) { RecyclerView rv = (RecyclerView)view; RecyclerView.LayoutManager lm = rv.GetLayoutManager(); int count = rv.GetAdapter().ItemCount; int lastVisible = -1; if (lm is LinearLayoutManager) { LinearLayoutManager llm = (LinearLayoutManager)lm; lastVisible = llm.FindLastVisibleItemPosition(); } else if (lm is GridLayoutManager) { GridLayoutManager glm = (GridLayoutManager)lm; lastVisible = glm.FindLastVisibleItemPosition(); } else { //throw new MaterialDialog.NotImplementedException("Material Dialogs currently only supports LinearLayoutManager and GridLayoutManager. Please report any new layout managers."); throw new System.Exception("Material Dialogs currently only supports LinearLayoutManager and GridLayoutManager. Please report any new layout managers."); } if (lastVisible == -1) { return(false); } /* We scroll if the last item is not visible */ bool lastItemVisible = lastVisible == count - 1; return(!lastItemVisible || rv.GetChildAt(rv.ChildCount - 1).Bottom > rv.Height - rv.PaddingBottom); }
public static bool CanRecyclerViewScroll(RecyclerView view) { if (view == null || view.GetAdapter() == null || view.GetLayoutManager() == null) { return(false); } RecyclerView.LayoutManager lm = view.GetLayoutManager(); int count = view.GetAdapter().ItemCount; int lastVisible; if (lm is LinearLayoutManager) { LinearLayoutManager llm = (LinearLayoutManager)lm; lastVisible = llm.FindLastVisibleItemPosition(); } else { throw new InvalidOperationException("Material Dialogs currently only supports LinearLayoutManager. Please report any new layout managers."); } if (lastVisible == -1) { return(false); } bool lastItemVisible = lastVisible == count - 1; return(!lastItemVisible || (view.ChildCount > 0 && view.GetChildAt(view.ChildCount - 1).Bottom > view.Height - view.PaddingBottom)); }
//当RecyclerView的滑动状态改变时触发 //滑动状态有3种,0:ScrollStateIdle手指离开屏幕1ScrollStateDragging:手指触碰屏幕2ScrollStateSetting public override void OnScrollStateChanged(RecyclerView recyclerView, int newState) { base.OnScrollStateChanged(recyclerView, newState); System.Diagnostics.Debug.Write("test", "newState:" + newState); int state = recyclerView.ScrollState; if (state == RecyclerView.ScrollStateIdle) { int lastVisibleItemPosition = _linearLayoutManager.FindLastVisibleItemPosition(); if (lastVisibleItemPosition + 1 == _adapter.ItemCount) { System.Diagnostics.Debug.Write("test", "loadding已经完成" + state); _InsertDataEvent(); } } }
public override void OnScrolled(RecyclerView recyclerView, int dx, int dy) { base.OnScrolled(recyclerView, dx, dy); LinearLayoutManager layoutManager = (LinearLayoutManager)recyclerView.GetLayoutManager(); if (layoutManager.FindLastVisibleItemPosition() + threshold > collection.Count && dy > 0) { LoadMore(threshold); } }
public override void OnScrolled(RecyclerView recyclerView, int dx, int dy) { base.OnScrolled(recyclerView, dx, dy); var totalItemCount = recyclerView.GetAdapter().ItemCount; var lastVisibleItemPosition = LayoutManager.FindLastVisibleItemPosition(); if (totalItemCount == (lastVisibleItemPosition + 1) && LoadMoreEvent != null) { LoadMoreEvent(this, null); } }
public override void OnScrollStateChanged(RecyclerView recyclerView, int newState) { base.OnScrollStateChanged(recyclerView, newState); try { if (newState == (int)Android.Widget.ScrollState.Idle) { //if (XRecyclerView.Thumbnail != null) // XRecyclerView.Thumbnail.Visibility = ViewStates.Visible; //if (XRecyclerView.PlayControl != null) // XRecyclerView.PlayControl.Visibility = ViewStates.Visible; XRecyclerView.PlayVideo(!recyclerView.CanScrollVertically(1)); } else { var startPosition = LayoutManager.FindFirstVisibleItemPosition(); var endPosition = LayoutManager.FindLastVisibleItemPosition(); var startPositionVideoHeight = XRecyclerView.GetVisibleVideoSurfaceHeight(startPosition); var endPositionVideoHeight = XRecyclerView.GetVisibleVideoSurfaceHeight(endPosition); var targetPosition = startPositionVideoHeight > endPositionVideoHeight; if (endPositionVideoHeight - 230 > startPositionVideoHeight) { if (XRecyclerView.VideoPlayer.PlayWhenReady) { XRecyclerView.VideoPlayer.PlayWhenReady = false; } } if (startPositionVideoHeight <= 60) { if (XRecyclerView.Thumbnail != null) { XRecyclerView.Thumbnail.Visibility = ViewStates.Visible; } if (XRecyclerView.PlayControl != null) { XRecyclerView.PlayControl.Visibility = ViewStates.Visible; } } } } catch (Exception e) { Console.WriteLine(e); } }
public override void OnScrolled(RecyclerView recyclerView, int dx, int dy) { base.OnScrolled(recyclerView, dx, dy); //var visibleItemCount = recyclerView.ChildCount; var totalItemCount = recyclerView.GetAdapter().ItemCount; var lastVisibleItemPosition = LayoutManager.FindLastVisibleItemPosition(); //var pastVisiblesItems = LayoutManager.FindFirstVisibleItemPosition(); Log.Debug("test", "totalItemCount =" + totalItemCount + "-----" + "lastVisibleItemPosition =" + lastVisibleItemPosition); if (totalItemCount == (lastVisibleItemPosition + 1) && LoadMoreEvent != null) { LoadMoreEvent(this, null); } }
public override void OnScrolled(RecyclerView recyclerView, int dx, int dy) { base.OnScrolled(recyclerView, dx, dy); LinearLayoutManager layoutManager = (LinearLayoutManager)recyclerView.GetLayoutManager(); int firstVisible = layoutManager.FindFirstVisibleItemPosition(); int visibleCount = Math.Abs(firstVisible - layoutManager.FindLastVisibleItemPosition()); int itemCount = recyclerView.GetAdapter().ItemCount; if (firstVisible != lastFirstVisible || visibleCount != lastVisibleCount || itemCount != lastItemCount) { scrollListener.OnScroll(null, firstVisible, visibleCount, itemCount); lastFirstVisible = firstVisible; lastVisibleCount = visibleCount; lastItemCount = itemCount; } }
public override void OnDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { base.OnDrawOver(c, parent, state); int itemCount = parent.GetAdapter().ItemCount; // center horizontally, calculate width and subtract half from center float totalLength = mIndicatorItemLength * itemCount; float paddingBetweenItems = Math.Max(0, itemCount - 1) * mIndicatorItemPadding; float indicatorTotalWidth = totalLength + paddingBetweenItems; float indicatorStartX = (parent.Width - indicatorTotalWidth) / 2F; float indicatorPosY = parent.Height - mIndicatorHeight / 2F; drawInactiveIndicators(c, indicatorStartX, indicatorPosY, itemCount); // find active page (which should be highlighted) LinearLayoutManager layoutManager = (LinearLayoutManager)parent.GetLayoutManager(); int activePosition = layoutManager.FindLastVisibleItemPosition(); if (activePosition == RecyclerView.NoPosition) { return; } // find offset of active page (if the user is scrolling) View activeChild = layoutManager.FindViewByPosition(activePosition); int left = activeChild.Left; int width = activeChild.Width; // on swipe the active item will be positioned from [-width, 0] // interpolate offset for smooth animation float progress = mInterpolator.GetInterpolation(left * -1 / (float)width); drawHighlights(c, indicatorStartX, indicatorPosY, activePosition, progress, itemCount); }
/// <summary> /// This method calculates the current scroll position and its offset to help new attached /// recyclerView on window at that position and offset /// </summary> /// <seealso cref="GetScrollPosition()"/> /// <seealso cref="GetScrollPositionOffset()"/> private void RenewScrollPosition(RecyclerView recyclerView) { LinearLayoutManager layoutManager = (LinearLayoutManager)recyclerView.GetLayoutManager(); mScrollPosition = layoutManager.FindFirstCompletelyVisibleItemPosition(); // That means there is no completely visible Position. if (mScrollPosition == -1) { mScrollPosition = layoutManager.FindFirstVisibleItemPosition(); // That means there is just a visible item on the screen if (mScrollPosition == layoutManager.FindLastVisibleItemPosition()) { } else { // in this case we use the position which is the last & first visible item. // That means there are 2 visible item on the screen. However, second one is not // completely visible. mScrollPosition = mScrollPosition + 1; } } mScrollPositionOffset = layoutManager.FindViewByPosition(mScrollPosition).Left; }
void UpdateProgressBar(int position, long progress) { queue[position].progress = (int)(progress * 100); DownloadQueue.instance?.RunOnUiThread(() => { LinearLayoutManager LayoutManager = (LinearLayoutManager)DownloadQueue.instance?.ListView?.GetLayoutManager(); if (LayoutManager != null && position >= LayoutManager.FindFirstVisibleItemPosition() && position <= LayoutManager.FindLastVisibleItemPosition()) { View view = DownloadQueue.instance.ListView.GetChildAt(position - LayoutManager.FindFirstVisibleItemPosition()); DownloadHolder holder = (DownloadHolder)DownloadQueue.instance.ListView.GetChildViewHolder(view); holder.Progress.Progress = (int)(progress * 100); } }); if (queue.Count == 1) { SetNotificationProgress((int)(progress * 100)); } }
public override void OnScrolled(RecyclerView recyclerView, int dx, int dy) { pref = PreferenceManager.GetDefaultSharedPreferences(Application.Context); LinearLayoutManager linearLayoutManager = (LinearLayoutManager)recyclerView.GetLayoutManager(); int lastVisibleItem = linearLayoutManager.FindLastVisibleItemPosition(); int totalItemCount = recyclerView.GetAdapter().ItemCount; if (!IsLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) { // End has been reached // Do something var currPrefPage = pref.GetString("CurrentPage", string.Empty); if (!String.IsNullOrEmpty(currPrefPage)) { if (Int32.Parse(currPrefPage) > 0) { currentPage++; } else { currentPage = 2; } } else { currentPage++; } var editor = pref.Edit(); editor.PutString("CurrentPage", currentPage.ToString()); editor.Apply(); IsLoading = true; Task.Factory.StartNew(async() => { try{ var newPostList = new List <Post>(); await WebClient.LoadPosts(newPostList, currentPage); (recyclerView.GetAdapter() as PostViewAdapter)._Posts.AddRange(newPostList); //recyclerView.GetAdapter().HasStableIds = true; _messageShown = false; Application.SynchronizationContext.Post(_ => { recyclerView.GetAdapter().NotifyDataSetChanged(); }, null); //recyclerView.GetAdapter().NotifyItemRangeInserted(recyclerView.GetAdapter().ItemCount,newPostList.Count); }catch (Exception ex) { //Insights.Report(ex,new Dictionary<string,string>{{"Message",ex.Message}},Insights.Severity.Error); var text = ex.Message; if (!_messageShown) { Application.SynchronizationContext.Post(_ => { Toast.MakeText(Application.Context, "При загрузке данных произошла ошибка", ToastLength.Short).Show(); }, null); _messageShown = true; } } IsLoading = false; }); } }
private int GetLastVisibleItemPosition() { //return (mLayoutManager as LinearLayoutManager).FindLastVisibleItemPosition(); return(mLayoutManager.FindLastVisibleItemPosition()); }
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = (LinearLayout)inflater.Inflate(Resource.Layout.reading_and_writing_fragment, container, false); mRecyclerView = rootView.FindViewById <RecyclerView>(Resource.Id.recyclerView); mRecyclerView.HasFixedSize = true; mLayoutManager = new LinearLayoutManager(Activity); mRecyclerView.SetLayoutManager(mLayoutManager); mAdapter = new RecyclerViewAdapter(App.db.GetAllImages(), Activity); mRecyclerView.SetAdapter(mAdapter); if (App.CurrentImagePossition != -1) { if (!(mLayoutManager.FindFirstVisibleItemPosition() <= App.CurrentImagePossition && mLayoutManager.FindLastVisibleItemPosition() >= App.CurrentImagePossition)) { Activity.RunOnUiThread(() => { mLayoutManager.ScrollToPositionWithOffset(App.CurrentImagePossition, 20); });; } //StartPostponedEnterTransition(); } else { //StartPostponedEnterTransition(); } mAdapter.ItemClick += delegate { var view = mRecyclerView.FindViewHolderForAdapterPosition(mAdapter.ClickedPosition) as RecyclerViewHolder; var tmp = view.Slika.TransitionName; GuessingImageFragment gif = new GuessingImageFragment(mAdapter.clickedImage.Id); //gif.SharedElementEnterTransition = new AutoTransition(); //gif.EnterTransition = new Fade(); //setExitTransition(new Fade()); Activity.FragmentManager.BeginTransaction() .Replace(App.fragmentContainer.Id, gif, "guessing") .AddSharedElement(view.Slika, mAdapter.clickedImage.Name) .AddToBackStack(null) .Commit(); App.CurrentFragment = gif; }; return(rootView); }
public override void MeasureChildWithMargins(Android.Views.View child, int widthUsed, int heightUsed) { base.MeasureChildWithMargins(child, widthUsed, heightUsed); // If has fixed width is true, than calculation of the column width is not necessary. if (mTableView.HasFixedWidth()) { return; } int position = GetPosition(child); ColumnLayoutManager childLayoutManager = (ColumnLayoutManager)((CellRecyclerView)child).GetLayoutManager(); // the below codes should be worked when it is scrolling vertically if (mCellRecyclerView.ScrollState != RecyclerView.ScrollStateIdle) { if (childLayoutManager.IsNeedFit()) { // Scrolling up if (mLastDy < 0) { Log.Error(LogTag, position + " fitWidthSize all vertically up"); FitWidthSize(true); } else { // Scrolling down Log.Error(LogTag, position + " fitWidthSize all vertically down"); FitWidthSize(false); } // all columns have been fitted. childLayoutManager.ClearNeedFit(); } // Set the right initialPrefetch size to improve performance childLayoutManager.InitialPrefetchItemCount = childLayoutManager.ChildCount; } else { // That means,populating for the first time like fetching all data to display. // It shouldn't be worked when it is scrolling horizontally ."getLastDx() == 0" // control for it. if (childLayoutManager.GetLastDx() == 0 && mCellRecyclerView.ScrollState == RecyclerView.ScrollStateIdle) { if (childLayoutManager.IsNeedFit()) { mNeedFit = true; // all columns have been fitted. childLayoutManager.ClearNeedFit(); } if (mNeedFit) { // for the first time to populate adapter if (mRowHeaderLayoutManager.FindLastVisibleItemPosition() == position) { FitWidthSize2(false); Log.Error(LogTag, position + " fitWidthSize populating data for the first time"); mNeedFit = false; } } } } }
public override bool Fling(int velocityX, int velocityY) { if (ItemCount < 1) { return(true); } FlingEventArgs flingEventArgs; if (velocityX > 0) { flingEventArgs = new FlingEventArgs(FlingDirection.Right, CurrentItem); if (CurrentItem == ItemCount - 1) { return(true); } } else { flingEventArgs = new FlingEventArgs(FlingDirection.Left, CurrentItem); } if (BeforeFling != null) { BeforeFling(this, flingEventArgs); } LinearLayoutManager linearLayoutManager = (LinearLayoutManager)GetLayoutManager(); //these four variables identify the views you see on screen. int lastVisibleView = linearLayoutManager.FindLastVisibleItemPosition(); int firstVisibleView = linearLayoutManager.FindFirstVisibleItemPosition(); View firstView = linearLayoutManager.FindViewByPosition(firstVisibleView); View lastView = linearLayoutManager.FindViewByPosition(lastVisibleView); //these variables get the distance you need to scroll in order to center your views. //my views have variable sizes, so I need to calculate side margins separately. //note the subtle difference in how right and left margins are calculated, as well as //the resulting scroll distances. int leftMargin = (screenWidth - lastView.Width) / 2; int rightMargin = (screenWidth - firstView.Width) / 2 + firstView.Width; int leftEdge = lastView.Left; int rightEdge = firstView.Right; int scrollDistanceLeft = leftEdge - leftMargin; int scrollDistanceRight = rightMargin - rightEdge; //if(user swipes to the left) if (velocityX > 0) { CurrentItem = lastVisibleView - 1; SmoothScrollBy(scrollDistanceLeft, 0); } else { CurrentItem = firstVisibleView - 1; SmoothScrollBy(-scrollDistanceRight, 0); } if (OnFling != null) { OnFling(this, flingEventArgs); } return(true); }