从Cardlist Item数组开始新的Activity

时间:2015-09-25 13:08:10

标签: android android-layout android-intent android-activity

我想从以下每个项目开始新的活动我该怎么做 这是我的卡适配器活动的片段

package com.masiliose.denta;

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 java.util.ArrayList;
import java.util.List;




public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

    List<NatureItem> mItems;

    public CardAdapter() {
        super();
        mItems = new ArrayList<NatureItem>();
        NatureItem nature = new NatureItem();
        nature.setName("HIV / AIDS");
        nature.setDes("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt" +
                      "ut labore et dolore magna aliqua. Ut enim ad minim veniam.");
        nature.setThumbnail(R.drawable.ccc);
        mItems.add(nature);

1 个答案:

答案 0 :(得分:0)

希望它会对你有所帮助。

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder>{

private List<NatureItem> mItems;
private Context mContext;

/*
pass a context via consectetur, it will be necessary to start activity on click card
*/
public CardAdapter(Context mContext){
    this.mContext = mContext;

    mItems = new ArrayList<NatureItem>();
    NatureItem nature = new NatureItem();
    nature.setName("HIV / AIDS");
    nature.setDes("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt" +
                  "ut labore et dolore magna aliqua. Ut enim ad minim veniam.");
    nature.setThumbnail(R.drawable.ccc);
    mItems.add(nature);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    /*
    your custom card item layout
    */
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {

    final String name = mItems.get(i).getName();

    viewHolder.nameView.setName(name);
    /*.......
    set values to other view's 
    .......*/

    viewHolder.cv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext, YourActivity.class);
            mContext.startActivity(intent);
        }
    });


}

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

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

public static class ViewHolder extends RecyclerView.ViewHolder {
    CardView cv;
    TextView nameView;

    ViewHolder(View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.cardView);
        nameView = (TextView)itemView.findViewById(R.id.nameView);

    }
} }