【发布时间】:2018-03-06 14:56:51
【问题描述】:
在我的应用程序中,我试图在片段可见时更改 LinearLayout 的背景颜色。当我不使用setContentView() 更改布局时,这可以正常工作。我想在发生某些事情时显示一个 ListView,所以我用setContentView() 显示,在选择项目后,我使用setContentView() 将布局设置回主布局,但由于某种原因,fragment.isVisible 返回 false。这是代码:
private void setFeedback(int color) {
Log.d(TAG, "set background color to:" + color + " mmainfragment is " + mMainFragment + mMainFragment.isAdded() + mMainFragment.isHidden() + mMainFragment.isVisible());
if (mMainFragment != null && mMainFragment.isVisible()) {
Log.d(TAG, "****COLOR ON THE SCREEN");
mMainFragment.mLayout.setBackgroundColor(color);
mFeedbackTask = new FeedbackTask();
mFeedbackTask.execute();
}
}
它在启动时记录以下内容:
将背景颜色设置为:-3407872 mmainfragment is MainFragment{41c30588 #0 id=0x7f07003c}true false true
然后我使用 setContentView 到 ListView 如下:
setContentView(R.layout.feedback_popup);
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.feedback_list, feedback.getOptions());
ListView listView = (ListView) findViewById(R.id.feedback_screen);
listView.setAdapter(adapter);
回到主视图如下:
setContentView(R.layout.overview);
然后当我再次尝试时,它会记录以下内容:
设置背景颜色为:-3407872 mmainfragment is MainFragment{41c30588 #0 id=0x7f07003c}true false false
isAdded() 和 isHidden() 分别返回 true 和 false 为什么不可见?
【问题讨论】:
-
奇怪的是你setContentView 2 次。我想当你再次设置它时,它将被重新渲染。如果你有2个布局,为什么不分成2个片段呢?
-
成功了,谢谢!
标签: android listview android-fragments