Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 重复使用与ListView中的convertView类似的自定义膨胀视图_Android_Android Layout - Fatal编程技术网

Android 重复使用与ListView中的convertView类似的自定义膨胀视图

Android 重复使用与ListView中的convertView类似的自定义膨胀视图,android,android-layout,Android,Android Layout,我目前正在开发一个包含时间表屏幕的应用程序,它是以高度定制的方式构建的,包含很多“重复”视图 我已经在XML中设置了我需要的每个视图(例如,包含事件标题和事件发生时间的框的视图),我在自定义视图类中放大了这些视图。例如: public class EventCell extends RelativeLayout { private TextView eventTitle; private TextView eventTime; private Button favouri

我目前正在开发一个包含时间表屏幕的应用程序,它是以高度定制的方式构建的,包含很多“重复”视图

我已经在XML中设置了我需要的每个视图(例如,包含事件标题和事件发生时间的框的视图),我在自定义视图类中放大了这些视图。例如:

public class EventCell extends RelativeLayout {
    private TextView eventTitle;
    private TextView eventTime;
    private Button favouritesButton;

    public EventCell(Context context) {
        super(context);
        setupView(context);
    }

    public EventCell(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupView(context);
    }

    private void setupView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.timetable_event, this);

        eventTitle = (TextView) findViewById(R.id.event_title);
        eventTime = (TextView) findViewById(R.id.event_time);
        favouritesButton = (Button) findViewById(R.id.favourites_button);   
    }
    ...
}
这很好,只是这个视图在其包含活动中被大量重用。例如,它可能被实例化50次。我的问题是,这会浪费大量内存,并导致一些设备崩溃

在ListView中,有一个getView()方法,该方法提供一个convertView参数,用于检查当前行是否已实例化,然后更新该行上的值。对于这个自定义视图,我想要的是一个类似的东西;理想情况下,重用它,而不是多次实例化它

如果没有办法,最好的解决方法是什么?这些视图本身并不特别复杂,但似乎仍能让大多数设备屈服