Android 2.3.3
我有一个listview,我正在填充自定义视图。
CustomView - ListView项目
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:layout_margin="2dp" >
<ImageView
android:id="@+id/imgView_addcontact"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:src="@drawable/addcontactsmall2"
android:layout_weight="0.20"
android:padding="5dp"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.60"
android:orientation="vertical"
android:padding="5dp"
android:layout_gravity="center" >
<TextView
android:id="@+id/txtView_addcontact_contactname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact Name"
android:textSize="20sp" />
<TextView
android:id="@+id/txtview_addcontact_phonenumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number"
android:textSize="16sp" />
</LinearLayout>
<CheckBox
android:id="@+id/chbk_addcontact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:padding="5dp"
android:layout_gravity="center"
/>
</LinearLayout>
ListView XML :::
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtViewContactsHeader"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="Your Contacts (Phone + SIM)"
android:layout_gravity="center"
android:gravity="center" />
<ListView
android:id="@+id/lv_addcontact"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center" >
<Button
android:id="@+id/btn_addcontact_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD CONTACTS" />
<Button
android:id="@+id/btn_addcontact_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
<TextView
android:id="@+id/txtViewNavigation"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Navigation"
android:layout_gravity="center"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
当我运行应用程序时,自定义视图会填充我需要的所有值,但每个自定义视图占用整个屏幕(列表视图可用的空间)。当我向下滚动时,我可以看到其他行。
代码 - getView()
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// View MyView = convertView;
ContactViewHolder contactViewHolder;
if (convertView == null) {
// Inflate the layout
LayoutInflater li = getLayoutInflater();
convertView = li.inflate(
R.layout.activity_listview_addcontact_item, null);
contactViewHolder = new ContactViewHolder();
contactViewHolder.imgContact = (ImageView) convertView
.findViewById(R.id.imgView_addcontact);
contactViewHolder.txtViewContactName = (TextView) convertView
.findViewById(R.id.txtView_addcontact_contactname);
contactViewHolder.txtViewPhoneNumber = (TextView) convertView
.findViewById(R.id.txtview_addcontact_phonenumber);
contactViewHolder.chbkAddContact = (CheckBox) convertView.findViewById(R.id.chbk_addcontact);
convertView.setTag(contactViewHolder);
} else {
contactViewHolder = (ContactViewHolder) convertView.getTag();
}
//while (cur.moveToNext()) {
cur.moveToPosition(position);
/*
// Add The Image!!!
String image = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (image != null) {
contactViewHolder.imgContact.setImageURI(Uri.parse(image));
} else {
contactViewHolder.imgContact
.setImageResource(R.drawable.addcontactsmall2);
}
*/
// Add Contact Name //
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (name != null)
contactViewHolder.txtViewContactName.setText(name);
else
contactViewHolder.txtViewContactName.setText("Unknown");
// Add Phone Number //
String phoneNumber = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (phoneNumber != null)
contactViewHolder.txtViewPhoneNumber.setText(phoneNumber);
else
contactViewHolder.txtViewPhoneNumber.setText("Unknown");
//System.out.println(image + " " + name + " " + phoneNumber);
//}
return convertView;
}
public class ContactViewHolder {
ImageView imgContact;
TextView txtViewContactName;
TextView txtViewPhoneNumber;
CheckBox chbkAddContact;
}
ScreenShot - 如果可以提供帮助
答案 0 :(得分:0)
在你的适配器的getView方法中,当你扩展listview项时,不要像你在这里那样在父视图中传递null:
convertView = li.inflate(R.layout.activity_listview_addcontact_item, null);
相反,将提供的父视图和false传递给备用重载的inflate方法:
convertView = li.inflate(R.layout.activity_listview_addcontact_item, parent, false);
如果您未传递父项,则不会遵守您在xml布局文件中设置的layout_height值。