【问题标题】:Material Card with Cardslib library带有 Cardslib 库的材料卡
【发布时间】:2015-01-15 15:06:08
【问题描述】:

我正在从 Github 的 Cardslib 库中实现新的 Material Card 设计。图书馆链接:Cardslib Github 但我无法在 Recycler 视图中实现多张卡片。如果有人已经实现了新的 Cardslib v2 库,那将非常有帮助。

问题是,卡片来了,但没有背景图片和操作按钮。

我要实现的卡片布局是:

这是 RecyclerView 的代码

<it.gmariotti.cardslib.library.recyclerview.view.CardRecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        card:list_card_layout_resourceID="@layout/native_recyclerview_card_layout"
        android:id="@+id/card_recyclerview"/>

这是活动的代码

public class AboutActivity extends ActionBarActivity {


    final int TOTAL_CARDS = 3;
    //private CardArrayAdapter
    private CardArrayRecyclerViewAdapter mCardArrayAdapter;
    private CardRecyclerView mRecyclerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_activity);
        ArrayList<Card> cards = new ArrayList<>();

        mCardArrayAdapter = new CardArrayRecyclerViewAdapter(this, cards);

        //Staggered grid view
        CardRecyclerView mRecyclerView = (CardRecyclerView) findViewById(R.id.card_recyclerview);
        mRecyclerView.setHasFixedSize(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        //Set the empty view
        if (mRecyclerView != null) {
            mRecyclerView.setAdapter(mCardArrayAdapter);
        }

        //Load cards
        new LoaderAsyncTask().execute();
    }

    private ArrayList<Card> initCard() {

        ArrayList<Card> cards = new ArrayList<Card>();

        for (int i = 0; i < TOTAL_CARDS; i++) {


            MaterialLargeImageCard card = new MaterialLargeImageCard(this);
            //card.setInnerLayout(R.layout.native_material_largeimage_text_card);
            card.setTextOverImage("Italian Beaches");
            card.setDrawableIdCardThumbnail(R.drawable.card_background_01);

            //Set the title and subtitle in the card
            card.setTitle("This is my favorite local beach");
            card.setSubTitle("A wonderful place");

            // Set supplemental actions
            TextSupplementalAction t1 = new TextSupplementalAction(this, R.id.action1);
            t1.setOnActionClickListener(new BaseSupplementalAction.OnActionClickListener() {
                @Override
                public void onClick(Card card, View view) {
                    Toast.makeText(AboutActivity.this, " Click on Text SHARE ", Toast.LENGTH_SHORT).show();
                }
            });
            card.addSupplementalAction(t1);

            //Set the layout for supplemental actions
            card.setLayout_supplemental_actions_id(R.layout.about_card_supplemental_actions);

            //Very important call: build the card
            card.build();
            cards.add(card);

        }

        return cards;
    }

    private void updateAdapter(ArrayList<Card> cards) {
        if (cards != null) {
            mCardArrayAdapter.addAll(cards);
        }
    }

    class LoaderAsyncTask extends AsyncTask<Void, Void, ArrayList<Card>> {

        LoaderAsyncTask() {
        }

        @Override
        protected ArrayList<Card> doInBackground(Void... params) {
            //elaborate images
            //SystemClock.sleep(1000); //delay to simulate download, don't use it in a real app

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

        }

        @Override
        protected void onPostExecute(ArrayList<Card> cards) {
            //Update the adapter
            updateAdapter(cards);
            //displayList();
        }
    }
}

更新:

material_card.xml:

<it.gmariotti.cardslib.library.view.CardViewNative
    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"
    style="@style/native_recyclerview_card.base"
    card:card_layout_resourceID="@layout/native_material_largeimage_text_card"/>

在布局xml中:

<it.gmariotti.cardslib.library.recyclerview.view.CardRecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/myCardList"
        card:list_card_layout_resourceID="@layout/material_card" />

在 For 循环内:

ArrayList<BaseSupplementalAction> actions = new ArrayList<BaseSupplementalAction>();

            // Set supplemental actions
            TextSupplementalAction t1 = new TextSupplementalAction(this, R.id.action1);
            t1.setOnActionClickListener(new BaseSupplementalAction.OnActionClickListener() {
                @Override
                public void onClick(Card card, View view) {
                    Toast.makeText(AboutActivity.this," Click on Text SHARE "+card.getTitle(),Toast.LENGTH_SHORT).show();
                }
            });
            actions.add(t1);



            //Create a Card, set the title over the image and set the thumbnail
            MaterialLargeImageCard card =
                    MaterialLargeImageCard.with(this)
                            .setTextOverImage("Italian Beaches "+i)
                            .setTitle("This is my favorite local beach "+i)
                            .setSubTitle("A wonderful place")
                            .useDrawableId(R.drawable.card_background_01)
                            .setupSupplementalActions(R.layout.about_card_supplemental_actions, actions)
                            .build();

            card.setOnClickListener(new Card.OnCardClickListener() {
                @Override
                public void onClick(Card card, View view) {
                    Toast.makeText(AboutActivity.this," Click on ActionArea ",Toast.LENGTH_SHORT).show();
                }
            });

            card.build(); 
            cards.add(card);

更新 2

做了一些实验后,我认为这可能是罪魁祸首

<it.gmariotti.cardslib.library.recyclerview.view.CardRecyclerView
        android:id="@+id/myCardList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="10dp"
        card:list_card_layout_resourceID="@layout/material_card" />

即使我将 material_card 重命名为其他名称,它也可以正常编译。我认为 "card:list_card_layout_resourceID="@layout/material_card"" 没有被触发。

更新 3

终于解决了。问题出在 xml 声明中。不小心我复制错了。 xmlns:card="http://schemas.android.com/apk/res-auto" 是正确的一个

非常感谢@Gabriele 的帮助。这个库简直太棒了:)

【问题讨论】:

  • Simplecity 将使用谷歌的新卡库!易于实施!
  • @twntee Yaa 但这个库太酷了,不容忽视。我从 v1.7 开始使用
  • 是时候向前看啦!
  • 哈哈...修复它。我的错。 xml 声明中的一个愚蠢错误可能会引起很多麻烦。

标签: android android-5.0-lollipop android-recyclerview cardslib


【解决方案1】:

你可以在这里找到一个例子:

https://github.com/gabrielemariotti/cardslib/blob/dev/demo/stock/src/main/java/it/gmariotti/cardslib/demo/fragment/nativeview/NativeRecyclerViewMaterialCardFragment.java

您的问题是布局。 您的 RecyclerView 使用此卡片布局: card:list_card_layout_resourceID="@layout/native_recyclerview_card_layout"

您应该使用带有 material_card_layout 的卡片布局。

示例(上例中使用的布局): https://github.com/gabrielemariotti/cardslib/blob/dev/demo/stock/src/main/res/layout/native_recyclerview_material_card_layout.xml

另外我建议您使用构建器而不是标准构造器来为您的卡使用。

注意。 我正在调查一个错误: https://github.com/gabrielemariotti/cardslib/issues/361

【讨论】:

  • 我已经更新了更改,但仍然无法看到材料卡。我只能看到基本卡。我已经更新了编辑中的更改。有什么我做错了吗?非常感谢您的帮助。
  • 可能罪魁祸首是 card:list_card_layout_resourceID="@layout/material_card"。请查看更新 2。
  • 解决了这个问题。更新了帖子。非常感谢:)
  • @Gabriele Mariotti,就我而言,我可以看到空白卡片列表。使用相同的方法
  • 嗨...卡片正在显示,但标题和字幕没有显示在素材卡片中。
猜你喜欢
  • 1970-01-01
  • 2019-08-13
  • 2015-10-27
  • 2021-09-27
  • 2015-03-02
  • 2016-03-11
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
相关资源
最近更新 更多