1: static private class ArticleListAdapter extends RecyclerView.Adapter<ArticleListAdapter.ArticleItem>
2: {
3: ArrayList<Article> mList = null;
4: public ArticleListAdapter(ArrayList<Article> articles)
5: {
6: mList = articles;
7: }
8:
9: @Override
10: public ArticleItem onCreateViewHolder(ViewGroup parent, int viewType) {
11: //새로운 itemView가 필요시마다 호출된다.
12:
13: LayoutInflater inflater = LayoutInflater.from(MyApplication.mContext);
14:
15: View rootView = inflater.inflate(R.layout.fragment_board_article_row_item,parent,false);
16:
17: return new ArticleItem(rootView);
18: }
19:
20: @Override
21: public void onBindViewHolder(ArticleItem holder, int position) {
22: /* itemView가 ArticleItem holder로 넘어오면 우리는 그냥 받아 쓰면된다.
23: 그것이 화면에서 사라져 재활용되기 위해서 온 view일수도 있고, 새로 만들어진 view일수도있지만
24: 사용자는 그것에 대해 신경쓸필요없다.*/
25: Article article = mList.get(position);
26: holder.setTitle(article.getTitle());
27: switch (article.getType())
28: {
29: case "normal":
30: holder.setType(ArticleItem.ArticleType.NORMAL);
31: break;
32: case "notice":
33: holder.setType(ArticleItem.ArticleType.NOTICE);
34: break;
35: }
36: holder.setDate(article.getDate());
37: holder.setNick(article.getNickName());
38:
39:
40:
41: }
42:
43: @Override
44: public int getItemCount() {
45: return mList.size();
46: }
47:
48: /*
49: * 이전까지 썻던 ViewHolder패턴에서 직접 만들어 쓰던 ViewHolder다.
50: * 이제는 만들어 쓸필요없이, RecyclerView에서 자체적으로 제공해주는것을
51: * 상속해 쓰기만 하면된다.
52: * */
53: static class ArticleItem extends RecyclerView.ViewHolder
54: {
55: enum ArticleType {NOTICE,NORMAL}
56: TextView mTitleView = null;
57: TextView mDateView = null;
58: TextView mNickView = null;
59: TextView mArticleTypeView = null;
60:
61: public ArticleItem(View itemView) {
62: super(itemView);
63: mDateView = (TextView) itemView.findViewById(R.id.articleDate);
64: mTitleView = (TextView) itemView.findViewById(R.id.articleTitle);
65: mArticleTypeView = (TextView) itemView.findViewById(R.id.articleType);
66: mNickView = (TextView) itemView.findViewById(R.id.author);
67: }
68: public void setDate(String date)
69: {
70: mDateView.setText(date);
71: }
72: public void setTitle(String title)
73: {
74: mTitleView.setText(title);
75: }
76: public void setType(ArticleType type)
77: {
78: if(type == ArticleType.NOTICE)
79: {
80: mArticleTypeView.setText("공지");
81: }
82: else
83: {
84: mArticleTypeView.setText("일반");
85: }
86: }
87: public void setNick(String nick)
88: {
89: mNickView.setText(nick);
90: }
91: }
92: }