gpt4 book ai didi

java - 无条件布局膨胀: Should use View Holder pattern

转载 作者:行者123 更新时间:2023-12-01 16:47:04 24 4
gpt4 key购买 nike

我是新手,我尝试膨胀项目行,但它会在滚动时减慢应用程序的速度。在下面的代码中,它说我需要使用 View Holder。

        row = inflater.inflate(mLayoutResourceId, parent, false);

我该如何解决这个问题?

package com.example.android.ontrack.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.android.ontrack.R;
import com.example.android.ontrack.models.Order;

public class OrdersList extends ArrayAdapter<Order> {

Context mContext;
int mLayoutResourceId;
Order mData[] = null;

public OrdersList(Context context, int resource, Order[] data) {
super(context, resource, data);
this.mContext = context;
this.mLayoutResourceId = resource;
this.mData = data;
}


@Override
public Order getItem(int position) {
return super.getItem(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;

LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(mLayoutResourceId, parent, false);

TextView nameOfSchool = row.findViewById(R.id.school_name_orders);
TextView nameOfAgent = row.findViewById(R.id.agent_name);
TextView orderId = row.findViewById(R.id.order_id);
TextView netRevenue = row.findViewById(R.id.net_revenue);
TextView orderQuantity = row.findViewById(R.id.total_quantity);
TextView date = row.findViewById(R.id.date);

Order orders = mData[position];

nameOfSchool.setText(orders.nameOfSchool);
nameOfAgent.setText(String.valueOf(orders.nameOfAgent));
orderId.setText(String.valueOf(orders.orderId));
netRevenue.setText(String.valueOf(orders.netRevenue));
orderQuantity.setText(String.valueOf(orders.totalQuantity));
date.setText(String.valueOf(orders.date));

return row;
}
}

最佳答案

您的问题出在这段代码中:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;

LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(mLayoutResourceId, parent, false);

...
return row;
}

警告消息表明,无论系统将什么传递给 getView() ,你总是打电话inflater.inflate()生成您的row .

首先要了解的是 convertView参数传递给getView()有时为空,有时不为空。 当它不为 null 时,它是一个“旧”(“回收”) View ,之前从 getView() 返回。换句话说,它与您自己膨胀的 View 类型完全相同,只是其中包含旧数据。

因此,您可以通过将上面的代码更改为以下内容来解决该问题:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;

if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(mLayoutResourceId, parent, false);
} else {
row = convertView;
}

...
return row;
}

第二个要理解的是findViewById()是慢的。不像慢几秒那样慢,但足够慢,如果你必须运行它数百次,就会导致你的 UI 滞后。因此,“ View 持有者模式”是一种避免每次都查找这些 View 的方法。

您要做的第一件事是创建一个类,其中包含行中每个 View 的字段。鉴于您发布的内容,看起来像这样:

private static class ViewHolder {

private final TextView nameOfSchool;
private final TextView nameOfAgent;
private final TextView orderId;
private final TextView netRevenue;
private final TextView orderQuantity;
private final TextView date;

private ViewHolder(View row) {
nameOfSchool = row.findViewById(R.id.school_name_orders);
nameOfAgent = row.findViewById(R.id.agent_name);
orderId = row.findViewById(R.id.order_id);
netRevenue = row.findViewById(R.id.net_revenue);
orderQuantity = row.findViewById(R.id.total_quantity);
date = row.findViewById(R.id.date);
}
}

现在您必须将其插入您的getView()代码。您将需要创建一个新的 ViewHolder每次你膨胀一个 View 时(即当 convertView 为空时),你会想要重新使用现有的 ViewHolder否则。我们为此使用 View 的标签。

总的来说,它看起来像这样:

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

if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(mLayoutResourceId, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
} else {
row = convertView;
holder = (ViewHolder) row.getTag();
}

Order orders = mData[position];

holder.nameOfSchool.setText(orders.nameOfSchool);
holder.nameOfAgent.setText(String.valueOf(orders.nameOfAgent));
holder.orderId.setText(String.valueOf(orders.orderId));
holder.netRevenue.setText(String.valueOf(orders.netRevenue));
holder.orderQuantity.setText(String.valueOf(orders.totalQuantity));
holder.date.setText(String.valueOf(orders.date));

return row;
}

关于java - 无条件布局膨胀: Should use View Holder pattern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48600914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com