【问题标题】:CursorAdapter row/list item heightCursorAdapter 行/列表项高度
【发布时间】: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


    【解决方案1】:

    项目的高度应为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>
    

    【讨论】:

    • 不,这与设置为行高的值无关。我的问题是视图回收。我有一个视图的布局,正在用于另一个视图。问题在于,在一个中,我显示了一个 viewpager,而在另一个中,我将其设置为 Visibility.GONE。当它回收布局时,它不会重置高度。
    【解决方案2】:

    我发现了我的问题。 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-01
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多