如何从cardview开始活动。我正在尝试从我的CardViewHolder类启动其他fragmentActivity,但应用程序正在崩溃

时间:2015-12-23 12:23:33

标签: android android-cardview

public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener {

public TextView countryName;
public ImageView countryPhoto;
public RecyclerViewHolders(View itemView) {
    super(itemView);
    itemView.setOnClickListener(this);
    countryName = (TextView)itemView.findViewById(R.id.tv_cardTitle);
    countryPhoto = (ImageView)itemView.findViewById(R.id.iv_cardImages);
}
@Override
public void onClick(View view) {

    Toast.makeText(view.getContext(), "Clicked Country Position = " + getPosition(), Toast.LENGTH_SHORT).show();
}

}

1 个答案:

答案 0 :(得分:0)

在RecyclerView的适配器类中的onCreateView()方法中,您需要获取对您的卡或您想要点击的任何内容的引用,并且在附加您的卡时还需要传递Context变量适配器RecyclerView

最后,在您的Activity中编写一个方法,如下所示:

public void openAnotherActivity() {
    Intent intent = new Intent(this, NewActivity.class);
    startActivity(intent);
}

在您的适配器中:

whateverItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ((Activity)context).openAnotherActivity();
                    }
                });