From f04c57b8e8e74b7874069dc6b367757eae21a9be Mon Sep 17 00:00:00 2001 From: Yaroslav Erokhin Date: Thu, 28 Feb 2019 18:55:27 +0300 Subject: [PATCH 1/7] Students 1.0 --- .../ok/technopolis/students/MainActivity.java | 144 +++++++++++++++++- .../technopolis/students/StudentAdapter.java | 73 +++++++++ app/src/main/res/layout/activity_main.xml | 119 +++++++++++++++ app/src/main/res/layout/student_item.xml | 6 + app/src/main/res/values-ru/strings.xml | 2 + app/src/main/res/values/colors.xml | 3 + app/src/main/res/values/strings.xml | 9 ++ 7 files changed, 354 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java create mode 100644 app/src/main/res/layout/student_item.xml create mode 100644 app/src/main/res/values-ru/strings.xml 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..5002115 100644 --- a/app/src/main/java/ru/ok/technopolis/students/MainActivity.java +++ b/app/src/main/java/ru/ok/technopolis/students/MainActivity.java @@ -1,14 +1,154 @@ package ru.ok.technopolis.students; -import android.support.v7.app.AppCompatActivity; +import android.app.Activity; 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.CheckBox; +import android.widget.EditText; +import android.widget.ImageView; -public class MainActivity extends AppCompatActivity { +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class MainActivity extends Activity { + + private List students; + private StudentAdapter studentAdapter; + private Student currentStudent; + + private final String DEFAULTFIRSTNAME = "Firstname"; + private final String DEFAULTSECONDNAME = "Secondname"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + RecyclerView recyclerView = findViewById(R.id.list); + students = new ArrayList<>(); + studentAdapter = new StudentAdapter(students, new StudentAdapter.Listener() { + @Override + public void onStudentClick(Student student) { + EditText firstName = findViewById(R.id.first_name_info); + firstName.setText(student.getFirstName()); + EditText secondName = findViewById(R.id.second_name_info); + secondName.setText(student.getSecondName()); + ImageView photo = findViewById(R.id.big_photo); + photo.setImageResource(student.getPhoto()); + CheckBox checkBox = findViewById(R.id.checkbox); + checkBox.setChecked(student.isMaleGender()); + currentStudent = student; + } + }); + recyclerView.setAdapter(studentAdapter); + LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); + recyclerView.setLayoutManager(linearLayoutManager); + setupAddButton(); + setupDeleteButton(); + setupSaveButton(); + } + + private void setupAddButton() { + Button addButton = findViewById(R.id.add_button); + addButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Random random = new Random(); + boolean maleGender = random.nextBoolean(); + int startPhoto = 0; + if (maleGender) { + switch (random.nextInt(3)) { + case 0: startPhoto = R.drawable.male_1; + break; + case 1: startPhoto = R.drawable.male_2; + break; + case 2: startPhoto = R.drawable.male_3; + break; + } + } else { + switch (random.nextInt(3)) { + case 0: startPhoto = R.drawable.female_1; + break; + case 1: startPhoto = R.drawable.female_2; + break; + case 2: startPhoto = R.drawable.female_3; + break; + } + } + students.add(new Student(DEFAULTFIRSTNAME, DEFAULTSECONDNAME, maleGender, startPhoto)); + studentAdapter.notifyDataSetChanged(); + } + }); + } + + private void setupDeleteButton() { + Button deleteButton = findViewById(R.id.delete_button); + deleteButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + int size = students.size(); + if (size != 0) { + if (size > 1) { + int index = students.indexOf(currentStudent); + if (index == 0) { + ++index; + } else { + if (index == size - 1) { + --index; + } else { + ++index; + } + } + Student tempStd = currentStudent; + currentStudent = students.get(index); + students.remove(tempStd); + EditText firstName = findViewById(R.id.first_name_info); + firstName.setText(currentStudent.getFirstName()); + EditText secondName = findViewById(R.id.second_name_info); + secondName.setText(currentStudent.getSecondName()); + ImageView photo = findViewById(R.id.big_photo); + photo.setImageResource(currentStudent.getPhoto()); + CheckBox checkBox = findViewById(R.id.checkbox); + checkBox.setChecked(currentStudent.isMaleGender()); + } else { + students.remove(currentStudent); + currentStudent = null; + EditText firstName = findViewById(R.id.first_name_info); + firstName.setText(DEFAULTFIRSTNAME); + EditText secondName = findViewById(R.id.second_name_info); + secondName.setText(DEFAULTSECONDNAME); + ImageView photo = findViewById(R.id.big_photo); + photo.setImageResource(0); + CheckBox checkBox = findViewById(R.id.checkbox); + checkBox.setChecked(false); + } + } + studentAdapter.notifyDataSetChanged(); + } + }); + } + + private void setupSaveButton() { + Button saveButton = findViewById(R.id.save_button); + saveButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (currentStudent != null) { + int index = students.indexOf(currentStudent); + EditText firstNameInfo = findViewById(R.id.first_name_info); + currentStudent.setFirstName(firstNameInfo.getText().toString()); + EditText secondInfo = findViewById(R.id.second_name_info); + currentStudent.setSecondName(secondInfo.getText().toString()); + CheckBox checkBox = findViewById(R.id.checkbox); + currentStudent.setMaleGender(checkBox.isChecked()); + students.set(index, currentStudent); + studentAdapter.notifyDataSetChanged(); + } + } + }); } } 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..4ac36a0 --- /dev/null +++ b/app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java @@ -0,0 +1,73 @@ +package ru.ok.technopolis.students; + +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.TextView; + +import java.util.List; + +public class StudentAdapter extends RecyclerView.Adapter { + + private final List students; + private final Listener listener; + + public StudentAdapter(List list, Listener listener) { + this.students = list; + this.listener = listener; + } + + @NonNull + @Override + public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { + View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.student_item, viewGroup, false); + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + listener.onStudentClick((Student)v.getTag()); + } + }); + return new StudentViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull StudentViewHolder holder, int position) { + Student student = students.get(position); + holder.bind(student); + holder.itemView.setTag(student); + } + + @Override + public int getItemCount() { + return students.size(); + } + + static final class StudentViewHolder extends RecyclerView.ViewHolder{ + private final TextView firstNameTextView; + private final TextView secondNameTextView; + private final ImageView photoTextView; + + + public StudentViewHolder(@NonNull View itemView) { + super(itemView); + firstNameTextView = itemView.findViewById(R.id.firstname); + secondNameTextView = itemView.findViewById(R.id.secondname); + photoTextView = itemView.findViewById(R.id.small_photo); + } + + private void bind(Student student) { + firstNameTextView.setText(student.getFirstName()); + secondNameTextView.setText(student.getSecondName()); + photoTextView.setImageResource(student.getPhoto()); + } + + } + + interface Listener { + void onStudentClick(Student student); + } + +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 304e72d..a8a887a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,9 +1,128 @@ + + + + +