【问题标题】:Can't scroll down to the last item in the ListView无法向下滚动到 ListView 中的最后一项
【发布时间】:2018-04-09 13:38:45
【问题描述】:

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

问题:

这是我的问题的可视化演示

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

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

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

这是我在自定义类中实现的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>

【问题讨论】:

  • 提供listview_custom_adapter_row_layout.xml的内容会很有帮助
  • @szamani20 添加了 ..
  • 我认为您对listView xml 有疑问,您能提供吗?主布局
  • @Ibrahim 完成....

标签: android listview scroll


【解决方案1】:

listView25dp 边距,将 15dp 设置为 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>

【讨论】:

  • marginBottom 添加到listview 可以解决问题,但要添加marginBottom,我必须设置constraintBottom_toBottomOf= parentlistview。如果我这样做,pageHeadinglistview 会相互重叠。我怎样才能避免这种情况?
  • 您可以将底部padding 添加到父布局中,我认为这是您情况的最佳解决方案,顺便说一句,使用recycleview 更好。
  • @Yousaf 您也可以在列表视图中使用app:layout_constraintBottom_toTopOf="parent"
  • 我已经使用嵌套布局修复了它...无论如何感谢您的回答,它肯定解决了主要问题。
  • @Yousaf with constraintLayout 你不需要特别用这个简单的布局,检查编辑。
【解决方案2】:

设置 layout_height match_constraint

【讨论】:

    【解决方案3】:

    您只需将layout_constrainedHeight设置为true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      相关资源
      最近更新 更多