RecyclerView에 onItemClick 추가


- Activity


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public class Sample_LaunchActivity extends Activity_Base {
 
    // Components
    RecyclerView rv_main;
 
    // adapter
    LaunchAdapter launchAdapter;
 
    // List
    ArrayList<LaunchModel> arrayList_launch;
 
    @Override
    protected void doCreate(Bundle savedInstanceState) {
        super.doCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);
 
        LogUtil.d(GlobalDefine.TAG, "===== Sample_LaunchActivity doCreate =====");
    }
 
    @Override
    protected void initLayout() {
        LogUtil.d(GlobalDefine.TAG, "===== Sample_LaunchActivity initLayout =====");
 
        /**
         * findViewById
         */
        rv_main = (RecyclerView) findViewById(R.id.rv_main);
 
        /**
         * setListener
         */
    }
 
    @Override
    protected void initData() {
        LogUtil.d(GlobalDefine.TAG, "===== Sample_LaunchActivity initData =====");
 
        arrayList_launch = new ArrayList<>();
 
        /**
         * 리스트 생성
         */
        for (int i=0; i<10; i++)
        {
            LogUtil.i(GlobalDefine.TAG, "===== Sample_LaunchActivity initData i = " + i);
 
            LaunchModel model = new LaunchModel();
            model.setMenuName(String.valueOf(i));
 
            arrayList_launch.add(model);
        }
 
        launchAdapter = new LaunchAdapter(arrayList_launch);
        launchAdapter.setItemClick(onItemClick);
 
        rv_main.setLayoutManager(new LinearLayoutManager(this));
        rv_main.setAdapter(launchAdapter);
    }
 
    /**
     * recyclerView itemClick
     */
    LaunchAdapter.ItemClick onItemClick = new LaunchAdapter.ItemClick() {
        @Override
        public void onClick(View view, int position) {
            LogUtil.d(GlobalDefine.TAG, "===== Sample_LaunchActivity LaunchAdapter onItemClick pos : " + position);
        }
    };
 
    @Override
    protected void doResume() {
 
    }
 
    @Override
    protected void doPause() {
 
    }
 
    @Override
    protected void doDestroy() {
 
    }
 
 
}
cs




- RecyclerViewAdapter


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
public class LaunchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    // DEFINE
    private static final int DEFAULT_VIEWTYPE = 1;
 
    // Data
    private List<LaunchModel> launchList;
 
 
 
 
 
 
 
    //아이템 클릭시 실행 함수
    private ItemClick itemClick;
    public interface ItemClick {
        public void onClick(View view,int position);
    }
 
    //아이템 클릭시 실행 함수 등록 함수
    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }
 
    public LaunchAdapter(List<LaunchModel> contactList)
    {
        launchList = contactList;
    }
 
    @Override
    public int getItemCount() {
        return launchList.size();
    }
 
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder contactViewHolder, final int position)
    {
        LogUtil.i(GlobalDefine.TAG, "===== LaunchAdapter onBindViewHolder position = " + position);
 
        LaunchModel model = launchList.get(position);
 
        switch (contactViewHolder.getItemViewType())
        {
            case DEFAULT_VIEWTYPE:
                LaunchModelViewHolder viewHolder_default = (LaunchModelViewHolder) contactViewHolder;
 
                viewHolder_default.tv_menuName.setText(model.getMenuName());
 
                // onClick
                viewHolder_default.ll_mainLayout.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View view)
                    {
                        if (itemClick != null)
                        {
                            itemClick.onClick(view, position);
                        }
                    }
                });
                break;
        }
    }
 
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
    {
        View itemView = null;
 
        LogUtil.i(GlobalDefine.TAG, "===== LaunchAdapter onCreateViewHolder getMessageType : " + viewType);
 
        switch (viewType)
        {
            case DEFAULT_VIEWTYPE:
                itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_launch, viewGroup, false);
 
                return new LaunchModelViewHolder (itemView);
        }
 
        return null;
 
 
 
        // 클릭 리스너
//        itemView.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v)
//            {
//                int itemPosition = recyclerView_favoriteLst.getChildPosition(v);
//                String item = String.valueOf(al.get(itemPosition).favorite_price);
//                Toast.makeText(getActivity(), item, Toast.LENGTH_SHORT).show();
//            }
//        });
    }
 
    @Override
    public int getItemViewType(int position)
    {
//        return launchList.get(position).getMessageType();
        return DEFAULT_VIEWTYPE;
    }
 
    public class LaunchModelViewHolder extends RecyclerView.ViewHolder
    {
        LinearLayout ll_mainLayout;
        TextView tv_menuName;
 
        public LaunchModelViewHolder (View v)
        {
            super(v);
 
            ll_mainLayout = (LinearLayout) v.findViewById(R.id.row_launch_ll_mainLayout);
            tv_menuName = (TextView) v.findViewById(R.id.row_launch_tv_menuName);
        }
    }
};
cs


이 글을 공유하기


댓글(0)

Designed by CMSFactory.NET