我在GridView中有4个TextView,在给定的片段中有自定义适配器.我的主要Activity通过TimerTask通知更新正确TextView的文本颜色.这适用于除应用程序首次启动时的第一个TextView之外的所有TextView.之后它像其他TextView一样正常工作.
在有问题的片段中:
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;
}
在主要活动中:
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是唯一不会更改的TextView.
什么会导致View在应用启动时忽略更改但不在之后?从这段代码我不明白为什么第一个TextView被挑选出来.
任何帮助将不胜感激.
编辑:
这是发送请求以强调TextViews的函数.
@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.
标签:timertask,android,android-fragments,gridview,textview
来源: https://codeday.me/bug/20190831/1774956.html