我已经实现了由两个片段组成的活动(称为 HomeScreenActivity)。其中一个显示项目列表,另一个显示所选项目的详细信息。详细信息片段(我的代码中的 ResumeFragment)由一系列组件组成,用户可以通过将其指定为配置文件中的多个单元格来配置高度和宽度。这些组件被添加到名为 ComponentsLayout 的自定义布局中。
打开 HomeScreenActivity 时,我在 onCreate 方法中显示每个片段,它们按预期显示。但是,当我单击项目列表片段中的项目,并将 resumeFragment 替换为同一类的新实例时,什么也没有显示。
我花了很多时间调试,我看到 resumeFragment 确实填写了它给定的高度和宽度,但是 componentsLayout 的高度只有 1。我发现这是因为组件的高度和宽度即使我在 componentsLayout 的 onMeasure 方法中设置了它们的 layoutParams,但没有设置包含在 componentsLayout 中。
下面是 HomeScreenActivity、ResumeFragment 和 ComponentsLayout 的代码:
public class HomeScreenActivity extends FragmentActivity implements OnItemSelectedListener {
.
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
.
.
//Check if device is in landscape orientation
if(width > height && isTablet) {
//The patient context fragment must maintain the same width as when in portrait orientation.
findViewById(R.id.patient_resume_fragment_container).setLayoutParams(new LinearLayout.LayoutParams(height, height));
findViewById(R.id.screen_pager).setLayoutParams(new LinearLayout.LayoutParams(width-height, height));
fragmentManager = getSupportFragmentManager();
patientResumeFragment = new ResumeFragment();
testFragment = new NysomTestFragment();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.patient_resume_fragment_container, patientResumeFragment);
fragmentTransaction.commit();
}
}
.
.
@Override
public void onItemSelected(Item item) {
itemResumeFragment.getView().findViewById(R.id.componentsContainer);
itemResumeFragment = new ResumeFragment(item);
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.item_resume_fragment_container, itemResumeFragment);
fragmentTransaction.commit();
}
简历片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.resume_fragment,container, false);
ComponentsLayout componentsContainer = (ComponentsLayout) fragmentView.findViewById(R.id.componentsContainer);
placeComponents(componentsContainer);
return fragmentView;
}
private void placeComponents(ComponentsLayout layout) {
List<ComponentConfig> configs = new ArrayList<ComponentConfig>(testMobile.getModel().getComponentConfiguration());
int viewId = 1;
for (ComponentConfig currConfig : configs) {
ComponentsLayout.LayoutParams params = new ComponentsLayout.LayoutParams(0, 0);
params.setWidthInCells(currConfig.getWidth());
params.setHeightInCells(currConfig.getHeight());
if (viewId == 1) {
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
} else {
if (currConfig.getPositionX() > 0) {
// looking for rightOf viewId
params.addRule(RelativeLayout.RIGHT_OF, getRightOfComponentId(configs, currConfig, viewId - 1));
}
if (currConfig.getPositionY() > 0) {
// looking for below viewId
params.addRule(RelativeLayout.BELOW, getBelowComponentId(configs, currConfig, viewId - 1));
}
}
try {
// this code will be changed in story 9
View view = currConfig.getClassType().newInstance().getView(getActivity());
view.setId(viewId);
view.setBackgroundColor(colors[(viewId - 1) % 6]);
layout.addView(view, params);
} catch (Exception e) {
// we should never get to this point as configuration is validated
Log.e(TAG, "getView: unable to create component for position " + viewId, e);
throw new RuntimeException(e);
}
viewId++;
}
}
和 ComponentLayout
public class ComponentsLayout extends RelativeLayout {
public ComponentsLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int horizontalCellsNum = getResources().getInteger(R.integer.resume_horizontal_cells_num);
int cellHeight = (int) (getResources().getDimension(R.dimen.resume_cell_height));
int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
LayoutParams params = (LayoutParams) child.getLayoutParams();
params.width = parentWidth * params.getWidthInCells() / horizontalCellsNum;
params.height = cellHeight * params.getHeightInCells();
child.setLayoutParams(params);
}
}
再一次,这在从 HomeScreenActivity onCreate 方法调用时有效,但在从 onItemSelected 方法调用时无效。
有人可以帮忙吗??