ScrollView не прокручивается полностью в XML в Android - PullRequest
0 голосов
/ 02 мая 2019

У меня есть XML-код, который отображает оценочную карту крикета команды.Это мой код Scorecard.xml, где я использую NonScrollableList под Linear Layout в Scrollview:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:fillViewport="true"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/windowBackground"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="50.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@drawable/scorecard_uperlayout">

            <TextView
                android:id="@+id/Batteam_description"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="10.0dip"
                android:layout_marginLeft="10.0dip"
                android:text="Aus"
                android:textColor="@color/textColorPrimary"
                android:textSize="18.0sp" />
        </RelativeLayout>

        <LinearLayout
            android:id="@id/tabllayout"
            android:layout_width="fill_parent"
            android:layout_height="40.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/graycolor2"
            android:visibility="visible"
            android:weightSum="11.0">

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="5.5">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10.0dip"
                    android:layout_marginLeft="10.0dip"
                    android:text="Batsman"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="R"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="B"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4s"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="6s"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="2.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="SR"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip">

            <com.example.live_score.activity.NonScrollableList
                android:id="@+id/list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/layoutBackground"
            android:visibility="visible">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10.0dip"
                android:layout_marginLeft="10.0dip"
                android:text=" Extras"
                android:textColor="@color/textcolor"
                android:textSize="16.0sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/layoutBackground"
            android:visibility="visible">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10.0dip"
                android:layout_marginLeft="10.0dip"
                android:text="Yet to Bat"
                android:textColor="@color/textcolor"
                android:textSize="16.0sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/tabllayout_bowl"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/graycolor2"
            android:visibility="visible"
            android:weightSum="11.0">

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="5.3">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginStart="10.0dip"
                    android:layout_marginLeft="10.0dip"
                    android:text="Bowler"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="O"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="M"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="R"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="W"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Wd"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Nb"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:visibility="visible">

            <com.example.live_score.activity.NonScrollableList
                android:id="@+id/list_bowlings"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="40.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/graycolor2"
            android:visibility="visible"
            android:weightSum="9.0">

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="6.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:layout_marginStart="10.0dip"
                    android:layout_marginLeft="10.0dip"
                    android:text="Fall of Wickets"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="W"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="R"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="O"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

        </LinearLayout>

    </LinearLayout>
</ScrollView>

Ниже приведен код действия NonScrollableList:

public class NonScrollableList extends ListView {

    public NonScrollableList(Context context) {
        super(context);
    }
    public NonScrollableList(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public NonScrollableList(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

Ниже приведено onCreateView для вышеупомянутогокод:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.scorecard, container, false);
        listView = (NonScrollableList) rootView.findViewById(R.id.list);
        list_bowlings = (NonScrollableList) rootView.findViewById(R.id.list_bowlings);
}

Но проблема, которую я не могу прокрутить вниз полностью.Где мне нужно добавить больше высоты, чтобы исправить прокрутку?Кто-нибудь может помочь?Заранее спасибо

Ответы [ 3 ]

0 голосов
/ 02 мая 2019

просто используйте android: nestedScrollingEnabled = "true" в ListView

0 голосов
/ 02 мая 2019

В коде xml убедитесь, что внутри ScrollView есть только один дочерний элемент, также измените высоту LinearLayout внутри ScrollView с "wrap_content" на "match_parent".

0 голосов
/ 02 мая 2019

Попробуйте использовать этот пользовательский ListView -

Пользовательский ListView:

public class MyCustomListView extends ListView {

    public MyCustomListView(Context context) {
        super(context);
    }

    public MyCustomListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyCustomListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int heightSpec;

        if (getLayoutParams().height == LayoutParams.FILL_PARENT) {
            heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        } else {
            heightSpec = heightMeasureSpec;
        }

        super.onMeasure(widthMeasureSpec, heightSpec);
    }
}

В XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout

        android:id="@+id/slistLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
                android:id="@+id/Batteam_description"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="10.0dip"
                android:layout_marginLeft="10.0dip"
                android:text="Aus"
                android:textColor="@color/textColorPrimary"
                android:textSize="18.0sp" />


        <your_packagename.MyCustomListView
            android:id="@+id/pListView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:clickable="true"
            android:descendantFocusability="blocksDescendants"
            android:fadeScrollbars="false"
            android:focusable="true"
            android:scrollbarAlwaysDrawVerticalTrack="true"
            android:scrollbarStyle="insideInset" />


        <your_packagename.MyCustomListView
            android:id="@+id/pListView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:clickable="true"
            android:descendantFocusability="blocksDescendants"
            android:fadeScrollbars="false"
            android:focusable="true"
            android:scrollbarAlwaysDrawVerticalTrack="true"
            android:scrollbarStyle="insideInset" />


    </LinearLayout>


</ScrollView>
...