将RecyclerView中的CardView数据发送到新片段?

时间:2016-06-09 15:09:54

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

所以我在RecyclerView中有一个CardView,里面有一些数据。我想点击一张特定的卡片并启动一个新片段,该片段显示CardView中数据的特定数据(有关CardView中数据的更多详细信息)。这是相关的代码。

我在编程和Android方面没有最丰富的经验,任何帮助都将不胜感激!

DetailsFragment:`

public class DetailsFragment extends Fragment {

private CardView cardView;

private RecyclerView rv;

private List<Detail> detailList = new ArrayList<>();


public DetailsFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_details, container, false);

    Context context = getActivity();
    cardView = (CardView) rootView.findViewById(R.id.cv);
    rv = (RecyclerView) rootView.findViewById(R.id.rv);

    final LinearLayoutManager llm = new LinearLayoutManager(context);
    rv.setLayoutManager(llm);

    RVDetailAdapter adapter = new RVDetailAdapter(detailList);
    rv.setAdapter(adapter);

    initializeData();
    initializeAdapter();

    return rootView;

}

private void initializeData() {
    detailList = new ArrayList<>();
    //        not sure here
    detailList.add(new Detail(" "));
}

private void initializeAdapter(){
    RVDetailAdapter adapter = new RVDetailAdapter(detailList);
    rv.setAdapter(adapter);
}

}

RVNewsAdapater:

public class RVNewsAdapter extends RecyclerView.Adapter<RVNewsAdapter.NewsViewHolder> {

public static final String KEY_LINK ="link";

public static class NewsViewHolder extends RecyclerView.ViewHolder
    implements View.OnClickListener {
    private static final String TAG = "Hello";
    CardView cv;
    TextView date;
    TextView link;
    TextView title;
    TextView today;


    public NewsViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.cv);
        date = (TextView) itemView.findViewById(R.id.date);
        link = (TextView) itemView.findViewById(R.id.link);
        title = (TextView) itemView.findViewById(R.id.heading);
        today = (TextView) itemView.findViewById(R.id.today);

        itemView.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        Context context = view.getContext();

        Intent intent = null;

        for(int postion = 0; postion<42; postion++) {
           intent = new Intent(context, Details.class);

        }

        context.startActivity(intent);
    }



}

List<News> news;

RVNewsAdapter(List<News> news) {
    this.news = news;
}

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

@Override
public RVNewsAdapter.NewsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.itemnewsfeed, viewGroup, false);
    NewsViewHolder nvh = new NewsViewHolder(v);
    return nvh;
}

@Override
public void onBindViewHolder(NewsViewHolder newsViewHolder, int i) {
    newsViewHolder.date.setText(news.get(i).date);
    newsViewHolder.link.setText(news.get(i).link);
    newsViewHolder.title.setText(news.get(i).title);
    newsViewHolder.today.setText(news.get(i).today);
}

@Override
public int getItemCount() {
    if (news != null) {
        return news.size();
    }
    return 0;
}
}

RVDetailAdapter:

public class RVDetailAdapter extends RecyclerView.Adapter<RVDetailAdapter.DetailViewHolder> {


public static class DetailViewHolder extends RecyclerView.ViewHolder {
    TextView link;

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

        link = (TextView) itemView.findViewById(R.id.link);
    }
}

List<Detail> detail;

RVDetailAdapter(List<Detail> detail) {
    this.detail = detail;
}

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

@Override
public RVDetailAdapter.DetailViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.itemnewsfeed, viewGroup, false);
    DetailViewHolder dvh = new DetailViewHolder(v);
    return dvh;
}

@Override
public void onBindViewHolder(DetailViewHolder detailViewHolder, int i) {
    detailViewHolder.link.setText(detail.get(i).link);

}

@Override
public int getItemCount() {
    if (detail != null) {
        return detail.size();
    }
    return 0;
}
}

1 个答案:

答案 0 :(得分:0)

不确定您是否还在寻找答案,但我刚看到帖子并认为我给了您一个选项。

当我使用RecyclerView时(无论是否使用CardView,对于任何列表项都是如此),我创建了一个点击监听器适配器中的视图项。当用户点击项目/ cardview时,我会发送项目点击事件。您可以选择如何响应 - 您可以使用标准回调接口(并在创建适配器时传入监听器)或使用EventBus之类的东西。

以下是您要添加到适配器类的代码示例:

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
    View view = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            itemClick(view);
        }
    });

    return new ViewHolder(view);
}   

private void itemClick(View view) {
    //Handle item event here
    //EventBus.getDefault().post(new ItemSelectedEvent(/*send apprpopriate data*/));
}