【发布时间】: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 添加了 ..
-
我认为您对
listViewxml 有疑问,您能提供吗?主布局 -
@Ibrahim 完成....