Android Can';t向下滚动到ListView中的最后一项
我有一个Android Can';t向下滚动到ListView中的最后一项,android,listview,scroll,Android,Listview,Scroll,我有一个ListView使用名为ListViewCustomAdapter的自定义类,它扩展了BaseAdapter类。在列表视图的每一行中,我显示学生信息,如姓名、id等 问题: 这是我的问题的一个直观演示 当我添加三个学生时,他们完全适合屏幕 现在当我添加第四名学生并尝试向下滚动查看第四名学生时,屏幕仅向下滚动至第四名学生信息的一半 知道是什么原因导致此问题,以及如何解决此问题吗? 下面是我在自定义类中实现的getView方法 @Override public View getV
ListView
使用名为ListViewCustomAdapter
的自定义类,它扩展了BaseAdapter
类。在列表视图的每一行中,我显示学生信息,如姓名、id等
问题:
这是我的问题的一个直观演示
当我添加三个学生时,他们完全适合屏幕
现在当我添加第四名学生并尝试向下滚动查看第四名学生时,屏幕仅向下滚动至第四名学生信息的一半
知道是什么原因导致此问题,以及如何解决此问题吗?
下面是我在自定义类中实现的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\u custom\u adapter\u row\u 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>
<?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>
活动\u自定义\u适配器\u 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">
<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>
<?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>
您的列表视图有25dp
边距,将15dp
设置为textView
android:layout_marginTop=“15dp”
和列表视图的10dp
通过将您的列表视图
置于文本视图的底部
应用程序:layout\u constraintTop\u 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>
设置布局高度匹配约束您只需将布局高度约束
设置为true如果您提供列表视图\u自定义适配器\u行\u布局的内容会很有帮助。添加了xml
@szamani20..我认为您对列表视图
xml有问题。您能提供它吗?主要layout@Ibrahim完成…将marginBottom
添加到listview
解决了问题,但要添加marginBottom
,我必须设置listview
的constraintBottom\u toBottomOf=parent
。如果我这样做,pageHeading
和listview
会相互重叠。如何避免这种情况?您可以在父布局中添加底部填充,我认为这是最适合您的解决方案,顺便说一句,使用recycleview更好。@如果您还可以在列表视图中使用app:layout\u constraint bottom\u tototopof=“parent”
。我已经使用嵌套布局修复了它。。。无论如何,谢谢你的回答,它肯定解决了主要问题。@如果使用constraintLayout,你不需要特别使用这个简单的布局,请选中“编辑”。