我有 4 个TextViews
在GridView
给定的Fragment
. 我的主要Activity
通过 a 得到通知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
s 的请求的函数。
@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;
}
}
我越来越绝望了。