0
我有一張卡片列表(cards_list.xml),其中每張卡片都有自己的佈局(card_in_list.xml)。在片段列表中爲每個項目設置圖像創建視圖
該卡片有一個'ImageView'元素,它正在嘗試在片段創建過程中更改列表中的每個卡片。
這裏是佈局和片段的onCreate代碼,由於
cards_list.xml
<ListView
android:id="@+id/cards_listview"/>
card_in_list.xml
<ImageView
android:id="@+id/cardImage"
android:src="@drawable/visa"/>
CardsListFragment.java
public view OnCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate cards list view
cardsListBinding binding = DataBindingUtil.inflate(inflater,
R.layout.cards_list, container, false);
binding.setSettingsService(this.settingsService);
View view = binding.getRoot();
// use adapter to set card layout to list of cards
itemsList = (ListView) view.findViewById(R.id.cards_listview);
itemsListAdapter = new ListViewBindingAdapter<>(getActivity(),
R.layout.card_in_list, this, myService.getCards());
// get image of each card item set a drawable to it
int i = 0;
for(Card c : myService().getCards()) {
View cardListView = itemsListAdapter.getView(i,null,itemsList);
ImageView iv = (ImageView)cardListView.findViewById(R.id.cardImage);
iv.setImageResource(R.drawable.discover);
i++;
}
itemsList.setAdapter(itemsListAdapter);
return view;
}