RecyclerView与卡。为每张卡开启新活动

时间:2017-03-16 21:57:09

标签: android android-layout android-fragments android-cardview

在我的项目中并包含了recyclerview和卡片,问题在于我想为每张卡片调用活动。我已经尝试了其他方法,但我无法解决我的问题。

RVAdapter.java

    package com.example.ivann.card2;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * Created by ivann on 15/03/2017.
 */

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
    private Context context;

    public RVAdapter(Context context){
        this.context = context;
    }
    public class PersonViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView personName;
        TextView personAge;
        ImageView personPhoto;

        public PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            personName = (TextView)itemView.findViewById(R.id.person_name);
            personAge = (TextView)itemView.findViewById(R.id.person_age);
            personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);


     //Problem
            itemView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {

                    final Intent intent;
                    if (getAdapterPosition() == 0){
                        intent =  new Intent(context, LaveryActivity.class);
                    } else if (getAdapterPosition() == 1){
                        intent =  new Intent(context, emma.class);
                    } else {
                        intent =  new Intent(context, lilie.class);
                    }
                    context.startActivity(intent);
                }
           });

        }
    }

    List<Person> persons;

    RVAdapter(List<Person> persons){
        this.persons = persons;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
        personViewHolder.personName.setText(persons.get(i).name);
        personViewHolder.personAge.setText(persons.get(i).age);
        personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
    }

    @Override
    public int getItemCount() {
        return persons.size();
    }
}

CardViewActivity.java

package com.example.ivann.card2;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;


public class CardViewActivity extends Activity{

    TextView personName;
    TextView personAge;
    ImageView personPhoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.cardview_activity);
        personName = (TextView)findViewById(R.id.person_name);
        personAge = (TextView)findViewById(R.id.person_age);
        personPhoto = (ImageView)findViewById(R.id.person_photo);

        personName.setText("Emma Wilson");
        personAge.setText("23 years old");
        personPhoto.setImageResource(R.drawable.emma);
    }
}

日志

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.ivann.card2, PID: 13042
              java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                  at android.content.ComponentName.<init>(ComponentName.java:128)
                  at android.content.Intent.<init>(Intent.java:4868)
                  at com.example.ivann.card2.RVAdapter$PersonViewHolder$1.onClick(RVAdapter.java:44)
                  at android.view.View.performClick(View.java:5610)
                  at android.view.View$PerformClick.run(View.java:22265)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Application terminated.

3 个答案:

答案 0 :(得分:0)

只是取消注释这一行。这就是它应该如何完成的。

itemView.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(v.getContext(), LaveryActivity.class);
        v.getContext().startActivity(intent);
});

答案 1 :(得分:0)

你的cardview clicklistener应该在onBindViewHolder()

Context context; //Declare  globally

    personViewHolder.cv.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {

     Intent i =new Intent(context, LaveryActivity.class);
                    context.startActivity(i);

                }
           });

答案 2 :(得分:0)

实际上你可以直接从RecyclerView Adapter .....中调用Intent。

从您的活动中删除Recyclerview的addOnItemTouchListener两者都无法合作......

在你Activity ..

 RVAdapter adapter = new RVAdapter(persons,RecyclerViewActivity.this);
 rv.setAdapter(adapter);

尝试在适配器构造函数中传递活动对象。

 RVAdapter(List<Person> persons,Activity activity){
        this.persons = persons;
        this.activity=activity;
    }

使用itemView ClickListener,但在onBindViewHolder()

itemView.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(activity, LaveryActivity.class);
        activity.startActivity(intent);
});