У меня есть два ряда ImageButton
с. После нажатия на любой из первого ряда я хочу удалить первый ряд, а второй - с центром в ImageButton
. Это должно выглядеть примерно так: «https://lifehacker.ru/special/travel-checklist/».
Макет первого ряда:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbbbbb"
android:orientation="horizontal">
<ImageButton
android:id="@+id/winter"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="Winter"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="@drawable/snejinka"
android:text="Зима"
android:textColor="#FFFFFF" />
<ImageButton
android:id="@+id/Middle"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="Middle"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="@drawable/listok"
android:text="Весна/Осень"
android:textColor="#FFFFFF" />
<ImageButton
android:id="@+id/Summer"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="Summer"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="@drawable/unnamed"
android:text="Лето"
android:textColor="#FFFFFF" />
</LinearLayout>
Макет второго ряда:
<ImageButton
android:id="@+id/three"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="three"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="@drawable/three"
android:text="three"
android:textColor="#FFFFFF" />
<ImageButton
android:id="@+id/seven"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="seven"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="@drawable/seven"
android:text="three"
android:textColor="#FFFFFF" />
<ImageButton
android:id="@+id/fourteen"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="fourteen"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="@drawable/fourteen"
android:text="three"
android:textColor="#FFFFFF" />
А вот и моя активность для обеих строк. Я не знаю, как заставить второй ряд появляться после первого клика (второй ряд под комментариями)
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import static com.example.samsunghomework.R.id.*;
public class MainActivity extends AppCompatActivity {
ImageButton winter;
ImageButton middle;
ImageButton summer;
LinearLayout mainlayout;
/*
ImageButton three;
ImageButton seven;
ImageButton fourteen;
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
winter = (ImageButton) findViewById(R.id.winter);
middle = (ImageButton) findViewById(Middle);
summer = (ImageButton) findViewById(Summer);
mainlayout = (LinearLayout) findViewById(R.id.mainlayout);
/*
three = (ImageButton) findViewById(R.id.three);
seven = (ImageButton) findViewById(R.id.seven);
fourteen = (ImageButton) findViewById(R.id.fourteen);
*/
}
int galka = 0;
int curr = 0;
public void Winter(View view) {
// mainlayout.removeAllViews();
if (galka == 0) winter.setImageResource(R.drawable.galka);
else if (curr == 2) {
middle.setImageResource(R.drawable.listok);
winter.setImageResource(R.drawable.galka);
}
if (curr == 3) {
summer.setImageResource(R.drawable.unnamed);
winter.setImageResource(R.drawable.galka);
}
galka = 1;
curr = 1;
// mainlayout.addView(new ImageButton(this));
ImageButton imageButton = new ImageButton(MainActivity.this);
imageButton.setImageResource(R.drawable.galka);
LinearLayout.LayoutParams imageViewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
imageButton.setLayoutParams(imageViewLayoutParams);
mainlayout.addView(imageButton);
}
public void Middle(View view) {
if (galka == 0) middle.setImageResource(R.drawable.galka);
else if (curr == 1) {
winter.setImageResource(R.drawable.snejinka);
middle.setImageResource(R.drawable.galka);
}
if (curr == 3) {
summer.setImageResource(R.drawable.unnamed);
middle.setImageResource(R.drawable.galka);
}
galka = 1;
curr = 2;
}
public void Summer(View view) {
if (galka == 0) summer.setImageResource(R.drawable.galka);
else if (curr == 1) {
winter.setImageResource(R.drawable.snejinka);
summer.setImageResource(R.drawable.galka);
}
if (curr == 2) {
middle.setImageResource(R.drawable.listok);
summer.setImageResource(R.drawable.galka);
}
galka = 1;
curr = 3;
}
int galka2 = 0;
int curr2 = 0;
/*
public void three(View view) {
if (galka2 == 0) three.setImageResource(R.drawable.galka);
else if (curr2 == 2) {
seven.setImageResource(R.drawable.seven);
three.setImageResource(R.drawable.galka);
}
if (curr2 == 3) {
fourteen.setImageResource(R.drawable.fourteen);
three.setImageResource(R.drawable.galka);
}
galka2 = 1;
curr2 = 1;
}
public void seven(View view) {
if (galka2 == 0) seven.setImageResource(R.drawable.galka);
else if (curr2 == 1) {
three.setImageResource(R.drawable.three);
seven.setImageResource(R.drawable.galka);
}
if (curr2 == 3) {
fourteen.setImageResource(R.drawable.fourteen);
seven.setImageResource(R.drawable.galka);
}
galka2 = 1;
curr2 = 2;
}
public void fourteen(View view) {
if (galka2 == 0) fourteen.setImageResource(R.drawable.galka);
else if (curr2 == 1) {
three.setImageResource(R.drawable.three);
fourteen.setImageResource(R.drawable.galka);
}
if (curr2 == 2) {
seven.setImageResource(R.drawable.seven);
fourteen.setImageResource(R.drawable.galka);
}
galka2 = 1;
curr2 = 3;
}
*/
}