我对 android Fragment 类有疑问。我已经实现了 FragmentStatePagerAdapter 等。现在我的问题如下:我将自己的视图类添加到每个“LSDataFragment”:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
mConnection = ((WorkActivity) this.getActivity()).getConnection(args
.getInt(ARG_OBJECT));
rootView = inflater.inflate(R.layout.view_pager, container, false);
mView = new LSDataView(this.getActivity().getApplicationContext(),(WorkActivity) this.getActivity());
update();
return rootView;
}
public void update(){
final RelativeLayout mainLayout = (RelativeLayout) rootView.findViewById(R.id.pagerMainLayout);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
mainLayout.addView(mView, params);
mainLayout.invalidate();
//doesn't work outside the uiThread?!
((WorkActivity) this.getActivity()).runOnUiThread(new Runnable() {
@Override
public void run() {
mainLayout.setBackgroundColor(Color.BLUE);
mainLayout.addView(mView, params);
mainLayout.invalidate();
}
});
}
但是当我从我的 FragmentStatePagerAdapter 中删除 Fragment 时,LSDataView mView 仍保留在屏幕上,而不是被破坏(wtf):
for(Connection c : WorkActivity.this.mConnections){
if(c.getHandler() == handler)
WorkActivity.this.mConnections.remove(c);
}
WorkActivity.this.runOnUiThread(new Runnable(){
@Override
public void run() {
mCollectionPagerAdapter.notifyDataSetChanged();
}
});