상식닷컴
로그인
가입하기
2026년 상식닷컴 선정 식당 & 카페 리스트
2025년 2026년 신상 호텔 리스트
최근에 오픈한 호텔을 찾는다면 살펴보세요
일주일 식단표 어플
자동 일주일 식단표 어플
안드로이드
아이폰
주식 & 코인 차트의 신
1000만원으로 2000만원 만들기 프로젝트
수정하기 - HorizontalScrollView에 페이징 기능을 추가하려면 어떻게 해야 하나요?
닉네임
비밀번호
제목
내용
[이미지 업로드는 권한이 있는 사람만 가능. 하단 카톡으로 연락]
`HorizontalScrollView`에 페이징 기능을 추가하는 것은 `ViewPager`나 `ViewPager2`를 사용하는 것이 일반적입니다. 그러나 `HorizontalScrollView`를 사용하여 수동으로 페이징 기능을 구현하고 싶다면, 다음과 같은 단계를 따라 할 수 있습니다. 1. 레이아웃 정의 우선, `HorizontalScrollView`와 그 안에 자식 뷰들을 포함하는 레이아웃을 정의하십시오. ```xml <HorizontalScrollView android:id="@+id/horizontalScrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <LinearLayout android:id="@+id/container" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal"> <!-- 여기에 여러 개의 View를 추가하세요 --> <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_blue_light" /> <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_green_light" /> <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_red_light" /> <!-- 추가적인 뷰들... --> </LinearLayout> </HorizontalScrollView> ``` 2. 페이징 기능 구현 `HorizontalScrollView`에 페이징 기능을 부여하기 위해, 사용자가 스와이프할 때마다 적절한 위치로 스크롤을 이동하도록 설정합니다. 이를 위해 `<a href='https://sangseek.com/sangseeks/GestureDetector/ko'>GestureDetector</a>`를 사용해서 터치 이벤트를 처리할 수 있습니다. ```java import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.widget.HorizontalScrollView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private HorizontalScrollView horizontalScrollView; private LinearLayout container; private int currentPage = 0; private int pageWidth; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); horizontalScrollView = findViewById(R.id.horizontalScrollView); container = findViewById(R.id.container); // 각 페이지의 너비를 가져옵니다. pageWidth = getResources().getDisplayMetrics().widthPixels; final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (e1.getX() - e2.getX() > 50) { // 왼쪽 스와이프 currentPage++; scrollToPage(currentPage); return true; } else if (e2.getX() - e1.getX() > 50) { // 오른쪽 스와이프 currentPage--; scrollToPage(currentPage); return true; } return false; } }); horizontalScrollView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } }); } private void scrollToPage(int page) { if (page < 0) { currentPage = 0; } else if (page >= container.getChildCount()) { currentPage = container.getChildCount() - 1; } horizontalScrollView.smoothScrollTo(currentPage * pageWidth, 0); } } ``` 3. 추가 고려사항 - 위 코드에서는 현재 페이지가 유효한지 확인하며, 스크롤을 진행합니다. - 페이지 수가 화면 너비에 맞는지 확인하고, 필요에 따라 페이지 수를 조정해야 합니다. - 스와이프 감지 기준을 조정하여 사용자의 경험을 개선할 수 있습니다. 이제 `HorizontalScrollView`에 페이징 기능이 추가되었습니다! 위 방법을 통해 간단한 페이징 기능을 구현할 수 있습니다. 그러나 복잡한 동작이나 애니메이션이 필요한 경우 `ViewPager`나 `ViewPager2`의 사용을 고려하는 것이 좋습니다.
이용안내
커뮤니티 이용안내
×
- 게시한 게시글로 발생하는 문제는 게시자에게 책임이 있습니다.
- 게시글이 타인/타업체의 저작권을 침해할 경우 모든 책임은 게시자에게 있습니다. 게시자가 모든 손해를 부담해야 합니다.
- 상식닷컴 운영자는 게시자와 상의하지 않고 게시글을 수정 또는 삭제할 수 있습니다.
- 상식닷컴 운영자는 깨끗한 커뮤니티 공간을 만드는 것이 1순위입니다.
수정하기
취소하기