跳过 148 帧!应用程序可能在其主线程上做了太多工作 android?
Skipped 148 frames! The application may be doing too much work on its main thread, android?
我正在以编程方式创建大量 edittext 和 texview,并将它们放入 tablelayout 中。
这是我的代码
private EditText[] etAgreedVolume, etAgreedSaleParty, etAgreedSalesMgr;
private void createOrderTable(ArrayList<POJOOrder> orderList) {
etAgreedVolume = new EditText[orderList.size()];
etAgreedSaleParty = new EditText[orderList.size()];
etAgreedSalesMgr = new EditText[orderList.size()];
int maxLength = 3;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
for (int i = 0; i < orderList.size(); i++) {
TableRow tableRow = new TableRow(getActivity());
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
TextView tvItem = new TextView(getActivity());
tvItem.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT));
tvItem.setText(orderList.get(i).getBrandWith_SKU());
tvItem.setTextColor(Color.parseColor("#2E3B41"));
tvItem.setGravity(Gravity.CENTER);
tvItem.setPadding(10, 10, 10, 10);
tvItem.setBackgroundResource(R.drawable.rectangle_grey);
TextView tvPlannedVolume = new TextView(getActivity());
tvPlannedVolume.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT));
tvPlannedVolume.setText(orderList.get(i).getPlannedVolume() + " CS");
tvPlannedVolume.setTextColor(Color.parseColor("#2E3B41"));
tvPlannedVolume.setGravity(Gravity.CENTER);
tvPlannedVolume.setPadding(10, 10, 10, 10);
tvPlannedVolume.setBackgroundResource(R.drawable.rectangle_grey);
LinearLayout linearLayout1 = new LinearLayout(getActivity());
linearLayout1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
TextView tvPlannedSalesParty = new TextView(getActivity());
tvPlannedSalesParty.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
tvPlannedSalesParty.setText(orderList.get(i).getPlannedSalesPromoParty() + "");
tvPlannedSalesParty.setTextColor(Color.parseColor("#2E3B41"));
tvPlannedSalesParty.setGravity(Gravity.CENTER);
tvPlannedSalesParty.setPadding(10, 10, 10, 10);
tvPlannedSalesParty.setBackgroundResource(R.drawable.rectangle_grey);
TextView tvPlannedSalesMgr = new TextView(getActivity());
tvPlannedSalesMgr.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
tvPlannedSalesMgr.setText(orderList.get(i).getPlannedSalesPromoMgr() + "");
tvPlannedSalesMgr.setTextColor(Color.parseColor("#2E3B41"));
tvPlannedSalesMgr.setGravity(Gravity.CENTER);
tvPlannedSalesMgr.setPadding(10, 10, 10, 10);
tvPlannedSalesMgr.setBackgroundResource(R.drawable.rectangle_grey);
linearLayout1.addView(tvPlannedSalesParty);
linearLayout1.addView(tvPlannedSalesMgr);
etAgreedVolume[i] = new EditText(getActivity());
etAgreedVolume[i].setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT));
etAgreedVolume[i].setTextColor(ContextCompat.getColor(getActivity(), android.R.color.black));
etAgreedVolume[i].setGravity(Gravity.CENTER);
etAgreedVolume[i].setHintTextColor(Color.parseColor("#888888"));
etAgreedVolume[i].setHint("0");
etAgreedVolume[i].setPadding(10, 10, 10, 10);
etAgreedVolume[i].setBackgroundResource(R.drawable.rectangle_grey);
etAgreedVolume[i].setInputType(InputType.TYPE_CLASS_NUMBER);
etAgreedVolume[i].addTextChangedListener(watcherAgreedVolume);
etAgreedVolume[i].setFilters(FilterArray);
LinearLayout linearLayout2 = new LinearLayout(getActivity());
linearLayout2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
etAgreedSaleParty[i] = new EditText(getActivity());
etAgreedSaleParty[i].setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
etAgreedSaleParty[i].setTextColor(ContextCompat.getColor(getActivity(), android.R.color.black));
etAgreedSaleParty[i].setGravity(Gravity.CENTER);
etAgreedSaleParty[i].setHintTextColor(Color.parseColor("#888888"));
etAgreedSaleParty[i].setHint("0");
etAgreedSaleParty[i].setPadding(10, 10, 10, 10);
etAgreedSaleParty[i].setBackgroundResource(R.drawable.rectangle_grey);
etAgreedSaleParty[i].setInputType(InputType.TYPE_CLASS_NUMBER);
etAgreedSaleParty[i].addTextChangedListener(watcherAgreedVolume);
etAgreedSaleParty[i].setFilters(FilterArray);
etAgreedSalesMgr[i] = new EditText(getActivity());
etAgreedSalesMgr[i].setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
etAgreedSalesMgr[i].setTextColor(ContextCompat.getColor(getActivity(), android.R.color.black));
etAgreedSalesMgr[i].setGravity(Gravity.CENTER);
etAgreedSalesMgr[i].setPadding(10, 10, 10, 10);
etAgreedSalesMgr[i].setHintTextColor(Color.parseColor("#888888"));
etAgreedSalesMgr[i].setHint("0");
etAgreedSalesMgr[i].setBackgroundResource(R.drawable.rectangle_grey);
etAgreedSalesMgr[i].setInputType(InputType.TYPE_CLASS_NUMBER);
etAgreedSalesMgr[i].addTextChangedListener(watcherAgreedVolume);
etAgreedSalesMgr[i].setFilters(FilterArray);
linearLayout2.addView(etAgreedSaleParty[i]);
linearLayout2.addView(etAgreedSalesMgr[i]);
tableRow.addView(tvItem);
tableRow.addView(tvPlannedVolume);
tableRow.addView(linearLayout1);
tableRow.addView(etAgreedVolume[i]);
tableRow.addView(linearLayout2);
tableLayout.addView(tableRow);
}
}
我的应用程序的这个页面变得太慢了。有时甚至会因为工作繁重而出现 ANR。
我怎样才能让它顺利。
在单独的 Uithread 上更新您的观点
样本:
private void runThread() {
new Thread() {
public void run() {
while (/*your condition*/) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
//update your views here
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
如果你更新你的视图,在你的情况下,主线程会做很多工作,导致跳帧和 ANRs
选项 1
在您的 AsyncTask
执行期间创建一个 AsyncTask
and do all the processing there, finally set the layout elements in onPostExecute
of the AsyncTask
. Additionally you may want to show a ProgressBar
,并在 onPostExecute
中关闭它。
向下滚动到 AsyncTask
documentation 中的副标题 "The 4 steps" 以更好地理解此解决方案。
选项 2
创建一个 XML 文件(例如 myTableRow.xml),其中包含您的 TableRow
及其所有 children,设置所有常用参数,即执行的参数不依赖于iterator integer i
。所以基本上你是在创建一个所有人通用的 TableRow
模板。
然后在for Loop
中你剩下要做的就是:为每次迭代膨胀myTableRow.xml
,设置剩余参数(取决于i
),最后将其添加到tableLayout
。
注意:应该可以将您的 EditText
视图 etAgreedVolume[i]
、etAgreedSaleParty[i]
和 etAgreedSalesMgr[i]
添加到 XML 文件(即您的TableRow
模板)尽管它们依赖于 i
。我建议你通过简单地创建(标准)EditText
视图来做到这一点,然后一旦你膨胀了 TableRow
你就可以检索这些 EditTexts
并将它们添加到数组中。
我不能保证 "Option 2" 会 运行 事情变得更快或更顺畅,但是,在大多数情况下,扩充布局比使用 hard-coded 创建布局更快 java.
必须使用 listView 来创建视图。
表格布局无法容纳如此庞大的数据。
我正在以编程方式创建大量 edittext 和 texview,并将它们放入 tablelayout 中。 这是我的代码
private EditText[] etAgreedVolume, etAgreedSaleParty, etAgreedSalesMgr;
private void createOrderTable(ArrayList<POJOOrder> orderList) {
etAgreedVolume = new EditText[orderList.size()];
etAgreedSaleParty = new EditText[orderList.size()];
etAgreedSalesMgr = new EditText[orderList.size()];
int maxLength = 3;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
for (int i = 0; i < orderList.size(); i++) {
TableRow tableRow = new TableRow(getActivity());
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
TextView tvItem = new TextView(getActivity());
tvItem.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT));
tvItem.setText(orderList.get(i).getBrandWith_SKU());
tvItem.setTextColor(Color.parseColor("#2E3B41"));
tvItem.setGravity(Gravity.CENTER);
tvItem.setPadding(10, 10, 10, 10);
tvItem.setBackgroundResource(R.drawable.rectangle_grey);
TextView tvPlannedVolume = new TextView(getActivity());
tvPlannedVolume.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT));
tvPlannedVolume.setText(orderList.get(i).getPlannedVolume() + " CS");
tvPlannedVolume.setTextColor(Color.parseColor("#2E3B41"));
tvPlannedVolume.setGravity(Gravity.CENTER);
tvPlannedVolume.setPadding(10, 10, 10, 10);
tvPlannedVolume.setBackgroundResource(R.drawable.rectangle_grey);
LinearLayout linearLayout1 = new LinearLayout(getActivity());
linearLayout1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
TextView tvPlannedSalesParty = new TextView(getActivity());
tvPlannedSalesParty.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
tvPlannedSalesParty.setText(orderList.get(i).getPlannedSalesPromoParty() + "");
tvPlannedSalesParty.setTextColor(Color.parseColor("#2E3B41"));
tvPlannedSalesParty.setGravity(Gravity.CENTER);
tvPlannedSalesParty.setPadding(10, 10, 10, 10);
tvPlannedSalesParty.setBackgroundResource(R.drawable.rectangle_grey);
TextView tvPlannedSalesMgr = new TextView(getActivity());
tvPlannedSalesMgr.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
tvPlannedSalesMgr.setText(orderList.get(i).getPlannedSalesPromoMgr() + "");
tvPlannedSalesMgr.setTextColor(Color.parseColor("#2E3B41"));
tvPlannedSalesMgr.setGravity(Gravity.CENTER);
tvPlannedSalesMgr.setPadding(10, 10, 10, 10);
tvPlannedSalesMgr.setBackgroundResource(R.drawable.rectangle_grey);
linearLayout1.addView(tvPlannedSalesParty);
linearLayout1.addView(tvPlannedSalesMgr);
etAgreedVolume[i] = new EditText(getActivity());
etAgreedVolume[i].setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT));
etAgreedVolume[i].setTextColor(ContextCompat.getColor(getActivity(), android.R.color.black));
etAgreedVolume[i].setGravity(Gravity.CENTER);
etAgreedVolume[i].setHintTextColor(Color.parseColor("#888888"));
etAgreedVolume[i].setHint("0");
etAgreedVolume[i].setPadding(10, 10, 10, 10);
etAgreedVolume[i].setBackgroundResource(R.drawable.rectangle_grey);
etAgreedVolume[i].setInputType(InputType.TYPE_CLASS_NUMBER);
etAgreedVolume[i].addTextChangedListener(watcherAgreedVolume);
etAgreedVolume[i].setFilters(FilterArray);
LinearLayout linearLayout2 = new LinearLayout(getActivity());
linearLayout2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
etAgreedSaleParty[i] = new EditText(getActivity());
etAgreedSaleParty[i].setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
etAgreedSaleParty[i].setTextColor(ContextCompat.getColor(getActivity(), android.R.color.black));
etAgreedSaleParty[i].setGravity(Gravity.CENTER);
etAgreedSaleParty[i].setHintTextColor(Color.parseColor("#888888"));
etAgreedSaleParty[i].setHint("0");
etAgreedSaleParty[i].setPadding(10, 10, 10, 10);
etAgreedSaleParty[i].setBackgroundResource(R.drawable.rectangle_grey);
etAgreedSaleParty[i].setInputType(InputType.TYPE_CLASS_NUMBER);
etAgreedSaleParty[i].addTextChangedListener(watcherAgreedVolume);
etAgreedSaleParty[i].setFilters(FilterArray);
etAgreedSalesMgr[i] = new EditText(getActivity());
etAgreedSalesMgr[i].setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
etAgreedSalesMgr[i].setTextColor(ContextCompat.getColor(getActivity(), android.R.color.black));
etAgreedSalesMgr[i].setGravity(Gravity.CENTER);
etAgreedSalesMgr[i].setPadding(10, 10, 10, 10);
etAgreedSalesMgr[i].setHintTextColor(Color.parseColor("#888888"));
etAgreedSalesMgr[i].setHint("0");
etAgreedSalesMgr[i].setBackgroundResource(R.drawable.rectangle_grey);
etAgreedSalesMgr[i].setInputType(InputType.TYPE_CLASS_NUMBER);
etAgreedSalesMgr[i].addTextChangedListener(watcherAgreedVolume);
etAgreedSalesMgr[i].setFilters(FilterArray);
linearLayout2.addView(etAgreedSaleParty[i]);
linearLayout2.addView(etAgreedSalesMgr[i]);
tableRow.addView(tvItem);
tableRow.addView(tvPlannedVolume);
tableRow.addView(linearLayout1);
tableRow.addView(etAgreedVolume[i]);
tableRow.addView(linearLayout2);
tableLayout.addView(tableRow);
}
}
我的应用程序的这个页面变得太慢了。有时甚至会因为工作繁重而出现 ANR。 我怎样才能让它顺利。
在单独的 Uithread 上更新您的观点 样本:
private void runThread() {
new Thread() {
public void run() {
while (/*your condition*/) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
//update your views here
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
如果你更新你的视图,在你的情况下,主线程会做很多工作,导致跳帧和 ANRs
选项 1
在您的 AsyncTask
执行期间创建一个 AsyncTask
and do all the processing there, finally set the layout elements in onPostExecute
of the AsyncTask
. Additionally you may want to show a ProgressBar
,并在 onPostExecute
中关闭它。
向下滚动到 AsyncTask
documentation 中的副标题 "The 4 steps" 以更好地理解此解决方案。
选项 2
创建一个 XML 文件(例如 myTableRow.xml),其中包含您的 TableRow
及其所有 children,设置所有常用参数,即执行的参数不依赖于iterator integer i
。所以基本上你是在创建一个所有人通用的 TableRow
模板。
然后在for Loop
中你剩下要做的就是:为每次迭代膨胀myTableRow.xml
,设置剩余参数(取决于i
),最后将其添加到tableLayout
。
注意:应该可以将您的 EditText
视图 etAgreedVolume[i]
、etAgreedSaleParty[i]
和 etAgreedSalesMgr[i]
添加到 XML 文件(即您的TableRow
模板)尽管它们依赖于 i
。我建议你通过简单地创建(标准)EditText
视图来做到这一点,然后一旦你膨胀了 TableRow
你就可以检索这些 EditTexts
并将它们添加到数组中。
我不能保证 "Option 2" 会 运行 事情变得更快或更顺畅,但是,在大多数情况下,扩充布局比使用 hard-coded 创建布局更快 java.
必须使用 listView 来创建视图。 表格布局无法容纳如此庞大的数据。