我在我的android項目中有一個recyclerview,它顯示了每個視圖中的媒體內容。我想要實現的是我可以在上下滾動時播放/暫停媒體。我需要獲取完全可見視圖的適配器位置。我正在做這樣的事情。layoutmanager.FindFirstCompletelyVisibleItemPosition總是返回-1
在我的活動片段我有這樣的:
layoutmanager = new LinearLayoutManager(Activity);
adapter = new FeedAdapter(vid, userName, this.Context);
feeditem.SetLayoutManager(layoutmanager);
feeditem.SetAdapter(adapter);
var onScrollListener = new XamarinRecyclerViewOnScrollListener(Activity, layoutmanager, adapter);
滾動監聽事件是這樣的:
public override void OnScrollStateChanged(RecyclerView recyclerView, int newState)
{
base.OnScrollStateChanged(recyclerView, newState);
if (newState == (int)ScrollState.Idle)
{
layoutmanager = (LinearLayoutManager)recyclerView.GetLayoutManager();
int firstVisiblePosition = layoutmanager.FindFirstCompletelyVisibleItemPosition();
int visible = layoutmanager.FindFirstVisibleItemPosition();
int last = layoutmanager.FindLastVisibleItemPosition();
if (firstVisiblePosition >= 0)
{
if (oldFocusedLayout != null)
{
Toast.MakeText(ctx, "Stop Video", ToastLength.Long).Show();
}
}
currentFocusedLayout = layoutmanager.FindViewByPosition(firstVisiblePosition);
Toast.MakeText(ctx, "Play video", ToastLength.Long).Show();
oldFocusedLayout = currentFocusedLayout;
}
}
feeditem.AddOnScrollListener(onScrollListener);
的問題是LinearLayout中管理方法FindFirstCompletelyVisibleItemPosition
總是返回-1,即使視圖完全可見。其他方法如FindFirstVisibleItemPosition
和FindLastVisibleItemPosition
給出了視圖的正確位置。
任何想法可能是這裏的問題?