安卓 2.3.3
我有一个列表视图,我正在其中填充一个自定义视图。
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>
列表视图 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>
当我运行应用程序时,customview 会填充我需要的所有值,但每个 customview 都占用整个屏幕(列表视图可用的空间)。当我向下滚动时,我可以看到其他行。
代码 - 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;
}
截图 - 如果它可以帮助