为什么不能滚动到ListView的末尾?

时间:2019-04-03 17:27:01

标签: java android

我无法将列表视图滚动到末尾。它似乎停留在最后一个元素(海王星)的一半。

ListView卡在图片的最后一个元素上。

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);

结果:

enter image description here

2 个答案:

答案 0 :(得分:0)

在activity_main.xml中,尝试将android:layout_height =“ 0dp”更改为android:layout_height =“ wrap_content”,该值仍将更新为以编程方式设置的hight 300,但不会定义为0dp,这应该有所帮助问题。

答案 1 :(得分:0)

将您的 android:layout_height =“ 0dp” 更改为 android:layout_height =“ wrap_content” 或设置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>