2016-09-26 1 views
0

Je crée une application avec cardlistview .. I mis en œuvre la vue de la liste des cartes en utilisant la bibliothèque de cartes gabrielemariotti gabrielemariotti cardsComment ajouter la mise en page personnalisée dans CardListView pour les cartes gabrielemariotti

Je suis en mesure d'atteindre la vue cardlist et en gonflant la mise en page personnalisée est également réussie. Bien que je ne peux pas définir les valeurs à textviews dans la mise en page personnalisée texview comme il obtient des valeurs nulles ..

Ma liste de carte XML est:

<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> 

Mon code d'activité pour créer les cartes ":

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); 
      } 

Répondre

0

Enfin j'ai obtenu la réponse .. Je viens de créer une carte personnalisée classe

CustomCard Classe:

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; 
    } 

Code d'activité:

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); 
      } 

----- IL RÉSOLVE LE PROBLÈME ----