我在 newView 方法中的自定义 CursorAdapter 中关注这篇博文InfamousViewHolder Pattern我创建了一个自定义 RelativeLayout 类,并在 bindView 中填充数据。
问题在于这些方法从未被调用过。我正确设置了 setListAdapter,因为如果我实现 ViewHolder 模式,列表将正确填充。
执行。这里的代码:
public class SearchUserListViewRow extends RelativeLayout{
public ImageView avatar = null;
public TextView name = null;
public ToggleButton inviteButton = null;
public ToggleButton followButton = null;
public FoundUser user;
public SearchUserListViewRow(Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.search_friend_row, this);
avatar = (ImageView) findViewById(R.id.avatar);
name = (TextView) findViewById(R.id.user_name);
inviteButton = (ToggleButton) findViewById(R.id.invite_button);
followButton = (ToggleButton) findViewById(R.id.follow_button);
}
public void init(Cursor cursor) {
user = UsersProcessor.toFoundUser(cursor);
name.setText(user.alias);
inviteButton.setChecked(user.invited);
inviteButton.setVisibility(Section.TO_INVITE.equals(user.section) ?
View.VISIBLE : View.GONE);
inviteButton.setTag(user);
followButton.setEnabled(!user.invited);
followButton.setChecked(user.isGuru);
followButton.setVisibility(Section.TO_FOLLOW.equals(user.section) ?
View.VISIBLE : View.GONE);
followButton.setTag(user);
}
}
和适配器:
public class UserCursorAdapter extends CursorAdapter {
public UserCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = new SearchUserListViewRow(context);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
SearchUserListViewRow row = (SearchUserListViewRow)v;
//ViewHolder like stuff
row.text = "foo";
}
}
和 serarch_friend_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/txt_box"
android:orientation="horizontal"
android:layout_marginTop="-3px"
android:padding="@dimen/padding_row" >
<ImageView
android:id="@+id/avatar"
android:layout_width="@dimen/image_user_avatar"
android:layout_height="@dimen/image_user_avatar"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<FrameLayout
android:id="@+id/button_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true">
<ToggleButton
android:id="@+id/follow_button"
android:visibility="gone" />
<ToggleButton
android:id="@+id/invite_button"0000000000000000000
android:visibility="gone" />
</FrameLayout>
<TextView
android:id="@+id/user_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/content_padding_normal"
android:layout_toLeftOf="@id/button_container"
android:layout_toRightOf="@id/avatar"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="@dimen/text_size_medium"
android:textStyle="bold" />
</RelativeLayout>
提前致谢。