[안드로이드] study day3

승아·2020년 9월 24일
0

RecyclerView 클릭 이벤트 구현

클릭 구현 부분

 mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), mRecyclerView, new ClickListener() {
            @Override
            public void onClick(View view, int position) {
                VolunteerData vdata = myDataset.get(position);
                // Toast.makeText(getActivity(), vdata.writer +"," + vdata.title+",", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getActivity(), VolunteerActivity.class);
                intent.putExtra("title", vdata.getTitle() );
                intent.putExtra("date", vdata.getDate() );
                intent.putExtra("writer", vdata.getWriter() );
                startActivity(intent);
            }

            @Override
            public void onLongClick(View view, int position) {
            }
        }));

클릭리스너 클래스

public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

        private GestureDetector gestureDetector;
        private VolunteerFragment.ClickListener clickListener;

        public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final VolunteerFragment.ClickListener clickListener) {
            this.clickListener = clickListener;
            gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (child != null && clickListener != null) {
                        clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child));
                    }
                }
            });
        }

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
                clickListener.onClick(child, rv.getChildAdapterPosition(child));
            }
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        }
    }

https://webnautes.tistory.com/1300

0개의 댓글