首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >采用可变尺寸单元的安卓listView单元回收

采用可变尺寸单元的安卓listView单元回收
EN

Stack Overflow用户
提问于 2014-01-24 23:57:19
回答 2查看 850关注 0票数 4

我有一台listView。单元格的高度可变。这在回收电池时会产生一个问题--回收的电池可能高度错误。关闭回收会使滚动变得太不稳定。我如何才能在回收电池的同时获得正确的高度?

编辑:每个地方的高度都不一样。不存在可能的值的简短列表。

EN

回答 2

Stack Overflow用户

发布于 2014-01-25 00:44:11

双电池类型

通常问题出在getView上。我不确定你真正想要的是什么,因为你没有提供你的自定义适配器。但是,我给出了一个具有两种不同单元格类型的示例,但该单元格只包含一个文本视图。

在这种情况下,所有的电池都会随着回收而完全调整。仔细阅读并扩展/修改以满足您的需求。

在你的CustomAdapter

代码语言:javascript
运行
复制
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;
}

单电池类型

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

代码语言:javascript
运行
复制
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;
}

单元格布局

在示例中,我给出的文件row_type.xml (您所在行的布局文件)应该如下所示:

代码语言:javascript
运行
复制
<?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"/>

不要忘记android:layout_height="wrap_content",否则单元格不会调整大小

SIDE COMMENT

这方面的评论没有讨论OP问题,但是,如果人们来这里是因为标题中的cell recycling字样,他们可能会感兴趣,因为我在这里给出了一个使用EditText回收电池的完整示例

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

票数 0
EN

Stack Overflow用户

发布于 2014-01-29 06:48:24

下面是我使用Xamarin解决这个问题的方法。

代码语言:javascript
运行
复制
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;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21336995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档