我正在使用cardlistview创建应用程序.. 我使用gabrielemariotti卡片库实现了卡片列表视图 gabrielemariotti cards
我能够实现卡片表视图并通过膨胀自定义布局也是成功的。虽然我无法在自定义布局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"/>
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>
我的活动代码来创建卡片&#34;:
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);
}
答案 0 :(得分:0)
最后我得到了答案.. 我刚创建了一个自定义卡片类
CustomCard类:
public class CustomCard extends Card {
protected TextView myDesc;
protected TextView myDate;
public String news;
public String date;
/**
* Constructor with a custom inner layout
* @param context
*/
public CustomCard(Context context) {
this(context, R.layout.news_card_layout);
}
/**
*
* @param context
* @param innerLayout
*/
public CustomCard(Context context, int innerLayout) {
super(context, innerLayout);
//init();
}
/**
* Init
*/
private void init(){
//No Header
//Set a OnClickListener listener
setOnClickListener(new OnCardClickListener() {
@Override
public void onClick(Card card, View view) {
Toast.makeText(getContext(), "Click Listener card=", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void setupInnerViewElements(ViewGroup parent, View view) {
//Retrieve elements
myDesc = (TextView) parent.findViewById(R.id.myNews);
myDate = (TextView) parent.findViewById(R.id.myDate);
if (myDesc != null)
myDesc.setText(news);
if (myDate != null)
myDate.setText(date);
}
public void setNews(String news) {
this.news=news;
}
public void setDate(String date) {
this.date=date;
}
活动代码:
ArrayList<Card> cards = new ArrayList<Card>();
for (int i = 0; i<5; i++) {
// Create a Card
CustomCard card1 = new CustomCard(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);
card1.setNews("test");
card1.setDate("12/3/2017");
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);
}
-----它解决了问题----