Ten
Ten

Reputation: 33

RecyclerView - how to scroll to the position of the required data item (by binded)?

I am new to android programming and I had a question, maybe I was just bad looking for an answer, but I wasted a couple of days on it.

I use RecyclerView for my data like

private JavaList<Good> goods = new JavaList<Good>();
public class Good : INotifyPropertyChanged
{
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public string name;
        public int quality;  //need
        public int real_quality;  // now
}

I have adapter, holder and etc., all of the documentation... I receive barcodes from barcode scanner

public class MyScanReceiver : BroadcastReceiver
{
     public interface BarcodeDataInterface
        {
            void OnBarcodeReceived(string barcode);
        }

        private BarcodeDataInterface mBarcodeDataInterface;
        // some code
}

in the MainActivity.cs:

      public void OnBarcodeReceived(string barcode)
        {
            Good g = GetByBarcode(barcode);  // get good by barcode, 
            if (g == null)
            {
                Console.WriteLine($"Not found {barcode}.");
                return;
            }
            g.real_quantity++;  
            g.isChecked = g.real_quantity >= g.quantity; 

            recyclerView1.ScrollToPosition( ?? POSITION ?? );
            rvAdapter.NotifyDataSetChanged();
        }

and now I need to scroll to the position of founded good, but how do I get this position?

Maybe I'm not doing everything right at all and there is some other way? I look forward to any advice and suggestions! :)

Upvotes: 1

Views: 111

Answers (1)

Leo Zhu
Leo Zhu

Reputation: 15001

You could get the postion of your good which receive from the barcode scanner in your RecycleView source.

For example:

private JavaList<Good> goods = new JavaList<Good>(); //this is the source of your recycleview

when you get the goods by barcode :

public void OnBarcodeReceived(string barcode)
    {
        Good g = GetByBarcode(barcode);  // get good by barcode, 
        if (g == null)
        {
            Console.WriteLine($"Not found {barcode}.");
            return;
        }
        g.real_quantity++;  
        g.isChecked = g.real_quantity >= g.quantity; 
        
        int position;
        for (int i = 0; i < goods.Count; i++)
        {
            if (goods[i].name.Equals(g.name))
            {
                position = i; //the position is what you want
            }
        }
        recyclerView1.ScrollToPosition(position);
    }

Upvotes: 1

Related Questions