Android 在运行时添加多个视图而不给主线程增加太多负载的正确方法是什么
在我的活动中,我在for循环中创建了大约100个视图按钮、文本视图和图像按钮。我的应用程序在android 5及更低版本上运行缓慢,有时甚至无法加载视图 我的问题是,在不给主线程增加太多负载的情况下,处理这些视图并加载它们的正确方法是什么 我不能使用单独的线程,因为除了主线程外,我不能引用任何其他线程的UI元素 谢谢Android 在运行时添加多个视图而不给主线程增加太多负载的正确方法是什么,android,multithreading,Android,Multithreading,在我的活动中,我在for循环中创建了大约100个视图按钮、文本视图和图像按钮。我的应用程序在android 5及更低版本上运行缓慢,有时甚至无法加载视图 我的问题是,在不给主线程增加太多负载的情况下,处理这些视图并加载它们的正确方法是什么 我不能使用单独的线程,因为除了主线程外,我不能引用任何其他线程的UI元素 谢谢 for (int i = 0; i < arr.size(); i++) { String name = arr.get(i).getName()
for (int i = 0; i < arr.size(); i++) {
String name = arr.get(i).getName();
final int songPath = arr.get(i).getSongPath();
int iconPath = arr.get(i).getIconPath();
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(rlp);
buttons[i] = new ImageButton(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 350);
params.setMargins(0, 0, 0, 8);
buttons[i].setLayoutParams(params);
buttons[i].setBackgroundResource(iconPath);
buttons[i].setTag(name + "button");
buttons[i].setId(id++);
TextView emoteName = new TextView(this);
RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
textParams.addRule(RelativeLayout.BELOW, buttons[i].getId());
textParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textParams.setMargins(20,10,0,0);
emoteName.setText(name.toUpperCase());
emoteName.setId(id++);
emoteName.setTextSize(20f);
emoteName.setLayoutParams(textParams);
Button threedots = new Button(this);
RelativeLayout.LayoutParams threedotsParams = new RelativeLayout.LayoutParams(50, RelativeLayout.LayoutParams.WRAP_CONTENT);
threedotsParams.addRule(RelativeLayout.BELOW, buttons[i].getId());
threedotsParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
threedotsParams.addRule(RelativeLayout.ALIGN_RIGHT,buttons[i].getId());
threedotsParams.setMargins(0,0,0,0);
threedots.setText(getString(R.string.vertical_ellipsis));
threedots.setTextSize(25f);
threedots.setBackgroundColor(Color.TRANSPARENT);
threedots.setLayoutParams(threedotsParams);
final int counterForOnClick = i;
threedots.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ShowDialog(arr.get(counterForOnClick));
}
});
relativeLayout.addView(buttons[i]);
relativeLayout.addView(emoteName);
relativeLayout.addView(threedots);
if (i % 2 == 0) {
leftLayout.addView(relativeLayout);
} else {
rightLayout.addView(relativeLayout);
}
final int index = i;
buttons[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playEmoteSound(songPath);
}
});
}
正如你所说,添加所有这些视图不是最好的主意 从您的代码中,我可以看到我们讨论的是具有不同数据的相同视图 要创建同一视图的列表,需要使用 从: 如果您的应用程序需要根据大数据集或经常更改的数据显示元素滚动列表,则应使用本页所述的RecyclerView
使用RecycleWebHanks,现在我将研究它。我使用RecycleWeber显示所有100个视图,并且工作正常。我已经在我的应用程序中实现了搜索和筛选功能。如何删除RecycleWeber中的所有视图,并将其替换为仅与搜索匹配的视图?或者我应该删除整个RecycleWeber并使用新适配器创建另一个?非常感谢