Не прокручиваемый ListView внутри прокручиваемого родительского макета - PullRequest
0 голосов
/ 03 ноября 2019

Используя Android Studio, у меня есть следующий макет:

enter image description here

Три элемента списка внизу - это ListView объекты, и все онивнутри объекта RelativeLayout, который находится внутри объекта ScrollView. Мне бы хотелось, чтобы три объекта списка имели высоту макета, которая обертывала содержимое, и не была прокручиваемой. Если высота трех объектов списка выходит за нижнюю часть экрана, я хочу, чтобы RelativeLayout становился прокручиваемым, но не ListView объектов.

enter image description here

И если вы хотите, вот код для этого действия:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:padding="@dimen/root_padding"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CourseViewActivity">

        <TextView
            android:id="@+id/title_display"
            android:text="@string/course_view"
            android:textSize="@dimen/header_text_size"
            android:layout_toLeftOf="@id/edit_course_button"
            android:layout_toStartOf="@id/edit_course_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/date_display"
            android:text="@string/start_and_end_dates"
            android:layout_toLeftOf="@id/edit_course_button"
            android:layout_toStartOf="@id/edit_course_button"
            android:layout_below="@id/title_display"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/mentor_display"
            android:text="@string/course_mentor_display_default"
            android:layout_toLeftOf="@id/edit_course_button"
            android:layout_toStartOf="@id/edit_course_button"
            android:layout_below="@id/date_display"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/credits_display"
            android:text="@string/course_credits_display_default"
            android:layout_toLeftOf="@id/edit_course_button"
            android:layout_toStartOf="@id/edit_course_button"
            android:layout_below="@id/mentor_display"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/passed_display"
            android:text="@string/passed"
            android:textColor="#128039"
            android:textStyle="bold"
            android:textSize="@dimen/sub_header_text_size"
            android:visibility="gone"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_above="@id/separator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/edit_course_button"
            android:text="@string/edit_course"
            android:onClick="editCourse"
            android:background="@color/blue_button_bg"
            android:textColor="@color/blue_button_fg"
            android:padding="@dimen/button_padding"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/separator"
            android:layout_below="@id/credits_display"
            android:background="#333"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_width="match_parent"
            android:layout_height="2dp" />

    <ScrollView
        android:layout_below="@id/separator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/objective_header"
            android:text="@string/objectives"
            android:textSize="@dimen/sub_header_text_size"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/create_objective_button"
            android:onClick="createNewObjective"
            android:src="@drawable/ic_add"
            android:layout_alignTop="@+id/objective_header"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ListView
            android:id="@+id/objective_list"
            android:layout_below="@id/objective_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/assessment_header"
            android:text="@string/assessments"
            android:textSize="@dimen/sub_header_text_size"
            android:layout_below="@id/objective_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/create_assessment_button"
            android:onClick="createNewAssessments"
            android:src="@drawable/ic_add"
            android:layout_alignTop="@+id/assessment_header"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ListView
            android:id="@+id/assessment_list"
            android:layout_below="@id/assessment_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/note_header"
            android:text="@string/notes"
            android:textSize="@dimen/sub_header_text_size"
            android:layout_below="@id/assessment_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/create_note_button"
            android:onClick="createNewNote"
            android:src="@drawable/ic_add"
            android:layout_alignTop="@+id/note_header"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ListView
            android:id="@+id/note_list"
            android:layout_below="@id/note_header"
            android:layout_marginBottom="10dp"
            android:scrollbars="none"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </RelativeLayout>

    </ScrollView>

</RelativeLayout>

РЕДАКТИРОВАТЬ: проект должен быть API 15, что означает, что я не могу использовать такие вещи, как RecyclerView.

Ответы [ 2 ]

0 голосов
/ 06 ноября 2019

Ваш xml-код верен, но нуждается в некоторых изменениях, проверьте код ниже:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SwipeRefreshLayoutActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/title_display"
                android:text="@string/course_view"
                android:layout_toLeftOf="@id/edit_course_button"
                android:layout_toStartOf="@id/edit_course_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/date_display"
                android:text="@string/start_and_end_dates"
                android:layout_toLeftOf="@id/edit_course_button"
                android:layout_toStartOf="@id/edit_course_button"
                android:layout_below="@id/title_display"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/mentor_display"
                android:text="@string/course_mentor_display_default"
                android:layout_toLeftOf="@id/edit_course_button"
                android:layout_toStartOf="@id/edit_course_button"
                android:layout_below="@id/date_display"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/credits_display"
                android:text="@string/course_credits_display_default"
                android:layout_toLeftOf="@id/edit_course_button"
                android:layout_toStartOf="@id/edit_course_button"
                android:layout_below="@id/mentor_display"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/passed_display"
                android:text="@string/passed"
                android:textColor="#128039"
                android:textStyle="bold"
                android:visibility="gone"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_above="@id/separator"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <Button
                android:id="@+id/edit_course_button"
                android:text="@string/edit_course"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ImageView
                android:id="@+id/separator"
                android:layout_below="@id/credits_display"
                android:background="#333"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:layout_width="match_parent"
                android:layout_height="2dp" />
        </RelativeLayout>
    </LinearLayout>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:text="Objectives"
                    android:layout_centerVertical="true"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/icon_arrow_left"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="true"/>
            </RelativeLayout>
            <ListView
                android:id="@+id/list_objectives"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"/>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:text="Assessments"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/icon_arrow_left"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="true"/>
            </RelativeLayout>
            <ListView
                android:id="@+id/list_assessments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"/>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:text="Notes"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/icon_arrow_left"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="true"/>
            </RelativeLayout>
            <ListView
                android:id="@+id/list_notes"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

К Scroll несколько списков просмотра в представлении прокрутки добавить код ниже в файле Java:

 public void setListViewHeight(ListView listView) {
        ArrayAdapter listAdapter = (ArrayAdapter) listView.getAdapter();
        if (listAdapter != null) {
            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }

            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
            listView.requestLayout();
        }
    }

Как вызвать вышеуказанный метод в Java-файле:

После установки adapter на listview метод вызова, как показано ниже:

 final ListArrayAdapter adapter3 = new ListArrayAdapter(this,
            android.R.layout.simple_list_item_1, arrayList3);
    listAssessments.setAdapter(adapter3);
    setListViewHeight(listAssessments);

Я надеюсь, что он работает длявы.

0 голосов
/ 04 ноября 2019

Решение:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:padding="@dimen/_5sdp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/relativeLayout">

    <TextView
        android:id="@+id/title_display"
        android:text="@string/course_view"
        android:textSize="@dimen/header_text_size"
        android:layout_toLeftOf="@id/edit_course_button"
        android:layout_toStartOf="@id/edit_course_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/date_display"
        android:text="@string/start_and_end_dates"
        android:layout_toLeftOf="@id/edit_course_button"
        android:layout_toStartOf="@id/edit_course_button"
        android:layout_below="@id/title_display"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/mentor_display"
        android:text="@string/course_mentor_display_default"
        android:layout_toLeftOf="@id/edit_course_button"
        android:layout_toStartOf="@id/edit_course_button"
        android:layout_below="@id/date_display"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/credits_display"
        android:text="@string/course_credits_display_default"
        android:layout_toLeftOf="@id/edit_course_button"
        android:layout_toStartOf="@id/edit_course_button"
        android:layout_below="@id/mentor_display"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/passed_display"
        android:text="@string/passed"
        android:textColor="#128039"
        android:textStyle="bold"
        android:textSize="@dimen/sub_header_text_size"
        android:visibility="gone"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_above="@id/separator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/edit_course_button"
        android:text="@string/edit_course"
        android:onClick="editCourse"
        android:background="@color/blue_button_bg"
        android:textColor="@color/blue_button_fg"
        android:padding="@dimen/button_padding"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/separator"
        android:layout_below="@id/credits_display"
        android:background="#333"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="2dp" />

    </RelativeLayout>

    <ScrollView
        android:layout_below="@id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/objective_header"
                android:text="@string/objectives"
                android:textSize="@dimen/sub_header_text_size"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <ImageView
                android:id="@+id/create_objective_button"
                android:onClick="createNewObjective"
                android:src="@drawable/ic_add"
                android:layout_alignTop="@+id/objective_header"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ListView
                android:id="@+id/objective_list"
                android:layout_below="@id/objective_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/assessment_header"
                android:text="@string/assessments"
                android:textSize="@dimen/sub_header_text_size"
                android:layout_below="@id/objective_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <ImageView
                android:id="@+id/create_assessment_button"
                android:onClick="createNewAssessments"
                android:src="@drawable/ic_add"
                android:layout_alignTop="@+id/assessment_header"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ListView
                android:id="@+id/assessment_list"
                android:layout_below="@id/assessment_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/note_header"
                android:text="@string/notes"
                android:textSize="@dimen/sub_header_text_size"
                android:layout_below="@id/assessment_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <ImageView
                android:id="@+id/create_note_button"
                android:onClick="createNewNote"
                android:src="@drawable/ic_add"
                android:layout_alignTop="@+id/note_header"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ListView
                android:id="@+id/note_list"
                android:layout_below="@id/note_header"
                android:layout_marginBottom="10dp"
                android:scrollbars="none"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

</RelativeLayout>

Я надеюсь, что это работает для вас

...