2012-03-08 9 views
0

私は2つの部分からなるアクティビティ(HomeScreenActivityと呼ばれる)を実装しました。そのうちの1つはアイテムのリストを表示し、もう1つは選択されたアイテムの詳細を表示します。詳細フラグメント(私のコードのResumeFragment)は、一連のコンポーネントで構成されています。構成要素の高さと幅は、構成ファイルのセル数として指定することでユーザーが設定できます。これらのコンポーネントは、ComponentsLayoutというカスタムレイアウトに追加されます。Androidのカスタムレイアウトの子供たちが測定した高さと幅を測定しない

HomeScreenActivity iを開くと、onCreateメソッドで各フラグメントが表示され、意図したとおりに表示されます。 しかし、アイテムリストのフラグメントをクリックすると、同じクラスの新しいインスタンスでresumeFragmentが置き換えられますが、何も表示されません。

私はデバッグに多くの時間を費やしています。resumeFragmentが与えられた高さと幅を記入していますが、componentsLayoutの高さは1です。これは高さとcomponentsLayout onMeasureメソッドでlayoutParamsを設定していても、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(); 
} 

ResumeFragment

@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、ResumeFragmentとComponentsLayoutのコードですHomeScreenActivity onCreateメソッドから呼び出されたときは、wh onItemSelectedメソッドから行われます。

誰でも手伝いできますか?

+0

btw私はonMeasureがResumeFragmentが初めてインスタンス化されたとき(onCreateから)、そしてリスト内の項目を変更するときにonMeasureが3回呼び出されることに気づいたことを忘れていました。これが何か言いたいことがあれば私は知らない。 –

+0

私は、onMeasureがcomponentLayoutで呼び出される前に、すべてのレイアウトパラメータをコンポーネントに追加することで回避策を作成しました。それは私が探していた解決策ではありませんでしたが、それはかなり単純でした。おそらく私はそれを見落としました。 –

答えて

0

onMeasureがcomponentLayoutで呼び出される前に、すべてのレイアウトパラメータをコンポーネントに追加することで回避策を作成しました。それは私が探していた解決策ではありませんでしたが、それはかなり単純でした。おそらく私はそれを見落としました。

関連する問題