2

我正在尝试使用 Cardslib 创建一个带有自定义布局行的 RecyclerView,当我单击它们上的按钮时,这些行会展开。我按照自定义指南创建了自定义布局和自定义卡片类,卡片确实像这样正确显示,在此处输入图像描述
但我不知道如何为这张卡片上的视图元素设置点击事件。

在 activity_main.xml 中,我有一个 CardRecyclerView:

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

my_cardview_layout.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"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
card:card_layout_resourceID="@layout/my_material_card_layout"
style="@style/card_external"
android:layout_marginTop="12dp" />

my_material_card_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<it.gmariotti.cardslib.library.view.ForegroundLinearLayout
    android:id="@+id/card_main_layout"
    style="@style/card.native.main_layout_foreground"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<ImageView
    android:layout_width="match_parent"
    android:layout_height="210dp"
    android:id="@+id/mycard_image"
    android:src="@drawable/sea"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Title"
    android:id="@+id/mycard_title"
    android:paddingTop="24dp"
    android:paddingLeft="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Subtitle"
    android:id="@+id/mycard_subtitle"
    android:paddingLeft="20dp" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/actions_padding"
    android:paddingLeft="@dimen/actions_padding_left"
    android:paddingRight="@dimen/actions_padding_left">

    <TextView
        android:id="@+id/mycard_suppaction1"
        android:text="SHARE"
        android:background="?android:selectableItemBackground"
        android:layout_width="wrap_content"
        style="@style/card.native.actions"
        android:layout_height="wrap_content"/>


    <TextView
        android:id="@+id/mycard_suppaction2"
        android:text="LEARN MORE"
        android:background="?android:selectableItemBackground"
        android:layout_width="wrap_content"
        style="@style/card.native.actions"
        android:layout_height="wrap_content"/>
</LinearLayout>

</it.gmariotti.cardslib.library.view.ForegroundLinearLayout>


<FrameLayout
    android:id="@+id/card_content_expand_layout"
    style="@style/card.native.main_contentExpand"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
</FrameLayout>

我的主要活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    //CardViewNative cardView = (CardViewNative) findViewById(R.id.myMaterialCardView);

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

        MyMaterialCard card = new MyMaterialCard(this);


        CardExpand expand = new CardExpand(getApplicationContext());
        //Set inner title in Expand Area
        expand.setTitle(" Hidden content");
        card.addCardExpand(expand);
        card.setId("" + i);

        cards.add(card);
    }

    CardArrayRecyclerViewAdapter mCardArrayAdapter = new CardArrayRecyclerViewAdapter(this, cards);

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

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

}

我的自定义卡类:

public class MyMaterialCard extends Card {

ImageView mImage;
TextView mTitle;
TextView mSubtitle;
TextView mActionButton1;
TextView mActionButton2;

public MyMaterialCard(Context context) {
    super(context, R.layout.my_cardview_layout);
}

public MyMaterialCard(Context context, int innerLayout) {
    super(context, innerLayout);
}

@Override
public void setupInnerViewElements(ViewGroup parent, View view) {

    mImage = (ImageView) view.findViewById(R.id.mycard_image);
    mTitle = (TextView) view.findViewById(R.id.mycard_title);
    mSubtitle = (TextView) view.findViewById(R.id.mycard_subtitle);
    mActionButton1 = (TextView) view.findViewById(R.id.mycard_suppaction1);
    mActionButton2 = (TextView) view.findViewById(R.id.mycard_suppaction2);

    mActionButton1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(), "action 1", Toast.LENGTH_SHORT).show();

        }
    });

    ViewToClickToExpand viewToClickToExpand =
            ViewToClickToExpand.builder()
                    .setupView(mActionButton2);
    setViewToClickToExpand(viewToClickToExpand);
}}

我认为问题可能是 my_cardview_layout.xml,但是,如果我card:list_card_layout_resourceID="@layout/my_material_card_layout" 在 recyclerview 中设置并且

public MyMaterialCard(Context context) {
super(context, R.layout.my_material_card_layout);}

它们不会显示为卡片: 在此处输入图像描述

已经为此工作了 2 天,仍然无法让这些按钮工作(尝试使用 Cardslib 材料卡,但添加扩展时,没有任何反应)。非常感谢任何帮助,谢谢!

4

1 回答 1

0

在主要活动上,添加函数 setOnClickListener:

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

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


        CardExpand expand = new CardExpand(getApplicationContext());
        //Set inner title in Expand Area
        expand.setTitle(" Hidden content");
        card.addCardExpand(expand);
        card.setId("" + i);

        cards.add(card4);
    }

我认为这可以帮助一些人,我希望你不会 xD(1 年,10 个月前)

于 2017-05-03T13:49:05.580 回答