列表视图不滚动直到最后一个项目

时间:2017-07-20 21:55:58

标签: java android

你好,我有滚动问题,直到最后一项滚动停在中间,这是我的布局代码。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fragment.Equivalent">
<ListView
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:focusable="false"/>

<TextView
    android:id="@+id/txt404"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Message that will be shown if the listview is empty"
    android:textSize="21dp"
    android:textStyle="bold"
    android:visibility="invisible" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

尝试使用滚动视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="fragment.Equivalent">
    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:scrollbarStyle="insideOverlay"
            style="@android:style/Widget.Holo.ScrollView">
     <TableLayout
        android:id="@+id/table_layout_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </TableLayout>
    </ScrollView>
</RelativeLayout>

在java文件中,执行以下操作:

TableLayout ll = (TableLayout) findViewById(R.id.table_layout_id);
TableRow row = new TableRow(this);
    
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    
row.setLayoutParams(lp);

    
    butt = new Button(this);
    //You can add button too, FYI
butt.setText("TEST");
    
    
    
TextView tv = (TextView) findViewById(R.id.txt404);

tv.setText("Text view");
    
tv2 = new TextView(this);
    //your job: add list view here
tv2.setText("Text View 2");
    
    
    
row.addView(butt);

    row.addView(tv1);
    
row.addView(tv2);
    
    
ll.addView(row, i); // where i is the row number, increment on addition

答案 1 :(得分:0)

解决方案是将布局更改为约束布局并更改列表视图的大小

<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="fragment">

<ListView
    android:id="@+id/listView"
    android:layout_width="399dp"
    android:layout_height="485dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="0dp"
    android:focusable="false"
    android:visibility="visible"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintTop_creator="1"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintVertical_bias="0.0" />

<TextView
    android:id="@+id/txt404"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="21dp"
    android:textStyle="bold"
    android:visibility="invisible"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintVertical_bias="0.391" />

   </android.support.constraint.ConstraintLayout>