2012-05-10 7 views
1

私はRelativeLayoutを継承する他の化合物コントロール(BaseControl)のベースとして機能するカスタムコントロールを持っています。 このBaseControlは、各コントロールのXMLを簡単に膨張させ、後で初期化することを目的としています。 、私はBaseControlを抽象化する前に、私は持っていなかった問題を化合物コントロールを動的にインスタンス化して追加するときにonFinishInflateが呼び出されない

abstract class BaseControl extends RelativeLayout { 

    public BaseControl(Context context) { 
     super(context); 
     this.baseInitializeControl(context, null); 
    } 

    public BaseControl(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.baseInitializeControl(context, attrs); 
    } 

    public BaseControl(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     this.baseInitializeControl(context, attrs); 
    } 

    private void baseInitializeControl(Context context, AttributeSet attrs) { 
     int layoutId = this.getControlLayoutId(); 
     if(layoutId != 0) { 
      LayoutInflater inflater = LayoutInflater.from(getContext()); 
      inflater.inflate(layoutId, this); 
     } 

     this.initializeControl(context, attrs); 
    } 

    protected abstract int getControlLayoutId(); 
    protected abstract void initializeControl(Context context, AttributeSet attrs); 
} 

実際の問題は、私は、コントロールの内部リストビューにOnItemClickedListenerを設定することであるが、それは任意のクリックに応答しない:すべては、コンストラクタで行われますクラス。私はonFinishInflateでListViewのsetOnItemClickedListenerメソッドを呼び出すことでこの問題を解決しました。

私はCustomList1とCustomList2を持っているので、両方ともBaseControlを継承します。 CustomList1オン は私が持っている:

@Override 
protected int getControlLayoutId() { 
    return R.layout.custom_list_1; 
} 

@Override 
protected void initializeControl(Context context, AttributeSet attrs) { 
    this.mListView = this.findTypedViewById(R.id.listView); 

    CustomAdapter adapter = new CustomAdapter(getContext()); 
    this.mListView.setAdapter(adapter); 
} 

@Override 
protected void onFinishInflate() { 
    super.onFinishInflate(); 

    this.mListView.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) { 
      HistoryGroupList.this.onListItemClicked(parentView, childView, position, id); 
     } 
    }); 
} 

private void onListItemClicked(AdapterView<?> parentView, View childView, int position, long id) { 
    CustomAdapter adapter = (CustomAdapter) this.mListView.getAdapter(); 
    CustomData data = adapter.getItem(position); 

    CustomList2 list = new CustomList2(getContext(), data); // CustomList2 should query more data here. 

    this.addView(list); // The new View is added in front of the current one 
} 

これまでのところは良いアイテムをクリックし、最初のCustomListは、応答(その中の2番目のリストビューで)新しいコントロールを作成し、前でそれを示しています。

2番目のコントロール(CustomList2)は同じコードで、getControlLayoutId()で異なるIDのみを返し、onListItemClicked(...)でトーストを表示し、コンストラクターの2番目のパラメーターを使用して別のアダプタ。 問題は、この2番目のコントロールがonFinishInflate()イベントを実行していないことです。ただし、BaseControlでxmlを膨張させています。 このコントロールをXMLタグとして別のレイアウトに追加すると、onFinishInflate()メソッドが正常に実行されます。

このメソッドが実行されていない理由は分かりますか? setOnItemClickedListenerメソッドを使用して問題を解決するための回避策があります。

ありがとうございます! Mike

答えて

0

リスナーの作成後にsuper.onFinishInflate()を呼び出してみてください。奇妙に思えますが、私は過去の経験から気付きました。時にはスーパーコール後にメソッドが終了することがあります。

@Override 
protected void onFinishInflate() { 
    this.mListView.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) { 
      HistoryGroupList.this.onListItemClicked(parentView, childView, position, id); 
     } 
    }); 

    super.onFinishInflate();  
} 
-1

同様の問題がちょうどInflateの呼び出し後にsetOnItemClickListenerを置く(私のために正常に動作します)。

関連する問題