卡片视图点击卡片移动到新活动

Nex*_*nly 18 android cardlayout

我是Android编程的新手,正在开发卡片布局.我想知道,如何让它可点击?

android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)

我在我的卡片小部件上有这个,然后我想知道在哪里点击可点击的动作?我希望能够单击该卡,它获取卡的ID,然后显示新的意图活动

这是我加载适配器的活动的代码

setContentView(R.layout.activity_my);


    RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
    recList.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recList.setLayoutManager(llm);

    ContactAdapter ca = new ContactAdapter(createList(30));

    recList.setAdapter(ca);
Run Code Online (Sandbox Code Playgroud)

Mah*_*nei 21

在你的适配器java文件和"ViewHolder"里面你会发现:

public ContactViewHolder(final View v) {
    super(v);
}
Run Code Online (Sandbox Code Playgroud)

写密码:

v.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        v.getContext().startActivity(new Intent(v.getContext(), YOUR_ACTIVITY_TO_START.class));
    }
});
Run Code Online (Sandbox Code Playgroud)


小智 9

如果您正确使用了实现,您的代码应如下所示:

card - is the card view you instantiated to display on your ui


card.setOnClickListener(...);
Run Code Online (Sandbox Code Playgroud)

在onClickListener的实现中,你应该有:

@Override
public void onClick(Card c ,View v) {
    Intent intent = new Intent(MyActivity.this, NextActivity.class);
    startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)

这几乎就是你从卡上开始新活动所需的一切


小智 5

Addind onClick to the cardView did it for me:

     <android.support.v7.widget.CardView
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:id="@+id/bankcardId"
            android:layout_width="160dp"
            android:layout_height="190dp"
            android:layout_margin="10dp"
            android:onClick="P1_bay">
Run Code Online (Sandbox Code Playgroud)

Then call it in your java function as below:

    public void P1_bay(View view) {
    Toast.makeText(this, "You have clicked P1", Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)