0

I'm doing an endless scroll RecyclerView that load items on demand. But I cannot detect the position of my LayoutManager because the function FindFirstVisibleItemPosition is always returning -1. Here's the code:

FRAGMENT VIEW

 <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);
        }
    }
}

Could you give me an idea of what's happening? Thanks in advance!

BytesGuy
  • 3,932
  • 6
  • 33
  • 52
user3535054
  • 67
  • 1
  • 9
  • I don't know how to see that LinearLayoutManager is getting correctly the LinearLayout that's containing the RecyclerView. – user3535054 Dec 07 '16 at 14:49
  • Take a look here http://stackoverflow.com/questions/31816866/recyclerview-for-large-items may be that will give you an idea – Yuri S Dec 07 '16 at 18:14
  • Thanks for the reply. The code is the same, as you can see, but the problem occurs when calling FindFirstVisibleItemPosition() method. It's returning -1 and I think It is not the result that should give. – user3535054 Dec 07 '16 at 18:19
  • If code is the same the only option I see is to provide complete solution and I (or may be somebody else) can try to debug it – Yuri S Dec 07 '16 at 18:21
  • Have you tried setting `android:visibility` from `gone` to `visible` or `invisible`? – Elvis Xia - MSFT Dec 09 '16 at 09:41

0 Answers0