我在 GridView
中有 4 个 TextViews
,在给定的 Fragment
中有一个自定义适配器。我的主要 Activity
通过 TimerTask
收到通知,为正确的 TextView
更新文本颜色。这适用于所有 TextViews
,但应用首次启动时第一个除外。之后它像其余的 TextViews
一样正常工作。
在相关的 Fragment
中:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.short_press_info_fragment, container, false);
GridView grid = (GridView) view.findViewById(R.id.grid_view);
infoAdapter = new ShortPressInfoAdapter(mKeyInfo, getActivity());
grid.setAdapter(infoAdapter);
return view;
}
在适配器中:
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(convertView == null) {
Log.d("GRID_VIEW", "inflating");
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.grid_item, null);
TextView text = texts[position] = (TextView) v.findViewById(R.id.gridItemText);
Log.d("GRID_VIEW", "creating view");
text.setText(mKeyInfo[position]);
switch(position) {
case 0:
text.setBackgroundColor(mContext.getResources().getColor(R.color.blue));
break;
case 1:
text.setBackgroundColor(mContext.getResources().getColor(R.color.yellow));
break;
case 2:
text.setBackgroundColor(mContext.getResources().getColor(R.color.green));
break;
case 3:
text.setBackgroundColor(mContext.getResources().getColor(R.color.red));
break;
default:
break;
}
}
return v;
}
在主要的 Activity
中:
public void emphasizeShort(final PressID p, final boolean b) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(b) {
Log.d(TAG, "setting short " + p + " to black");
getShortTextView(p).setTextColor(getResources().getColor(R.color.black));
}
else {
Log.d(TAG, "setting short " + p + " to white");
getShortTextView(p).setTextColor(getResources().getColor(R.color.white));
}
}
});
}
无论我在应用程序启动时尝试更改哪个 TextView
,日志都显示相同的输出,但第一个 TextView
是唯一一个根本不这样做的输出不改变。
什么会导致 View
在应用程序启动时而不是之后忽略更改?从这段代码中我不明白为什么第一个 TextView
被挑出来。
如有任何帮助,我们将不胜感激。
编辑:
下面是发送请求以强调 TextView
的函数。
@Override
public boolean onTouch(final View v, final MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) { // button pressed
pressTime = System.currentTimeMillis();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Log.d("TIMER_TASK", "short press "
+ (System.currentTimeMillis() - pressTime));
mFourButtonListener.onShortPress(buttonID);
}
}, SHORT_PRESS_DELAY);
timer.schedule(new TimerTask() {
@Override
public void run() {
Log.d("TIMER_TASK", "long press "
+ (System.currentTimeMillis() - pressTime));
mFourButtonListener.onLongPress(buttonID);
}
}, LONG_PRESS_DELAY);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
duration = System.currentTimeMillis() - pressTime;
timer.cancel();
timer.purge();
if (duration >= SHORT_PRESS_DELAY && duration < LONG_PRESS_DELAY)
mFourButtonListener.onShortRelease(buttonID);
else if (duration >= LONG_PRESS_DELAY)
mFourButtonListener.onLongRelease(buttonID);
return true;
}
return false;
}
}
我越来越绝望了。
最佳答案
这里有很多缺失的代码,但如果您遇到应用程序首次启动时无法运行的问题,那么问题很可能是由 runOnUiThread
引起的,这根本不是启动时可靠。 (即,并不是每一个发布的任务都能保证运行。)
添加一个类级别的Handler
变量
Handler h;
并在 onCreate
h = new Handler();
然后使用h.post()
代替runOnUiThread
。
关于android - GridView 中的特定 TextView 未在应用程序初始化时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19993630/