1

我剛剛在我的代碼中實現了Cardslib使用Cardslib:「CardView不能轉換爲CardNativeView」

我用下面的代碼來實現TopColoredCard:

ArrayList<Card> cards = initCard(); 
updateAdapter(cards);  

... 

private ArrayList<Card> initCard() { 
    //Init an array of Cards 
    ArrayList<Card> cards = new ArrayList<>(); 
    for (int i = 0; i < AppData.mTitles.length; i++) { 
     TopColoredCard card = new TopColoredCard(getApplicationContext()); 
     card.setColorResourceId(AppData.mColors[i]); 
     card.setTitleOverColor(AppData.mTitles[i]); 
     card.setSubTitleOverColor(subTitle(i)); 
     cards.add(card); 
    } 
    return cards; 
} 

並實現了:

compile 'com.android.support:cardview-v7:21.0.3' 
compile 'com.github.gabrielemariotti.cards:cardslib-core:2.0.1' 
compile 'com.github.gabrielemariotti.cards:cardslib-recyclerview:2.0.1' 
compile 'com.github.gabrielemariotti.cards:cardslib-cards:2.0.1' 

它運作良好,在Android 5.0及更高版本。 較低版本我得到以下錯誤:

java.lang.ClassCastException: it.gmariotti.cardslib.library.view.CardView cannot be cast to it.gmariotti.cardslib.library.view.CardViewNative 
     at it.gmariotti.cardslib.library.cards.topcolored.TopColoredCard.setupInnerViewElements(TopColoredCard.java:206) 
     at it.gmariotti.cardslib.library.internal.Card.getInnerView(Card.java:293) 
     at it.gmariotti.cardslib.library.view.CardView.setupMainView(CardView.java:379) 
     at it.gmariotti.cardslib.library.view.CardView.buildUI(CardView.java:282) 
     at it.gmariotti.cardslib.library.view.CardView.setCard(CardView.java:242) 
     at it.gmariotti.cardslib.library.recyclerview.internal.BaseRecyclerViewAdapter.onBindViewHolder(BaseRecyclerViewAdapter.java:124) 
     at it.gmariotti.cardslib.library.recyclerview.internal.BaseRecyclerViewAdapter.onBindViewHolder(BaseRecyclerViewAdapter.java:38) 
     at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4138) 
     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3448) 
     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340) 
     at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810) 
     at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306) 
     at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269) 
     at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523) 
     at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1988) 
     at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237) 
     at android.view.View.layout(View.java:14817) 
     at android.view.ViewGroup.layout(ViewGroup.java:4631) 
     at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:561) 
     at android.view.View.layout(View.java:14817) 
     at android.view.ViewGroup.layout(ViewGroup.java:4631) 
     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055) 
     at android.view.View.layout(View.java:14817) 
     at android.view.ViewGroup.layout(ViewGroup.java:4631) 
     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
     at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
     at android.view.View.layout(View.java:14817) 
     at android.view.ViewGroup.layout(ViewGroup.java:4631) 
     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 
     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 
     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 
     at android.view.View.layout(View.java:14817) 
     at android.view.ViewGroup.layout(ViewGroup.java:4631) 
     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
     at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
     at android.view.View.layout(View.java:14817) 
     at android.view.ViewGroup.layout(ViewGroup.java:4631) 
     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 
     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 
     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 
     at android.view.View.layout(View.java:14817) 
     at android.view.ViewGroup.layout(ViewGroup.java:4631) 
     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
     at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
     at android.view.View.layout(View.java:14817) 
     at android.view.ViewGroup.layout(ViewGroup.java:4631) 
     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987) 
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744) 
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670) 
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
     at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
     at android.view.Choreographer.doFrame(Choreographer.java:544) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 

我認爲這是11b的缺陷,因爲它適用於API拉特。 20,但它不適用於較低版本。

也許任何人都可以幫助我。

盧卡斯:)

//編輯: 這是我layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@color/background_material_light" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <include 
     android:id="@+id/toolbar_actionbar" 
     layout="@layout/toolbar_default" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
    <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/swipeRefreshLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/toolbar_actionbar"> 
     <view 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      class="it.gmariotti.cardslib.library.recyclerview.view.CardRecyclerView" 
      android:id="@+id/recycler" /> 
    </android.support.v4.widget.SwipeRefreshLayout> 


</RelativeLayout> 
+0

你還可以在佈局文件中發佈你的CardListView嗎? – Sharj

+0

@Sharj編輯我的帖子。 – ich5003

回答

1

更改從CardView你的類CardNativeView在XML文件中。

相關問題