Develop/Android
Android RecyclerView ItemClcik 하기
dlsdnd345
2016. 2. 14. 18:29
Android RecyclerView ItemClcik 하기
android RecyclerView 은 listView 에서 사용한 OnItemClcikLister 가 없습니다.
제스쳐를 통해서 하는 방법과 ViewHolder를 사용한 방법이 있습니다.
저는 아래와 같이 ViewHolder를 사용한 방식을 사용해 보았습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public ImageView img; public TextView textTitle; public TextView textContent; public ViewHolder(View itemView) { super(itemView); itemView.setOnClickListener(this); img = (ImageView) itemView.findViewById(R.id.img); textTitle = (TextView) itemView.findViewById(R.id.textTitle); textContent = (TextView) itemView.findViewById(R.id.textContent); } @Override public void onClick(View v) { System.out.println(getPosition()); Intent intent = new Intent(v.getContext() , DetailActivity.class); v.getContext().startActivity(intent); } } | cs |
위와 같이 인터페이스를 통해 클릭이벤트를 부여 하고 onClick 에서 클릭을 사용 할 수 있습니다.
해당 포지션은 getPosition() 을 통해서 얻어 올수 있고,
Context 는 해당 view 에서 얻어 올수 있습니다.