问题描述
我是 android 编程的新手,我有一个问题,也许我只是不想找到答案,但我浪费了几天时间。
我使用 RecyclerView 来处理我的数据
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
}
我有适配器、支架等,所有文档... 我从条码扫描器收到条码
public class MyScanReceiver : broadcastReceiver
{
public interface BarcodeDataInterface
{
void OnBarcodeReceived(string barcode);
}
private BarcodeDataInterface mBarcodeDataInterface;
// some code
}
在 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();
}
现在 我需要滚动到已创建好的位置,但是如何获取该位置?
也许我没有做对所有事情,还有其他方法吗? 我期待任何意见和建议! :)
解决方法
您可以从您的 RecycleView 源中的条形码扫描仪获取您的商品的位置。
例如:
private JavaList<Good> goods = new JavaList<Good>(); //this is the source of your recycleview
当您通过条形码收到货物时:
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);
}