我有一个获取GPS信号的片段,并将结果发送到自定义视图。
我可以显示片段中获取的信号的输出,并看到它更新得很好。这是fragment1的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.gps_stamp_fragment, container,false);
RelativeLayout rl1 = new RelativeLayout(view.getContext());
TextView tView1 = new TextView(view.getContext());
tView1.setText("waiting..."); //this is the only thing custom view gets!
rl1.addView(tView1);
rl1.setId(1);
tView1.setId(2);
view.addView (rl1);
lm =(LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);
return view;
}
//And here's where I get the GPS signal from and use setText again to update to the GPS signal.
@Override
public void onLocationChanged(Location arg0) {
String lat = String.valueOf(arg0.getLatitude());
RelativeLayout rl1 = (RelativeLayout) getView().findViewById(1);
TextView tView1 = (TextView) rl1.findViewById(2);
tView1.setText(lat);
}
然后,在自定义视图中,我有以下代码,用于查找相对布局和关联的文本视图并获取文本。
RelativeLayout rl1 = (RelativeLayout) getRootView().findViewById(1);
TextView tView1 = (TextView) rl1.findViewById(2);
tView1.getText();
getRootView().invalidate();
问题是,此自定义视图中出现的唯一文本是"等待..."。 GPS测量永远不会出现。
我已经阅读了类似的问题,我需要.invalidate()视图,这就是我添加最后一行的原因,但这对我来说似乎没有用。
为什么TextView在从fragment1调用时正确更新,但在从自定义视图调用时却没有?
感谢。
答案 0 :(得分:2)
invalidate
应位于onLocationChanged