我使用下面的代码来获取recyclerview中的可见项目
在我的片段活动中,我有:
layoutmanager = new LinearLayoutManager(Activity);
adapter = new FeedAdapter(vid, userName, this.Context);
feeditem.SetLayoutManager(layoutmanager);
feeditem.SetAdapter(adapter);
var onScrollListener = new XamarinRecyclerViewOnScrollListener(Activity, layoutmanager, adapter);
feeditem.AddOnScrollListener(onScrollListener);
,事件处理程序类如下所示:
public class XamarinRecyclerViewOnScrollListener : RecyclerView.OnScrollListener
{
public delegate void LoadMoreEventHandler(object sender, EventArgs e);
public event LoadMoreEventHandler LoadMoreEvent;
private LinearLayoutManager layoutmanager;
private Action StateChange;
private FeedAdapter adapter;
private View currentFocusedLayout, oldFocusedLayout;
private Context ctx;
public XamarinRecyclerViewOnScrollListener(LinearLayoutManager layoutManager)
{
this.layoutmanager = layoutManager;
}
public XamarinRecyclerViewOnScrollListener(Context ctx, LinearLayoutManager layoutmanager, FeedAdapter adapter)
{
// TODO: Complete member initialization
this.layoutmanager = layoutmanager;
this.adapter = adapter;
this.ctx = ctx;
//this.StateChange = StateChange;
}
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();
if (firstVisiblePosition >= 0)
{
if (oldFocusedLayout != null)
{
Toast.MakeText(ctx, "Stop Video", ToastLength.Long).Show();
}
}
currentFocusedLayout = layoutmanager.FindViewByPosition(firstVisiblePosition);
//VideoView vv_dashboard = (VideoView)currentFocusedLayout.findViewById(R.id.vv_dashboard);
////to play video of selected recylerview, videosData is an array-list which is send to recyclerview adapter to fill the view. Here we getting that specific video which is displayed through recyclerview.
//playVideo(videosData.get(positionView));
Toast.MakeText(ctx, "Play video", ToastLength.Long).Show();
oldFocusedLayout = currentFocusedLayout;
}
}
}
layoutmanager.FindFirstCompletelyVisibleItemPosition()
始终返回-1。我想检查屏幕上可见的列表上的项目,然后用它做一些事情。我尝试了不同的解决方案,但没有一个有效。什么是实现这一目标的正确方法?
答案 0 :(得分:0)
如果检测到-1,则表示RecyclerView未检测到布局中的任何项目