【发布时间】:2014-09-24 20:12:56
【问题描述】:
我目前已经找到了一种在我的列表视图/光标适配器中使用 5 种不同行布局的方法。 但是我遇到了我需要能够更改行高的问题。
我在列表视图中单击关闭并滑动关闭功能。它们通过用光标包装器替换当前光标来工作,从而隐藏一行。
通过这样做,列表视图从已删除视图中获取高度并将其应用于列表视图中的下一行。这样一来,行的高度就会错误。
我认为我的问题是,如何强制 bindView/newView 方法为行提供适当的高度。
我的代码:
新视图:
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v;
switch (cursor.getInt(Card.CARD_TYPE_COLUMN_INDEX)) {
case CardViewModel.DepartmentInformation: {
v = LayoutInflater.from(context).inflate(R.layout.department_information_card, parent, false);
DepartmentInformationViewHolder holder = new DepartmentInformationViewHolder();
holder.mHeader = (CardHeader) v.findViewById(R.id.department_title);
holder.mHeading = (RelativeLayout) v.findViewById(R.id.department_heading_with_remove_option);
holder.mRemoveButton = (CardRemoveButton) v.findViewById(R.id.department_heading_remove_button);
holder.mRemovableHeader = (CardHeader) v.findViewById(R.id.department_heading_title);
holder.mDescription = (CardText) v.findViewById(R.id.department_description);
holder.mGallery = (CardGallery) v.findViewById(R.id.department_gallery);
holder.mButton = (CardButton) v.findViewById(R.id.department_button);
v.setTag(holder);
break;
}
绑定视图
@Override
public void bindView(View view, Context context, Cursor cursor) {
String content = cursor.getString(Card.CARD_CONTENT_COLUMN_INDEX);
int itemType = getItemViewType(cursor.getPosition());
switch (itemType) {
case CardViewModel.DepartmentInformation: {
DepartmentInformationViewModel viewModel = mGson.fromJson(content, DepartmentInformationViewModel.class);
DepartmentInformationViewHolder holder = (DepartmentInformationViewHolder) view.getTag();
/**
* Do the binding of all the components.
*/
if (cursor.getInt(Card.CARD_REMOVABLE_COLUMN_INDEX) == 1) {
holder.mHeader.setVisibility(View.VISIBLE);
bindCardTitle(context, holder.mHeader, viewModel.getTitle());
holder.mHeading.setVisibility(View.GONE);
} else {
holder.mHeading.setVisibility(View.VISIBLE);
bindCardTitle(context, holder.mRemovableHeader, viewModel.getTitle());
bindRemoveCardButton(context, holder.mRemoveButton, cursor, cursor.getPosition(), cursor.getString(0));
holder.mHeader.setVisibility(View.GONE);
}
bindCardText(context, holder.mDescription, viewModel.getDescription());
bindCardGallery(context, holder.mGallery, viewModel.getGallery());
bindCardButton(context, holder.mButton, viewModel.getButton());
break;
}
@Override
public int getItemViewType(int position) {
Cursor cursor = getCursor();
cursor.moveToPosition(position);
switch (cursor.getInt(Card.CARD_TYPE_COLUMN_INDEX)) {
case CardViewModel.DepartmentInformation: {
return CardViewModel.DepartmentInformation;
}
case CardViewModel.EstablishmentInformation: {
return CardViewModel.EstablishmentInformation;
}
case CardViewModel.EstablishmentOutdoorNavigation: {
return CardViewModel.EstablishmentOutdoorNavigation;
}
case CardViewModel.SuggestionInDoorNavigation: {
return CardViewModel.SuggestionInDoorNavigation;
}
default: {
return 4;
}
}
}
@Override
public int getViewTypeCount() {
return 5;
}
你们知道该怎么做吗?
【问题讨论】:
标签: android android-cursoradapter