[英]Why can't I scroll to the end of my ListView?
我無法將列表視圖滾動到最后。 它似乎被困在最后一個元素(海王星)的中途。
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);
結果:
在activity_main.xml嘗試將android:layout_height =“0dp”更改為android:layout_height =“wrap_content”,它仍將更新為以編程方式設置的300的高度,但它不會被定義為0dp,這應該有助於解決問題。
將你的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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.