개발/안드로이드

안드로이드에서 스크롤 뷰 안에 리스트 뷰 사용하기

맨날치킨 2023. 3. 5. 09:06
반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Android list view inside a scroll view

안드로이드에서 스크롤 뷰 안에 리스트 뷰를 사용하는 것

 문제 내용 

I have an android layout which has a scrollView with a number of elements with in it. At the bottom of the scrollView I have a listView which is then populated by an adapter.

저는 scrollView 안에 여러 요소가 있는 Android 레이아웃이 있습니다. scrollView 맨 아래에는 adapter를 통해 채워진 listView가 있습니다.

 

The problem that I am experiencing, is that android is excluding the listView from the scrollView as the scrollView already has a scroll-able function. I want the listView to be as long as the content is and for the master scroll view to be scroll-able.

제가 겪고 있는 문제는, 안드로이드가 이미 스크롤 기능을 가진 스크롤뷰에서 리스트뷰를 제외하고 있다는 것입니다. 나는 리스트뷰가 컨텐츠만큼 길고, 마스터 스크롤 뷰가 스크롤 가능하도록 하려고 합니다.

 

How can I achieve this behavior?

어떻게 이러한 동작을 구현할 수 있을까요?

 

Here is my main layout:

다음은 제 메인 레이아웃입니다:
<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:fillViewport="true"
    android:gravity="top" >

    <LinearLayout
        android:id="@+id/foodItemActvity_linearLayout_fragments"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

</ScrollView>

 

I then programmatically add my components to the linearlayour with the id: foodItemActvity_linearLayout_fragments. Below is one of the views that is loaded into that linearlayout. This is the one giving me trouble with the scrolls.

그런 다음 foodItemActvity\_linearLayout\_fragments라는 ID를 가진 linearlayout에 컴포넌트를 동적으로 추가합니다. 아래는 그 linearlayout에 로드되는 뷰 중 하나입니다. 이것이 스크롤에서 문제가 발생하는 뷰입니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
       android:id="@+id/fragment_dds_review_textView_label"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Reviews:"
       android:textAppearance="?android:attr/textAppearanceMedium" />

   <ListView
       android:id="@+id/fragment_dds_review_listView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
   </ListView>
</LinearLayout>

 

My adapter then fills up this list view.

제 어댑터는 이 리스트 뷰를 채웁니다.

 

Here is an image from the android hierarchy viewer when I click on the master scrollView:

여기는 마스터 스크롤뷰를 클릭했을 때 나오는 안드로이드 계층 구조 뷰의 이미지입니다.

 

Android List View inside a scroll view

As you can see, it is excluding the reviews listView.

보시다시피, 리뷰 listView가 제외되고 있습니다.

 

I should be able to scroll the page down and see 8 reviews, but instead it only shows me those 3, and I can scroll on the tiny part where the reviews are. I want a global page scroll

전체적인 스크롤이 가능하도록 구현하고 싶은데, 현재는 리뷰 부분만 스크롤이 가능하며 3개의 리뷰만 보이고 있습니다. 전체적으로 스크롤이 가능하도록 구현하고 싶습니다.

 

 

 

 높은 점수를 받은 Solution 

For any Child view to scroll inside a ScrollView. Anything like ListView, RecyclerView, etc. You just have to replace ScrollView with androidx.core.widget.NestedScrollView in your current xml and then magic happens.

ScrollView 안에서 ListView, RecyclerView 등의 하위 뷰를 스크롤하려면 현재 xml에서 ScrollView를 androidx.core.widget.NestedScrollView로 대체하면 됩니다. 그러면 마법이 일어납니다.

 

Below is a sample xml code :

아래는 샘플 XML 코드입니다.
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp"
        android:paddingBottom="20dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Recycler View inside a Scroll View"
            android:textColor="@color/black"
            android:textSize="@dimen/_20sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Below is a Recycler View as an example."
            android:textSize="16sp" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@id/et_damaged_qty" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="This textview automatically goes below the Recycler View."
            android:textSize="16sp" />
    </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>

 

Now you can get rid of all the ugly hacks you did to get around with nested scrolling.

이제 중첩 스크롤링 문제를 해결하기 위해 수행한 모든 지저분한 코드를 제거할 수 있습니다.

 

 

 

 가장 최근 달린 Solution 

Best solution is add this android:nestedScrollingEnabled="true" attribute in child scrolling for example i have inserted this attribute in my ListView that is child of ScrollView. i hope this mathod works for you :-

가장 좋은 해결책은 자식 스크롤뷰에 android:nestedScrollingEnabled="true" 속성을 추가하는 것입니다. 예를 들어 내 ListView가 ScrollView의 자식이므로 이 속성을 삽입했습니다. 이 방법이 도움이 되기를 바랍니다.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="TextView"/>
        <ListView
            android:nestedScrollingEnabled="true" //add this only
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="300dp"/>
    </LinearLayout>
</ScrollView>

 

 

출처 : https://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view

반응형