2017-07-21 26 views
0

我正在開發針對KitKat(4.4 - API19)的C#上的Xamarain Android。Android GridView適配器使用錯誤的位置

設置

所以,我有車,我想使用GridView控件來呈現這個列表。由於這是在一些tab中,所以當第一次點擊相應的選項卡(代碼未在此處顯示)時,GridView將包含在要創建的片段中。這很好,當GarageFragmentAdapter開始獲取視圖時就會出現問題。 我確信片段和適配器只創建一次,所以它不是多個實例與其作品相沖突的問題。我沒有附加任何花裏胡哨的東西(滾動反應或物品反應),只是在渲染過程中。

問題

在我的例子,我有4輛車,所以我的名單是4項長。第一次調用適配器使用位置0(OK),第二次調用也使用位置0(不OK),然後只有第三次調用使用位置1(絕對不行)並且沒有第四次調用。然後,可視化輸出僅顯示兩個項目,這也不是我所期望的,但我認爲GridView使用該位置呈現項目AT位置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; 
    } 
} 
+0

在'GetView'里加''參數''Log.d',你看到了什麼? – pskink

+0

@pskink D/Position(1492):0,D/Position(1492):0,D/Position(1492):1 – TiGoRn

+0

它在'var view = convertView;'之前被調用? – pskink

回答

0

看來的GridView不工作,我期望的那樣。 它不隨所提供的內容一起增長,它根據它擁有的空間定義它將顯示/加載多少內容。(在GridView的文檔中會有很好的說明)

因爲這對我的需求是不可行的,所以我將使用GridLayout並在其中添加內容元素視圖。