我正在使用Cardslib并尝试使用自定义布局填充每张卡片。
我已经设置好了所有东西,但我似乎无法让它工作。我遇到的问题是 CardListView 没有被填充。
这是我的代码:
public class ThemeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_theme);
ArrayList<Card> cards = new ArrayList<Card>();
for (int i = 0; i < 8; i++) {
ColorCard card = new ColorCard(this);
switch (i) {
case 0:
card.mTitle = "Default Theme";
card.imageId = R.drawable.defaulttheme_thumb;
break;
case 1:
card.mTitle = "Cool Blue";
card.imageId = R.drawable.theme1_thumb;
break;
case 2:
card.mTitle = "Tropic Red";
card.imageId = R.drawable.theme2_thumb;
break;
case 3:
card.mTitle = "Distant Road";
card.imageId = R.drawable.theme3_thumb;
break;
case 4:
card.mTitle = "Simplistic";
card.imageId = R.drawable.theme4_thumb;
break;
case 5:
card.mTitle = "Coffe Time";
card.imageId = R.drawable.theme5_thumb;
break;
case 6:
card.mTitle = "Gloomy Orange";
card.imageId = R.drawable.theme6_thumb;
break;
case 7:
card.mTitle = "Dappled Purple";
card.imageId = R.drawable.theme7_thumb;
break;
case 8:
card.mTitle = "Vibrant Green";
card.imageId = R.drawable.theme8_thumb;
break;
}
cards.add(card);
}
CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(this, cards);
CardListView listView = (CardListView) this.findViewById(R.id.myList);
if (listView != null) {
listView.setAdapter(mCardArrayAdapter);
}
}
}
自定义卡类:
public class ColorCard extends Card {
protected String mTitle;
protected int imageId;
TextView themeName;
ImageView themeThumb;
protected int count;
public ColorCard(Context context) {
this(context, R.layout.layout_cardview);
}
public ColorCard(Context context, int innerLayout) {
super(context, innerLayout);
init();
}
private void init() {
// Add ClickListener
setOnClickListener(new OnCardClickListener() {
@Override
public void onClick(Card card, View view) {
Toast.makeText(getContext(), "Click Listener card=" + count,
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void setupInnerViewElements(ViewGroup parent, View view) {
// Retrieve elements
themeName = (TextView) parent.findViewById(R.id.themeText);
themeThumb = (ImageView) parent.findViewById(R.id.themeThumb);
if (themeName != null)
themeName.setText(mTitle);
if (themeThumb != null)
themeThumb.setImageResource(imageId);
}
}
这是我的布局:
主卡片列表视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<it.gmariotti.cardslib.library.view.CardListView
android:id="@+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent"
card:list_card_layout_resourceID="@layout/layout_cardview" />
</LinearLayout>
layout_cardview
<it.gmariotti.cardslib.library.view.CardViewNative xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:id="@+id/list_cardId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card:card_layout_resourceID="@layout/row_card" />
行卡:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/themeThumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"
/>
<TextView
android:id="@+id/themeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="15dp"
android:textColor="@android:color/black"
android:textSize="20sp"
/>
</LinearLayout>