作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我正在使用多个 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);
}
});
}
请提出任何建议。谢谢
最佳答案
关于android - For 循环导致应用程序可能在其主线程上做太多工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49023648/
有人可以向我澄清主线 DHT 规范中的声明吗? Upon inserting the first node into its routing table and when starting up th
我正在尝试使用 USB 小工具驱动程序使嵌入式设备作为 MTP 设备工作。 我知道 Android 从大容量存储设备切换到 MTP 设备已经有一段时间了,并且找到了 source code for M
我是一名优秀的程序员,十分优秀!