gpt4 book ai didi

android - For 循环导致应用程序可能在其主线程上做太多工作

转载 作者:行者123 更新时间:2023-11-29 02:32:11 24 4
gpt4 key购买 nike

您好,我正在使用多个 for 循环在适配器类中动态添加 View 。它导致应用程序可能在其主线程上做太多工作。

当我单击我正在调用 notifyItemChanged 的​​特定项目时,应用程序变慢了。我正在使用 runOnUiThread 更新适配器中的 View ,但它仍然卡住应用程序一段时间并收到警告消息应用程序可能在其主线程上做了太多工作

代码:

适配器类

private void bind() {
try {
((Activity) mContext).runOnUiThread(() -> {
mFlowLayout.removeAllViews();
for (Element element : elementList) {
prepareElement(element, mFlowLayout);
}
mFlowLayout.removeViewAt(mFlowLayout.getChildCount() - 1);
});
} catch (Exception e) {
//e.print("");
}
}


private void prepareElement(Element element, FlowLayout flowLayout) {
if (element.getSentenceList() != null) {
if (element.getSentenceList().size() > 0) {
updateSentenceList(element, element.getSentenceList().get(0).getPhraseList(),
flowLayout, element.getSentenceList().get(0).getTemplateId());
} else {
addView(element.getElementName(), flowLayout);
}
} else {
addView(element.getElementName(), flowLayout);
}
}

private void updateSentenceList(Element element, List<PhraseList> phraseList, FlowLayout flowLayout,
String templateId) {
if (phraseList.size() > 0) {
prepareSuggestions(element, phraseList, flowLayout, templateId);
} else {
addView(element.getElementName(), flowLayout);
}
}

private void prepareSuggestions(Element element, List<PhraseList> suggestions, FlowLayout flowLayout, String templateId) {
for (int i = 0; i < suggestions.size(); i++) {
PhraseList phraseList = suggestions.get(i);
if (phraseList.getHighlight() != null) {
if (phraseList.getHighlight().equalsIgnoreCase("Normal")
&& !phraseList.isClicked()) {
addView(phraseList, flowLayout, i, suggestions.size(), templateId, element);
}
} else {
if (!phraseList.isClicked()) {
addView(phraseList, flowLayout, i, suggestions.size(), templateId, element);
}
}
}
}

private void addView(PhraseList suggestion, FlowLayout flowLayout, int position, int totalSize,
String templateId, Element element) {
View currentView = LayoutInflater.from(mContext).inflate(R.layout.item_flow_layout
, flowLayout, false);
View indicatorView = LayoutInflater.from(mContext).inflate(R.layout.custom_indicator
, flowLayout, false);
TextView textView = currentView.findViewById(R.id.text_view_element_with_attribute);
textView.setBackgroundResource(R.drawable.bottom_line_black);
if (suggestion.getIsKey()) {
textView.setText(suggestion.getPhrase());
textView.setTypeface(FontCache.getInstance(mContext).getFont(FontCache.Font.REGULAR));
} else {
textView.setText(Utils.changeStringCase(suggestion.getKey()));
textView.setTypeface(FontCache.getInstance(mContext).getFont(FontCache.Font.BOLD));
}
textView.setOnClickListener(onClickView -> {
((RichElementSelectionActivity) mContext).onClickItem(element, templateId);
});
((Activity)mContext).runOnUiThread(() -> {
flowLayout.addView(currentView);
if (position == totalSize - 1) {
flowLayout.addView(indicatorView);
}
});
}

请提出任何建议。谢谢

最佳答案

  • 你应该改用 RecyclerView
  • 繁重的任务应该在后台线程中完成:Asynctask、RxAndroid...

关于android - For 循环导致应用程序可能在其主线程上做太多工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49023648/

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