我有一张卡片列表(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;
}