У меня около 9 LinearLayout в моем проекте, и я хочу, чтобы только один LinearLayout показывал его содержимое. Я сделал это, сохранив идентификатор LinearLayout, который отображает его содержимое, в переменной int (рассмотрим int currentLayout
). Проблема в том, что когда я скрываю макет и отображаю другой, я назначаю его глобальной переменной
, код выглядит примерно так:
view
находится в OnCreateView(Layout Inflator inflator, .....) {}
view = inflater.inflate(R.layout.some_fragment, container, false);
Представление переменной объявлено глобально Это в случае переключения
@Override
public void OnClick(View v) {
...
}
int currentLayout = R.id.linear_layout_1; // This layout is showing its content
int currentBtn = R.id.btn_1; // Button to display the current layout
//Hide currently open layout (layout_1)
view.findViewById(currentLayout).setVisibility(View.GONE);
view.findViewById(currentBtn).setRotation(0); // Everything works fine till here
// Show layout_2 (The next 2 lines of codes dont work)
view.findViewById(R.id.linear_layout_2).setVisibility(View.VISIBLE);
view.findViewById(R.id.btn2).setRotation(180);
// Save currenty open layout n its corresponding button to a variable (These two below also works)
currentLayout = R.id.linear_layout_2
currentBtn = R.id.btn_2
Я понятия не имею, почему это не работает. Я изменил свой подход к выполнению этого проекта, но все же я действительно хочу знать, почему это не работает.