CursorAdapter行/列表项高度

时间:2014-09-23 08:30:51

标签: android android-cursoradapter

我目前已经找到了在listview / cursoradapter中使用5种不同行布局的方法。 但是我遇到了我需要能够改变行高的问题。

我点击解除并轻扫以解除我的listview中的功能。他们通过用cursorwrapper替换当前光标从而隐藏一行来工作。

通过执行此操作,listview将从已删除的视图中获取高度,并将其应用于listview中的下一行。通过这样做,行占用了错误的高度。

我认为我的问题是,如何强制bindView / newView方法为行提供适当的高度。

我的代码:

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;
    }

bindView

@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;
}

你们知道该怎么办吗?

2 个答案:

答案 0 :(得分:0)

项目的高度应为android:layout_height="fill_parent" 每个项目将占用所需的空间,并且列表视图中的每个项目都有不同的高度。

以下示例根据提供的文本数量提供不同的高度。

<?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="fill_parent" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:contentDescription="@string/image"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/icon"
    android:paddingBottom="10dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#CC8439"
    android:textIsSelectable="false" />

<TextView
    android:id="@+id/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/title"
    android:layout_toRightOf="@+id/icon"
    android:paddingLeft="10dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#3399FF"
    android:textIsSelectable="false" />

<TextView
    android:id="@+id/resrawid"
    android:layout_width="0dip"
    android:layout_height="0dip"
    android:visibility="gone" />


</RelativeLayout>

答案 1 :(得分:0)

我发现了我的问题。这不是cursoradapter中的问题,也不是ListView的逻辑。 问题出现在我用于滑动动画的图书库中。

我找到了以下网页:https://github.com/romannurik/Android-SwipeToDismiss/issues/7

它描述了如何重置视图的高度。替换以下内容对我有用:

我的SwipeDismissListViewTouchListener.java版本中的@line 379

// TODO: If problems are found with the height of cards. This might be the problem.
// ORIGINAL: lp.height = originalHeight;
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;

我的SwipeDismissTouchListener.java版本中的@line 302

// TODO: If problems are found with the height of cards. This might be the problem.
// ORIGINAL: lp.height = originalHeight;
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;