滚动时Gridview行高度搞乱

时间:2013-06-14 13:46:03

标签: android

我正在使用gridview来呈现由两个文本字段组成的各种文章项目。由于文本数量不同,每个项目的高度不同,行的大小应与行中最大项目的高度相对应。 gridview使用colnum = autofit。

如滚动显示之前和之后的以下屏幕截图所示,行为是预期的,因为我向下再向上滚动:行混乱并削减大项目的内容。 我错过了什么?

Gridview 之前滚动:

screenshot of gridview before scrolling with proper

GridView 后向上滚动:

screenshot of gridview after scrolling with messed up items

进一步的代码:

项目xml代码

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="14dp"
    android:textColor="#040404"
    android:textSize="20sp"
    android:textStyle="bold"
    android:typeface="serif" />

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:textColor="#343434"
    android:textSize="14sp"
    android:typeface="serif" />

</LinearLayout>

我的ListAdapter(如果可能,回收视图):

public class SearchResultListViewAdapter extends ArrayAdapter {

Context                    context;
Newspaper                  newspaper;
List<SearchResultListItem> searchResults;
int                        viewStyle;

public SearchResultListViewAdapter(Context context, int resourceId, List<SearchResultListItem> searchResults, int viewStyle) {
    super(context, resourceId, searchResults);
    this.context = context;
    this.searchResults = searchResults;
    this.viewStyle = viewStyle;
}

/* private view holder class */
private class ViewHolder
{
    TextView txtTitle;
    TextView txtText;
}

public View getView(int position, View convertView, ViewGroup parent)
{
    View gridItem;
    ViewHolder holder = null;

    if (convertView == null)
    {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
        if (this.viewStyle == 0)
        {
            gridItem = mInflater.inflate(R.layout.searchresult_list_item, null);
        } else
        {
            gridItem = mInflater.inflate(R.layout.searchresult_grid_item, null);
        }
        holder = new ViewHolder();
    } else {
        gridItem = convertView;
        holder = (ViewHolder) gridItem.getTag();
    }

    holder.txtText = (TextView) gridItem.findViewById(R.id.text);
    holder.txtTitle = (TextView) gridItem.findViewById(R.id.title);
    gridItem.setTag(holder);
    SearchResultListItem rowItem = getItem(position);
    holder.txtText.setText(Html.fromHtml(rowItem.getText()), TextView.BufferType.SPANNABLE);
    holder.txtTitle.setText(Html.fromHtml(rowItem.getTitle()), TextView.BufferType.SPANNABLE);

    return gridItem;
}
}

最后我的活动布局包含gridview:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent" >

<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <EditText
            android:id="@+id/searchPatternInput"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@+id/loadSearchResults"
            android:imeOptions="actionSearch"
            android:inputType="text" />

        <ImageButton
            android:id="@+id/showExtendedSearch"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/ic_action_filter" />

        <ImageButton
            android:id="@+id/loadSearchResults"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/showExtendedSearch"
            android:src="@drawable/ic_action_search" />
    </RelativeLayout>
</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#ffffff" >

        <ImageSwitcher
            android:id="@+id/nothingLoaded"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/ic_nothing_loaded" >
        </ImageSwitcher>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true" >

            <GridView
                android:id="@+id/searchResultThumbnailsGridView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:columnWidth="160dp"
                android:divider="#b5b5b5"
                android:dividerHeight="1dp"
                android:gravity="center"
                android:horizontalSpacing="4dp"
                android:listSelector="@drawable/list_selector"
                android:numColumns="auto_fit"
                android:padding="10dp"
                android:layout_weight="1"
                android:stretchMode="columnWidth"
                android:verticalSpacing="4dp" />

        </LinearLayout>
    </RelativeLayout>
</TableRow>

</TableLayout>

2 个答案:

答案 0 :(得分:0)

您似乎使用View.setTag()和View.getTag()方法混淆了。试试这个:

public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;

if (convertView == null)
{
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
    if (this.viewStyle == 0)
    {
        convertView = mInflater.inflate(R.layout.searchresult_list_item, null);
    } else
    {
        convertView = mInflater.inflate(R.layout.searchresult_grid_item, null);
    }
    holder = new ViewHolder();

    //Always use View.findViewById() and View.setTag() here
    holder.txtText = (TextView) convertView.findViewById(R.id.text);
    holder.txtTitle = (TextView) convertView.findViewById(R.id.title);

    convertView.setTag(holder);
} else {
    //Always use View.getTag() here
    holder = (ViewHolder) convertView.getTag();
}

//Always set your data here
SearchResultListItem rowItem = getItem(position);
holder.txtText.setText(Html.fromHtml(rowItem.getText()), TextView.BufferType.SPANNABLE);
holder.txtTitle.setText(Html.fromHtml(rowItem.getTitle()), TextView.BufferType.SPANNABLE);

return convertView;

}

答案 1 :(得分:0)

我继续使用第三方组件。 StaggeredGridView对我来说很好,可以找到更多信息here