Cardview onclick活动

时间:2017-03-22 21:38:24

标签: android android-recyclerview android-cardview

我正在使用cardview创建一个回收者视图,当点击cardview时,它会打开一个新的活动,而且我需要传递我的firebase数据库的子名称/ id,我已经查看了很多教程,但我不是了解如何实现,但徒劳无功,以下是我的代码我已经使itemClickListener接口传递视图和位置。

public class MedicalData扩展了AppCompatActivity {

private RecyclerView patientdata;
private DatabaseReference mDatabase;

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

    mDatabase = FirebaseDatabase.getInstance().getReference().child("Patients");

    patientdata = (RecyclerView) findViewById(R.id.patientdata);
    patientdata.setHasFixedSize(true);
    patientdata.setLayoutManager(new LinearLayoutManager(this));


}

@Override
public void onStart() {
    super.onStart();
    FirebaseRecyclerAdapter<PatientRequest, DataViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<PatientRequest, DataViewHolder>(

            PatientRequest.class,
            R.layout.datatype,
            DataViewHolder.class,
            mDatabase
    ) {

        @Override
        public void populateViewHolder(DataViewHolder viewHolder, PatientRequest model, int position) {

            viewHolder.setName(model.getPatientName());
            viewHolder.setAge(model.getPatientAge());

            viewHolder.setonClickListener(new ItemClickListener() {
                @Override
                public void onItemClick(View v, int pos) {
                    Intent int2 = new Intent(MedicalData.this, ShowPatientDetails.class);

                    MedicalData.this.startActivity(int2);
                }
            });


        }
    };
    patientdata.setAdapter(firebaseRecyclerAdapter);
}

public static class DataViewHolder extends RecyclerView.ViewHolder implements  View.OnClickListener {

    View mView;
    ItemClickListener itemClickListener;


    public DataViewHolder(View itemView) {
        super(itemView);

        mView = itemView;

        itemView.setOnClickListener(this);


    }

    public void setName(String name) {
        TextView pname = (TextView) mView.findViewById(R.id.pname1);
        pname.setText(name);
    }

    public void setAge(String age) {
        TextView page = (TextView) mView.findViewById(R.id.page1);
        page.setText(age);
    }


    @Override
    public void onClick(View v) {

        this.itemClickListener.onItemClick(v , getLayoutPosition());
    }

    public void setonClickListener(ItemClickListener ic)
    {
        this.itemClickListener=ic;
    }
}

}

1 个答案:

答案 0 :(得分:0)

声明cardview

 static class ProductViewHolder extends RecyclerView.ViewHolder{
    private TextView tvPatientName;
    private TextView tvPatientAge;
    private CardView cvPatient;

    public ProductViewHolder(View itemView) {
        super(itemView);            
        tvPatientName = (TextView)itemView.findViewById(R.id.tvPatientName);
        tvPatientAge = (TextView)itemView.findViewById(R.id.tvPatientAge);            
        cvPatient = (CardView)itemView.findViewById(R.id.cvPatient);            
    }
}

然后在popluateViewHolder

@Override
    public void populateViewHolder(DataViewHolder viewHolder, PatientRequest model, int position) {

        viewHolder.tvPatientName.setText(model.getPatientName());
        viewHolder.tvPatientAge.setText(model.getPatientAge());
        viewHolder.cvPatient.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Log.d("Tag", "Cardview clicked"+model.getPatientName());
            }
        });
    }

希望这有帮助