0

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

如以下滚动前后的屏幕截图所示,由于我再次向下和向上滚动,因此行为符合预期:行混乱并剪切大项目的内容。我错过了什么?

滚动的Gridview :

正确滚动之前的gridview屏幕截图

向下和向上滚动后的GridView

滚动混乱项目后的gridview屏幕截图

进一步的代码:

项目 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(如果可能,回收视图):

公共类 SearchResultListViewAdapter 扩展 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>
4

2 回答 2

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;

}

于 2013-06-15T05:49:25.937 回答
0

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

于 2014-06-02T11:26:46.320 回答