使用可变大小的单元格回收Android listView单元格

时间:2014-01-24 15:57:19

标签: android listview

我有一个listView。细胞高度可变。这会在回收细胞时造成问题 - 回收的细胞可能具有错误的高度。关闭回收使滚动过于波动。如何在仍然回收细胞的同时获得正确的高度?

编辑:各地的高度各不相同。没有可能值的简短列表。

2 个答案:

答案 0 :(得分:0)

两种类型

通常问题来自getView。我不确定你真正在寻找什么,因为你没有提供自定义适配器。但是,我举例说明了两种不同的细胞类型,但细胞只包含一个文本视图。

在这种情况下,所有细胞都通过回收完全调整。仔细阅读并扩展/修改以满足您的需求。

CustomAdapter

private final class CustomArrayAdapter extends ArrayAdapter<String> {
    //the fastest is to get the layout inflater once and not each time getView is called
    private LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    public CustomArrayAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    ...

    //here depending on the item position determine if cell is of type 1 or 2
    @Override
    public int getItemViewType(int position) {
        if (/* your type 1 criteria is match */) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
                case TYPE_1:
                    convertView = mInflater.inflate(R.layout.row_of_type_1, parent, false);
                    holder.text = (TextView) convertView.findViewById(R.id.textview_type_1);
                    break;
                case TYPE_2:
                    convertView = mInflater.inflate(R.layout.row_of_type_2, parent, false);
                    holder.text = (TextView) convertView.findViewById(R.id.textview_type_2);
                    break;
             }
             convertView.setTag(holder);
         } else {
             holder = (ViewHolder) convertView.getTag();
         }

         //get the text for that specific cell and set it
         holder.text.setText(getMyCustomTextForThisCell());

         return convertView;
    }
}

static class ViewHolder {     
    TextView text;
}

单一小区类型

如果您只需要一个类型的单元格,其中包含单个文本视图,并希望在所有单元格中放置不同长度的文本,并且单元格自动调整其高度,只需在{{1}中使用以下代码}

CustomAdapter

CELL LAYOUT

在示例中,我给文件row_type.xml(行的布局文件)看起来应该是这样的:

private final class CustomArrayAdapter extends ArrayAdapter<String> {
    //the fastest is to get the layout inflater once and not each time getView is called
    private LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    public CustomArrayAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    ...

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

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.row_type, parent, false);
            holder.text = (TextView) convertView.findViewById(R.id.textview_in_your_row);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //get the text for that specific cell and set it
        holder.text.setText(getMyCustomTextForThisCell());

        return convertView;
    }
}

static class ViewHolder {     
    TextView text;
}

不要忘记<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textview_in_your_row" android:paddingLeft="6dp" android:paddingRight="6dp" android:layout_width="match_parent" android:layout_height="wrap_content"/> 或者单元格不会调整大小


SIDE COMMENT

这方面的评论没有谈到OP问题,但如果人们因为标题中的单元回收字来到这里,他们可能会对我提供回收的完整示例感兴趣此处带有EditText的单元格

The content of EditText contained into a Android ListView isn't saved

答案 1 :(得分:0)

以下是我使用Xamarin解决它的方法。

public override View GetView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        int type = GetItemViewType(position);
        if (convertView == null)
        {
            holder = new ViewHolder();
            convertView = context.LayoutInflater.Inflate(Resource.Layout.my_row_layout, parent, false);
            holder.myTextView = FindViewById<TextView>(Resource.Id.viewRow);
            convertView.Tag = holder;
        }
        else 
        {
            holder = (ViewHolder)convertView.Tag;
            int heightWeWant = heights[type];
            if (holder.myTextView.Height != heightWeWant) //height we have isn't what we want
            {
                AbsListView.LayoutParams parms = new AbsListView.LayoutParams(LinearLayout.LayoutParams.MatchParent, heightWeWant); //Width, Height //resize the view to the height we want
                convertView.LayoutParameters = parms;
            }
            convertView.Tag = holder;
        }

        holder.myText = "Some text here";

        return convertView;
    }