Android GridView适配器使用错误的位置



我正在针对KitKat(4.4-API19)的C#上使用Xamarain Android进行开发。

设置

所以我有一个我想用GridView渲染的车辆列表。由于这是在一些选项卡中,GridView包含在第一次单击相应选项卡时要创建的片段中(此处未显示代码)。这很好,当GarageFragmentAdapter开始获取视图时,问题就出现了。我确保片段和适配器只创建过一次,所以不会出现多个实例与其工作冲突的问题。我还没有附加任何铃声或口哨声(滚动反应或项目反应),这只是关于渲染。

问题

在我的例子中,我有4辆车,所以我的清单有4项。对适配器的第一次调用使用位置0(ok),第二次调用也使用位置0,然后只有第三次调用使用了位置1(肯定不ok),没有第四次调用。视觉输出只显示了两个项目,这也不是我所期望的,但我认为GridView使用该位置在x位置渲染项目。

所以我的问题是,如何说服适配器正确地遍历我的数据列表?

代码

如下所示的代码是最新的迭代,之前在片段中设置了适配器,我认为这是问题所在,因为在某个地方读到了这可能是一个问题。

public class GarageFragment : Fragment
{
private readonly VehiclesResponse _garageResponse;
private readonly GarageFragmentAdapter _adapter;
public GarageFragment(VehiclesResponse garageResponse, GarageFragmentAdapter adapter)
{
_garageResponse = garageResponse;
_adapter = adapter;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var fragmentView = inflater.Inflate(Resource.Layout.fragment_garage, container, false);
fragmentView.FindViewById<GridView>(Resource.Id.gridVehicleCards).Adapter = _adapter;
fragmentView.FindViewById<Button>(Resource.Id.btnShowFullGarage).Visibility = _garageResponse.TotalCarsInGarageCount > 4 ? ViewStates.Visible : ViewStates.Gone;
fragmentView.FindViewById<LinearLayout>(Resource.Id.boxAddVehicles).Visibility = _garageResponse.CanAddVehicles ? ViewStates.Visible : ViewStates.Gone;
return fragmentView;
}
}
public class GarageFragmentAdapter : BaseAdapter
{
private readonly Activity _context;
private readonly IList<Vehicle> _tileList;
public GarageFragmentAdapter(Activity context, IList<Vehicle> vehicles)
{
_context = context;
_tileList = vehicles;
}
public override int Count => _tileList.Count;
public override Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = convertView;
if (view == null)
{
var item = _tileList[position];
view = _context.LayoutInflater.Inflate(Resource.Layout.BasicVehicleCard, null);
view.FindViewById<TextView>(Resource.Id.vehicleName).Text = item.Name;
}
return view;
}
}

GridView似乎无法正常工作它不会随着所提供的内容而增长,它根据所拥有的空间来定义将显示/加载多少内容(在GridView的文档中说明会很好)

由于这对我的需求不可行,我将使用GridLayout并在其中添加内容元素视图。

最新更新