我正在进行无限滚动RecyclerView
按需加载项目。但我无法检测到LayoutManager
的位置,因为函数FindFirstVisibleItemPosition
始终返回-1
。这是代码:
碎片视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/SearchRelativeLayout"
android:layout_height="match_parent"
android:layout_width="match_parent">
<EditText
android:id="@+id/BuscarEditText"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="Buscar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="@+id/BuscarTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerVertical="true"
android:text="Buscador!" />
<android.support.v7.widget.RecyclerView
android:id="@+id/SongListRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
FRAGMENT ONCREATEVIEW
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(mPage, container, false);
buscarTextView = view.FindViewById<TextView>(Resource.Id.BuscarTextView);
buscarEditText = view.FindViewById<EditText>(Resource.Id.BuscarEditText);
buscarEditText.TextChanged += BuscarEditText_TextChanged;
songListRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.SongListRecyclerView);
if (songListRecyclerView != null)
{
songListRecyclerView.HasFixedSize = true;
var layoutManager = new LinearLayoutManager(Activity);
var onScrollListener = new SearchOnScrollListener(layoutManager);
int page = 0;
songList = new List<Song>();
onScrollListener.LoadMoreEvent += async (object sender, EventArgs e) =>
{
var songsSearched = await Task.Run(() =>
{
return songService.SearchSongs(keyword, page);
});
songList.AddRange(songsSearched.Value.ToList());
songListAdapter = new SongListAdapter(songList, SongViewHolder.MenuOptions.AddToPlaylist | SongViewHolder.MenuOptions.PlaySong);
songListAdapter.SongList = songList;
songListAdapter.NotifyDataSetChanged();
page++;
};
songListRecyclerView.AddOnScrollListener(onScrollListener);
songListRecyclerView.SetLayoutManager(layoutManager);
}
return view;
}
CUSTOM ONSCROLLLISTENER
public class SearchOnScrollListener : OnScrollListener
{
public delegate void LoadMoreEventHandler(object sender, EventArgs e);
public event LoadMoreEventHandler LoadMoreEvent;
private LinearLayoutManager LayoutManager;
public SearchOnScrollListener(LinearLayoutManager layoutManager)
{
LayoutManager = layoutManager;
}
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(); //That line is returning -1
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount)
{
LoadMoreEvent(this, null);
}
}
}
你能告诉我发生了什么吗?提前谢谢!