我正在使用 cardlistview 创建一个应用程序。我使用 gabrielemariotti 卡库 gabrielemariotti 卡实现了卡列表视图
我能够实现卡片列表视图,并且通过扩展自定义布局也是成功的。虽然我无法将值设置为自定义布局 texview 内的 textviews,因为它正在获取空值..
我的卡片列表 XML 是:
<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/news_card"/>
新闻卡.xml:
<?xml version="1.0" encoding="utf-8"?>
<it.gmariotti.cardslib.library.view.CardView
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="match_parent"
android:layout_height="wrap_content"
card:card_layout_resourceID="@layout/card_thumbnail_layout"
/>
news_card_layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<TextView
android:text="News"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/myNews"
android:textSize="18sp"
tools:text="news" />
<TextView
android:text="Date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mydate"
android:gravity="right"
tools:text="date" />
</LinearLayout>
我创建卡片的活动代码”:
ArrayList<Card> cards = new ArrayList<Card>();
for (int i = 0; i<5; i++) {
// Create a Card
Card card1 = new Card(this.getActivity(),R.layout.news_card_layout);
CardHeader header1 = new CardHeader(this.getActivity());
// Add Header to card
header1.setTitle("hi");
card1.addCardHeader(header1);
CardThumbnail thumb = new CardThumbnail(this.getActivity());
thumb.setUrlResource("https://lh5.googleusercontent.com/-N8bz9q4Kz0I/AAAAAAAAAAI/AAAAAAAAAAs/Icl2bQMyK7c/s265-c-k-no/photo.jpg");
card1.addCardThumbnail(thumb);
TextView newsdesc = (TextView)getView().findViewById(R.id.myNews);
newsdesc.setText("hello");
/*TextView mydate = (TextView)getView().findViewById(R.id.mydate);
mydate.setText("2 Seconds ago");*/
cards.add(card1);
card1.setOnClickListener(new Card.OnCardClickListener() {
@Override
public void onClick(Card card, View view) {
Toast.makeText(getActivity(),"Clickable card", Toast.LENGTH_LONG).show();
}
});
}
CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(this.getActivity(), cards);
CardListView listView = (CardListView) this.getView().findViewById(R.id.myList);
if (listView != null) {
listView.setAdapter(mCardArrayAdapter);
}