简体   繁体   中英

Why can't I scroll to the end of my ListView?

I can't scroll my list view to the end. It seems to be stuck halfway at the last element(Neptune).

ListView is stuck at the last element as shown in the picture.

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <include
        layout="@layout/app_bar"
        android:id="@+id/app_bar"
        app:layout_constraintBottom_toTopOf="@+id/listView"
        />


    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/app_bar"
        />



</android.support.constraint.ConstraintLayout>

MainActivity.java

ListView listView = findViewById(R.id.listView);

String[] planetsArray = getResources().getStringArray(R.array.planet_list);

ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, planetsArray){

    @Override
    public View getView(int position, View convertView, ViewGroup parent){

        View view = super.getView(position,convertView,parent);

        ViewGroup.LayoutParams layoutparams = view.getLayoutParams();

        //Define your height here.
        layoutparams.height = 300;

        view.setLayoutParams(layoutparams);

        return view;
    }
};

listView.setAdapter(arrayadapter);

Result:

在此输入图像描述

在activity_main.xml尝试将android:layout_height =“0dp”更改为android:layout_height =“wrap_content”,它仍将更新为以编程方式设置的300的高度,但它不会被定义为0dp,这应该有助于解决问题。

change your android:layout_height="0dp" to android:layout_height="wrap_content" or set layout_weight="1"

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <include
        layout="@layout/app_bar"
        android:id="@+id/app_bar"
        app:layout_constraintBottom_toTopOf="@+id/listView"
        />


    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/app_bar"
        />



</android.support.constraint.ConstraintLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM