无法向下滚动到ListView中的最后一项

时间:2017-10-27 21:29:42

标签: android listview scroll

我有ListView使用名为ListViewCustomAdapter的自定义类扩展BaseAdapter类。在ListView的每一行中,我显示学生信息,如姓名,身份证等......

问题:

以下是我的问题的视觉展示

当我添加三名学生时,他们完全适合屏幕

enter image description here

现在当我添加第4名学生并尝试向下滚动以查看第4名学生时,屏幕仅向下滚动到第4名学生信息的一半

enter image description here

知道是什么导致了这个问题,我该如何解决这个问题?

这是我的自定义类

中实现的getView方法
@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if(convertView == null){
            LayoutInflater inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.listview_custom_adapter_row_layout, null);

            holder = new ViewHolder();
            holder.img = (ImageView)convertView.findViewById(R.id.profileImage);
            holder.name = (TextView)convertView.findViewById(R.id.nameLabel2);
            holder.age = (TextView)convertView.findViewById(R.id.ageLabel2);
            holder.ID = (TextView)convertView.findViewById(R.id.IDLabel2);
            holder.degree = (TextView)convertView.findViewById(R.id.degreeLabel2);
            holder.email = (TextView)convertView.findViewById(R.id.emailLabel2);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder)convertView.getTag();
        }

        Student stu = studentList.get(position);
        holder.img.setImageResource(stu.getProfilePicID());
        holder.name.setText(stu.getStudentName());
        holder.age.setText(stu.getStudentAge());
        holder.ID.setText(stu.getStudentID());
        holder.degree.setText(stu.getStudentDegree());
        holder.email.setText(stu.getStudentEmail());

        return convertView;
} 

listview_custom_adapter_row_layout.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">

    <ImageView
        android:id="@+id/profileImage"
        android:layout_width="130dp"
        android:layout_height="155dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_margin="5dp"
        android:src="@drawable/student_profile_pic"/>

    <TextView
        android:id="@+id/nameLabel"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textColor="@android:color/background_dark"
        android:text="Name: "
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="10dp"
        app:layout_constraintBaseline_toBaselineOf="@+id/nameLabel2"
        />

    <TextView
        android:id="@+id/ageLabel"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:text="Age: "
        android:textColor="@android:color/background_dark"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/nameLabel"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBaseline_toBaselineOf="@+id/ageLabel2"
        />

    <TextView
        android:id="@+id/IDLabel"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:text="ID: "
        android:textColor="@android:color/background_dark"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ageLabel"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBaseline_toBaselineOf="@+id/IDLabel2"
        />

    <TextView
        android:id="@+id/degreeLabel"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:text="Degree: "
        android:textColor="@android:color/background_dark"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/IDLabel"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBaseline_toBaselineOf="@+id/degreeLabel2"
        />

    <TextView
        android:id="@+id/emailLabel"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:text="Email: "
        android:textColor="@android:color/background_dark"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/degreeLabel"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBaseline_toBaselineOf="@+id/emailLabel2"
        />

    <TextView
        android:id="@+id/nameLabel2"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@android:color/background_dark"
        app:layout_constraintLeft_toRightOf="@+id/nameLabel"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="10dp"
        />

    <TextView
        android:id="@+id/ageLabel2"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@android:color/background_dark"
        app:layout_constraintLeft_toRightOf="@+id/nameLabel"
        app:layout_constraintTop_toBottomOf="@+id/nameLabel2"
        android:layout_marginTop="6dp"
        />

    <TextView
        android:id="@+id/IDLabel2"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@android:color/background_dark"
        app:layout_constraintLeft_toRightOf="@+id/nameLabel"
        app:layout_constraintTop_toBottomOf="@+id/ageLabel2"
        android:layout_marginTop="6dp"
        />

    <TextView
        android:id="@+id/degreeLabel2"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@android:color/background_dark"
        app:layout_constraintLeft_toRightOf="@+id/nameLabel"
        app:layout_constraintTop_toBottomOf="@+id/IDLabel2"
        android:layout_marginTop="6dp"
        />

    <TextView
        android:id="@+id/emailLabel2"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@android:color/background_dark"
        app:layout_constraintLeft_toRightOf="@+id/nameLabel"
        app:layout_constraintTop_toBottomOf="@+id/degreeLabel2"
        android:layout_marginTop="6dp"/>

    <!--just to add margin at the bottom of each row-->
    <TextView
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/emailLabel"
        android:layout_marginTop="3dp"/>

</android.support.constraint.ConstraintLayout>

activity_custom_adapter_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="com.example.yousaf.listview_customadapter.CustomAdapterMain">

    <TextView
        android:id="@+id/pageHeading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Student Information"
        android:textSize="30sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="15dp"
        android:textStyle="bold"/>

    <ListView
        android:id="@+id/studentInfoList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_constraintTop_toBottomOf="@id/pageHeading"
        android:layout_marginTop="10dp"
        android:dividerHeight="7dp">
    </ListView>

</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:1)

25dp的{​​{1}}页边距为listView15dp textView android:layout_marginTop="15dp"10dp listView

listView放在textView app:layout_constraintTop_toBottomOf="@id/pageHeading"

的底部

添加15dp + 10dp

有许多技巧可以避免这种情况:

  • 将保证金底部添加到listView 25dp

  • 删除不重要的边距

  • 将底部填充添加到主布局

你可以自己处理。

您的布局应该是:

   <?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"
    android:paddingBottom="25dp"
    >

    <TextView
        android:id="@+id/pageHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Student Information"
        android:textSize="30sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:paddingTop="10dp"
        android:gravity="center_horizontal"
        android:textStyle="bold"/>

    <ListView
        android:id="@+id/studentInfoList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_constraintTop_toBottomOf="@id/pageHeading"
        android:paddingTop="10dp"
        android:dividerHeight="7dp">
    </ListView>

</android.support.constraint.ConstraintLayout>

答案 1 :(得分:0)

设置layout_height match_constraint

答案 2 :(得分:0)

您只需要将layout_constrainedHeight设置为 true