如何在 CardListView 中为 gabrielemariotti 卡片添加自定义布局
How to add custom layout in CardListView for gabrielemariotti cards
我正在使用 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>
我的 Activity 创建卡片的代码":
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);
}
终于找到答案了..
我刚刚创建了一张自定义卡片 class
自定义名片Class:
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;
}
Activity代码:
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);
}
-----它解决了问题----
我正在使用 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>
我的 Activity 创建卡片的代码":
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);
}
终于找到答案了.. 我刚刚创建了一张自定义卡片 class
自定义名片Class:
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;
}
Activity代码:
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);
}
-----它解决了问题----