单击CardView加载片段

时间:2015-06-09 05:37:41

标签: android android-fragments android-recyclerview android-cardview

我在使用RecyclerView实施可点击卡时遇到问题。我想获得点击哪张卡的位置,然后加载正确的片段。

我确实看了一下this post并花了近30分钟试图理解代码并自己实现它但当我意识到代码片段中有多少错误时我放弃了。

这是我的适配器类。

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {

public static CardView cv;

public static class PersonViewHolder extends RecyclerView.ViewHolder {
    TextView chapterName;
    TextView chapterNumber;
    // ImageView chapterPhoto;

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

        cv = (CardView) itemView.findViewById(R.id.cv);
        chapterName = (TextView) itemView.findViewById(R.id.chapter_name);
        chapterNumber = (TextView) itemView.findViewById(R.id.chapter_number);
        // chapterPhoto = (ImageView) itemView.findViewById(R.id.person_photo);

    }
}

List<Chapter> chapters;

RVAdapter(List<Chapter> chapters) {
    this.chapters = chapters;
}

@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.chapterName.setText(chapters.get(i).chapterName);
    personViewHolder.chapterNumber.setText(chapters.get(i).chapterNumber);
    // personViewHolder.chapterPhoto.setImageResource(persons.get(i).photoId);
}

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

我已经查看了StackOverflow上的每个实现,要么我有隧道视觉,要么没有实际工作。我知道旁边有很多帖子,但在问这个问题之前我确实花了不少时间寻找答案,所以所有这些链接对我来说都是紫色的。

感谢所有帮助,我非常乐意发布任何其他可能有用的代码!

2 个答案:

答案 0 :(得分:2)

todo你需要使用自己的Clicklistener将OnClickListener添加到ViewHolder,如下所示:

新界面:

public interface PersonViewHolderClickListener {

    void onItemClick(long id);
}

并像这样更改您的RVAdapter类:

protected Context mContext;

RVAdapter(Context context, List<Chapter> chapters) {
    mContext = context;
    this.chapters = chapters;
}

public static class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView chapterName;
    TextView chapterNumber;
    // ImageView chapterPhoto;

    private long PERSONId;

    public PersonViewHolderClickListener clickListener;

    public PersonViewHolder(View itemView,PersonViewHolderClickListener clickListener) {
        super(itemView);

        cv = (CardView) itemView.findViewById(R.id.cv);
        chapterName = (TextView) itemView.findViewById(R.id.chapter_name);
        chapterNumber = (TextView) itemView.findViewById(R.id.chapter_number);
        // chapterPhoto = (ImageView) itemView.findViewById(R.id.person_photo);

        this.clickListener = clickListener;

        view.setOnClickListener(this);  
    }

    @Override
    public void onClick(View v) {
        clickListener.onItemClick(PERSONId);
    }
}

@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, new PersonViewHolderClickListener() {

        @Override
        public void onItemClick(long id) {
            Intent intent = new Intent(mContext, YOURActivity.class);
            intent.putExtra(EXTRA_PERSON_ID, id);
            mContext.startActivity(intent);
        }
    });
    return pvh;
}

@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
    personViewHolder.chapterName.setText(chapters.get(i).chapterName);
    personViewHolder.chapterNumber.setText(chapters.get(i).chapterNumber);
    // personViewHolder.chapterPhoto.setImageResource(persons.get(i).photoId);
    personViewHolder.PERSONId = i;
    }

答案 1 :(得分:1)

我的完整工作代码,感谢@ fmt.Println.MKO。

<强> RVAdapter.java:

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.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> implements View.OnClickListener {

    Context mContext;

    RVAdapter(Context context, List<Chapter> chapters) {
        mContext = context;
        this.chapters = chapters;
    }

    public static CardView cv;
    public static int position;

    public class PersonViewHolder extends RecyclerView.ViewHolder {
        TextView chapterName;
        TextView chapterNumber;
        // ImageView chapterPhoto;

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

            cv = (CardView) itemView.findViewById(R.id.cv);
            chapterName = (TextView) itemView.findViewById(R.id.chapter_name);
            chapterNumber = (TextView) itemView.findViewById(R.id.chapter_number);
            // chapterPhoto = (ImageView) itemView.findViewById(R.id.person_photo);

            itemView.setOnClickListener(RVAdapter.this);
            position = getPosition();
        }
    }

    @Override
    public void onClick(View v) {

            Intent intent = new Intent(mContext, ChapterActivity.class);
            mContext.startActivity(intent);
    }

    List<Chapter> chapters;

    @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.chapterName.setText(chapters.get(i).chapterName);
        personViewHolder.chapterNumber.setText(chapters.get(i).chapterNumber);
        // personViewHolder.chapterPhoto.setImageResource(persons.get(i).photoId);

    }

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

}

使用RecyclerView的片段:

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class UnitOneFragment extends android.support.v4.app.Fragment {

    private List<Chapter> chapters;
    private RecyclerView rv;
    private RecyclerView.LayoutManager llm;

    public UnitOneFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_unit_one, container, false);

        rv = (RecyclerView) v.findViewById(R.id.rv);
        rv.setHasFixedSize(true);

        llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);

        initializeData();
        initializeAdapter();

        return v;
    }

    private void initializeData() {
        chapters = new ArrayList<>();
        chapters.add(new Chapter("Human Prehistory to the Early Civilizations", "Chapter One"));
        chapters.add(new Chapter("Classical China", "Chapter Two"));
        chapters.add(new Chapter("Classical India", "Chapter Three"));
        chapters.add(new Chapter("Classical Greece and Rome", "Chapter Four"));
        chapters.add(new Chapter("Classical Period: Declines and Diversities", "Chapter Five"));
    }

    private void initializeAdapter() {
        RVAdapter adapter = new RVAdapter(getActivity(), chapters);
        rv.setAdapter(adapter);
    }

}

class Chapter {
    String chapterName;
    String chapterNumber;
    // int photoId;

    Chapter(String chaptername, String chapternumber) {
        this.chapterName = chaptername;
        this.chapterNumber = chapternumber;
        // this.photoId = photoId;
    }
}