レイアウト xml に次の 2 つの LinearLayout があります。
レイアウト項目layout_editとlayout_createの両方にonClickListenerを追加しました
<LinearLayout
android:id="@+id/layout_edit"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@color/white"
android:gravity="center"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:visibility="visible" >
<ImageView
android:id="@+id/imgView_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_edit" >
</ImageView>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_create"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_toLeftOf="@id/layout_edit"
android:background="@color/white"
android:gravity="center"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:visibility="visible" >
<ImageView
android:id="@+id/imgView_create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_create" >
</ImageView>
</LinearLayout>
次のコードを追加しました。
LinearLayout linearLayoutEdit = (LinearLayout) getActivity().findViewById(R.id.layout_edit);
mLinearLayoutEdit.setOnClickListener(this);
LinearLayout linearLayoutCreate = (LinearLayout) getActivity().findViewById(R.id.layout_create);
linearLayoutCreate.setOnClickListener(this);
特定のシナリオでは、次のコードが追加されます。
linearLayoutEdit.setVisibility(View.INVISIBLE);
現在、linearLayoutCreate は引き続き表示されますが、linearLayoutCreate onClick は機能しません。
linearLayoutEdit と linearLayoutCreate の両方が VISIBLE の場合、両方の onClick が正常に機能します。
linearLayoutCreate は linearLayoutEdit の左に配置され、linearLayoutEdit はコードを使用して INVISIBLE になっているため、linearLayoutCreate のクリック可能なアクションは表示されていても機能していないようです。
linearLayoutEdit が INVISIBLE のときに linearLayoutCreate をクリック可能にする方法に関するヒント。
私が見つけた解決策の 1 つは、既に利用可能な ID を持つコードを使用してレイアウトを再作成することですが、他に利用可能な解決策はありますか?
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.layout_edit:
break;
case R.id.layout_create:
// not navigating within this condition only when layout_edit is INVISIBLE, but //navigating when both layout_edit & layout_create are VISIBLE
break;
default:
break;
}
}