diff --git a/app/build.gradle b/app/build.gradle index 3710062..ebf6a9b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -22,6 +22,7 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' + implementation 'com.android.support:recyclerview-v7:28.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' diff --git a/app/src/main/java/ru/ok/technopolis/students/MainActivity.java b/app/src/main/java/ru/ok/technopolis/students/MainActivity.java index adde500..a9af2b1 100644 --- a/app/src/main/java/ru/ok/technopolis/students/MainActivity.java +++ b/app/src/main/java/ru/ok/technopolis/students/MainActivity.java @@ -2,13 +2,143 @@ import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.RadioGroup; +import android.widget.Toast; + +import java.util.Random; public class MainActivity extends AppCompatActivity { + private StudentAdapter adapter; + private ImageView photo; + private int photoId; + private EditText firstNameField; + private EditText lastNameField; + private RadioGroup radioGroup; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + RecyclerView recyclerView = findViewById(R.id.activity_main__list); + adapter = new StudentAdapter(this); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + recyclerView.setAdapter(adapter); + + photo = findViewById(R.id.activity_main__photo); + firstNameField = findViewById(R.id.activity_main__firstName); + lastNameField = findViewById(R.id.activity_main__lastName); + radioGroup = findViewById(R.id.activity_main__radio); + + radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(RadioGroup group, int checkedId) { + randomizePhoto(); + } + }); + + Button addButton = findViewById(R.id.activity_main__add); + addButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + clearFields(); + adapter.setActiveStudent(StudentAdapter.NO_STUDENT); + } + }); + + Button saveButton = findViewById(R.id.activity_main__save); + saveButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + saveStudent(); + } + }); + + Button deleteButton = findViewById(R.id.activity_main__delete); + deleteButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + deleteStudent(); + } + }); + } + + private void deleteStudent() { + adapter.deleteStudent(); + } + + private void saveStudent() { + String firstNameStr = firstNameField.getText() == null ? "" : firstNameField.getText().toString(); + String lastNameStr = lastNameField.getText() == null ? "" : lastNameField.getText().toString(); + Boolean gender = null; + switch (radioGroup.getCheckedRadioButtonId()) { + case R.id.activity_main__radio_male: + gender = true; + break; + case R.id.activity_main__radio_woman: + gender = false; + break; + } + + if (firstNameStr.isEmpty() || lastNameStr.isEmpty() || gender == null) { + Toast.makeText(this, "Заполните все поля", Toast.LENGTH_SHORT).show(); + return; + } + + Student student = new Student( + firstNameStr, + lastNameStr, + gender, + photoId + ); + adapter.saveStudent(student); + } + + private void randomizePhoto() { + int[] imgMale = {R.drawable.male_1, R.drawable.male_2, R.drawable.male_3}; + int[] imgFemale = {R.drawable.female_1, R.drawable.female_2, R.drawable.female_3}; + + int i; + switch (radioGroup.getCheckedRadioButtonId()) { + case R.id.activity_main__radio_male: + i = new Random().nextInt(3); + photo.setImageDrawable(getResources().getDrawable(imgMale[i])); + photoId = imgMale[i]; + break; + case R.id.activity_main__radio_woman: + i = new Random().nextInt(3); + photo.setImageDrawable(getResources().getDrawable(imgFemale[i])); + photoId = imgFemale[i]; + break; + default: + photoId = imgMale[0]; + break; + } + } + + public void clearFields() { + photo.setImageDrawable(null); + firstNameField.setText(""); + lastNameField.setText(""); + radioGroup.clearCheck(); + } + + public void setActiveStudent(Student student) { + photo.setImageDrawable(getResources().getDrawable(student.getPhoto())); + firstNameField.setText(student.getFirstName()); + lastNameField.setText(student.getSecondName()); + if (student.isMale()) { + radioGroup.check(R.id.activity_main__radio_male); + } else { + radioGroup.check(R.id.activity_main__radio_woman); + } } } diff --git a/app/src/main/java/ru/ok/technopolis/students/Student.java b/app/src/main/java/ru/ok/technopolis/students/Student.java index 7be4d10..73c5b61 100644 --- a/app/src/main/java/ru/ok/technopolis/students/Student.java +++ b/app/src/main/java/ru/ok/technopolis/students/Student.java @@ -2,18 +2,27 @@ public class Student { + private Boolean selected; private String firstName; private String secondName; - private boolean maleGender; + private boolean male; private int photo; public Student(String firstName, String secondName, boolean maleGender, int photo) { this.firstName = firstName; this.secondName = secondName; - this.maleGender = maleGender; + this.male = maleGender; this.photo = photo; } + public Boolean isSelected() { + return selected; + } + + public void setSelected(Boolean selected) { + this.selected = selected; + } + public String getFirstName() { return firstName; } @@ -30,12 +39,12 @@ public void setSecondName(String secondName) { this.secondName = secondName; } - public boolean isMaleGender() { - return maleGender; + public boolean isMale() { + return male; } - public void setMaleGender(boolean maleGender) { - this.maleGender = maleGender; + public void setMale(boolean male) { + this.male = male; } public int getPhoto() { @@ -45,4 +54,12 @@ public int getPhoto() { public void setPhoto(int photo) { this.photo = photo; } + + public void copy(Student student) { + this.firstName = new String(student.firstName); + this.secondName = new String(student.secondName); + this.male = new Boolean(student.male); + this.photo = new Integer(student.photo); + } + } diff --git a/app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java b/app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java new file mode 100644 index 0000000..c01fe66 --- /dev/null +++ b/app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java @@ -0,0 +1,116 @@ +package ru.ok.technopolis.students; + +import android.content.Context; +import android.graphics.Color; +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; + +import java.util.ArrayList; +import java.util.List; + +public class StudentAdapter extends RecyclerView.Adapter { + + public static final int NO_STUDENT = -1; + private MainActivity context; + private List students = new ArrayList<>(); + private int activeStudent = NO_STUDENT; + + public StudentAdapter(Context context) { + this.context = (MainActivity) context; + } + + public void saveStudent(Student student) { + if (activeStudent == NO_STUDENT) { + students.add(student); + activeStudent = students.size() - 1; + student.setSelected(true); + notifyItemInserted(activeStudent); + } else { + Student oldStudent = students.get(activeStudent); + oldStudent.copy(student); + notifyItemChanged(activeStudent); + } + } + + public void deleteStudent() { + if (activeStudent != NO_STUDENT) { + students.remove(activeStudent); + notifyItemRemoved(activeStudent); + activeStudent = NO_STUDENT; + } + context.clearFields(); + } + + public void setActiveStudent(int activeStudent) { + if (this.activeStudent != NO_STUDENT){ + Student student = students.get(this.activeStudent); + student.setSelected(false); + } + this.activeStudent = activeStudent; + notifyDataSetChanged(); + } + + @NonNull + @Override + public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { + View view = LayoutInflater + .from(viewGroup.getContext()) + .inflate(R.layout.recycler_item, viewGroup, false); + return new StudentViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull StudentViewHolder studentViewHolder, int i) { + studentViewHolder.bind(students.get(i)); + } + + @Override + public int getItemCount() { + return students.size(); + } + + public class StudentViewHolder extends RecyclerView.ViewHolder { + private ImageView face; + private TextView fullName; + private LinearLayout mainLayout; + + public StudentViewHolder(@NonNull View itemView) { + super(itemView); + + mainLayout = itemView.findViewById(R.id.recycler_item__main); + face = itemView.findViewById(R.id.recycler_item__image); + fullName = itemView.findViewById(R.id.recycler_item__text); + + itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (activeStudent != NO_STUDENT) { + Student oldStudent = students.get(activeStudent); + oldStudent.setSelected(false); + } + activeStudent = getAdapterPosition(); + Student student = students.get(getAdapterPosition()); + student.setSelected(true); + context.setActiveStudent(student); + notifyDataSetChanged(); + } + }); + } + + private void bind(Student student) { + if (student.isSelected()) + mainLayout.setBackgroundColor(Color.BLUE); + else + mainLayout.setBackgroundColor(Color.DKGRAY); + face.setImageDrawable(context.getResources().getDrawable(student.getPhoto())); + fullName.setText(student.getFirstName() + " " + student.getSecondName()); + } + } + +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 304e72d..39268d9 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,9 +1,138 @@ - + - \ No newline at end of file + + +