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だと思いますが、recyclerviewに設定card:list_card_layout_resourceID="@layout/my_material_card_layout" して

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 に答える