【发布时间】:2017-11-03 16:28:10
【问题描述】:
我有一个活动,它从一个 List 中生成一个以编程方式创建的按钮的可滚动列表(比如说一列),这是读取 sqlite 表的结果,我的问题是随着 List 的增长(因此数量按钮)屏幕的初始绘制变得缓慢(目前绘制 50 个按钮需要 3 秒)所以我正在寻找解决方案。
起初我想使用一个线程(可运行,处理程序或任何最好的),假设在 For 内部创建一个新线程,它迭代列表但它不起作用(或者至少我无法让它工作)所以我的问题是下一个:
从列表开始这是创建大量可滚动按钮的最合适的方式,因此用户在访问屏幕时不会有这样的延迟。
分页可能是一种选择,但我想先了解其他可能性并将其作为最后一个资源。
谢谢,下面是我的代码。
public static void createButtons(LinearLayout llContainer,
List<TestType> TestTypes, List<Test> Tests,
int buttonFontSize) {
Context oContext = llContainer.getContext();
String strTheme = TMAppearance.getThemeFromPreferences(oContext);
testMe = ((ApplicationConfiguration)oContext.getApplicationContext());
int callerActivity = TestTypes!=null ? 2 : 1;
if (TestTypes!=null || Tests!=null) {
int lCols = strTheme.equals(testMe.theme_vivid) ? 1 : 2;
//int sourceElementIndex = 0;
int originListSize = calculateOriginalListSize(callerActivity, TestTypes, Tests);
int lRows = (int) Math.ceil((double)originListSize/lCols);
List<String> aStartColors = TMUtils_ThemeVivid.generateStartColorArray(lRows, oContext);
List<String> aEndColors = TMUtils_ThemeVivid.generateEndColorArray(lRows, oContext);
for (i = 0; i < lRows; i++) {
LinearLayout outerButtonLayout = generateOuterButtonLayout(oContext);
for (j = 0; j < lCols; j++) {
final Thread r = new Thread() {
public void run() {
LinearLayout innerButtonLayout = generateInnerButtonLayout(oContext);
outerButtonLayout.addView(innerButtonLayout, j);
if (sourceElementIndex<originListSize){
final TMButton oButton = new TMButton(oContext);
if (callerActivity==1) { //testMenu
setTestMenuButtonSettings(oButton, sourceElementIndex, Tests);
} else {
if (callerActivity==2) { //testTypeMenu
setTestTypeMenuButtonSettings(oButton, sourceElementIndex, TestTypes);
}
}
if (strTheme.equals(testMe.theme_vivid)){
oButton.fontSize = buttonFontSize;
oButton.gradientStartColor = aStartColors.get(i);
oButton.gradientEndColor = aEndColors.get(i);
}else{
if (strTheme.equals(testMe.theme_purple)){
oButton.gradientStartColor = testMe.btnStartColor_purple;
oButton.gradientEndColor = testMe.btnEndColor_purple;
}
}
configureButton(oButton, callerActivity);
oButton.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Context oContext = v.getContext();
TMButton oButton = (TMButton) v;
int callerActivity = Integer.valueOf(v.getTag().toString().split("@")[0]);
String sourceId = String.valueOf(v.getTag().toString().split("@")[1]);
if(event.getAction() == MotionEvent.ACTION_DOWN) { //pressed
setButtonPressed(oButton);
TMSound.playButtonSound(oContext);
} else if (event.getAction() == MotionEvent.ACTION_UP) { //released
setButtonReleased(oButton);
startTargetActivity(callerActivity, sourceId, oContext);
}
return true;
}
});
TMAppearance.doButtonAnimation(oContext, oButton, i);
innerButtonLayout.addView(oButton);
sourceElementIndex++;
}
}
};
r.run();
}
llContainer.addView(outerButtonLayout);
}
}
}
【问题讨论】:
-
您反对使用
ListView或RecycleView吗?通过使用自定义布局,它们可以非常灵活。 -
非常感谢 Barns52 的回复,不,我当然不反对 ListView,事实上我正在研究如何实现 ListView,因为我的按钮是以编程方式生成的。您认为我可以毫无问题地使用带有动态生成按钮的 ListView 吗?
-
我喜欢 ListView(怀旧? ;-) )但 RecyclerView 被介绍为与 ListView 相比具有更好的性能,例如滚动时。
-
再次感谢您的回复@0X0nosugar,但我问了同样的问题:是否可以将 RecycleView 与动态生成的按钮列表一起使用?
-
是的。不可以。可以将 RecyclerView 与包含信息的数据列表一起使用,以便 RecyclerView 适配器可以决定将哪种视图类型用于一个数据列表条目。各种视图类型可能是您不同的按钮类型。所以总而言之,是的:)
标签: java android multithreading programmatically-created