From 518b6a18917469c9c56e5af6b796b087caf8de0d Mon Sep 17 00:00:00 2001 From: EgoriusE Date: Fri, 1 Mar 2019 02:00:37 +0300 Subject: [PATCH 1/8] not full functionality --- .../ok/technopolis/students/MainActivity.java | 130 +++++++++++++++- .../ru/ok/technopolis/students/Student.java | 6 +- .../technopolis/students/StudentAdapter.java | 69 ++++++++ .../recycler_view_item_circle_icon.xml | 8 + app/src/main/res/layout/activity_main.xml | 147 +++++++++++++++++- app/src/main/res/layout/bottom_view.xml | 117 ++++++++++++++ .../main/res/layout/recycler_view_item.xml | 34 ++++ app/src/main/res/values/strings.xml | 8 + 8 files changed, 513 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java create mode 100644 app/src/main/res/drawable/recycler_view_item_circle_icon.xml create mode 100644 app/src/main/res/layout/bottom_view.xml create mode 100644 app/src/main/res/layout/recycler_view_item.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..0cdaf28 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,140 @@ package ru.ok.technopolis.students; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.design.widget.FloatingActionButton; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.KeyEvent; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.RadioButton; +import android.widget.RadioGroup; +import android.widget.TextView; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Random; public class MainActivity extends AppCompatActivity { + private Random random = new Random(2); + private int randomIcon; + private boolean sexOfStudent; + private Student currentStudent; + + List students; + TextView text; + EditText firstName; + EditText secondName; + ImageView icon; + FloatingActionButton add; + View bottomView; + RadioGroup sex; + RadioButton male; + RadioButton female; + Button delete; + Button save; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + sex = findViewById(R.id.activity_main_sex); + male = findViewById(R.id.activity_main_sex_male); + female = findViewById(R.id.activity_main_sex_female); + add = findViewById(R.id.activity_main_fb_add); + text = findViewById(R.id.recycler_view_item_name); + firstName = findViewById(R.id.activity_main_first_name); + secondName = findViewById(R.id.activity_main_second_name); + icon = findViewById(R.id.activity_main_icon); + bottomView = findViewById(R.id.activity_main_bottom_view); + delete = findViewById(R.id.activity_main_delete); + save = findViewById(R.id.activity_main_save); + RecyclerView recyclerView = findViewById(R.id.activity_main_recycler_view); + + StudentAdapter studentAdapter = new StudentAdapter(generateStudentList(), + new StudentAdapter.Listener() { + @Override + public void onStudentClick(Student student) { + currentStudent = student; + firstName.setText(student.getFirstName()); + secondName.setText(student.getSecondName()); + icon.setImageResource(student.getPhoto()); + add.hide(); + bottomView.setVisibility(View.VISIBLE); + + if (student.isMaleGender()) + male.toggle(); + else female.toggle(); + } + }); + + recyclerView.setAdapter(studentAdapter); + LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); + recyclerView.setLayoutManager(linearLayoutManager); + + add.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + add.hide(); + bottomView.setVisibility(View.VISIBLE); + + firstName.setText(""); + secondName.setText(""); + sex.clearCheck(); + icon.setVisibility(View.GONE); + } + }); + + delete.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + students.remove(currentStudent); + bottomView.setVisibility(View.GONE); + add.show(); + + } + }); + + save.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Student student = new Student(firstName.getText().toString(), + secondName.getText().toString(), + sexOfStudent, + randomIcon); + students.add(student); + + bottomView.setVisibility(View.GONE); + add.show(); + } + }); + + sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(RadioGroup group, int checkedId) { + switch (checkedId) { + case R.id.activity_main_sex_male: + sexOfStudent = true; + randomIcon = R.drawable.male_3; + break; + case R.id.activity_main_sex_female: + sexOfStudent = false; + randomIcon = R.drawable.female_3; + } + } + }); } -} + private List generateStudentList() { + students = new LinkedList<>(); + students.add(new Student("Вася", "Пупкин", true, R.drawable.male_1)); + students.add(new Student("Инна", "Вупкина", false, R.drawable.female_1)); + students.add(new Student("Лариса", "Купкина", false, R.drawable.female_2)); + return students; + } + } 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..e294bc6 100644 --- a/app/src/main/java/ru/ok/technopolis/students/Student.java +++ b/app/src/main/java/ru/ok/technopolis/students/Student.java @@ -7,14 +7,14 @@ public class Student { private boolean maleGender; private int photo; - public Student(String firstName, String secondName, boolean maleGender, int photo) { + Student(String firstName, String secondName, boolean maleGender, int photo) { this.firstName = firstName; this.secondName = secondName; this.maleGender = maleGender; this.photo = photo; } - public String getFirstName() { + String getFirstName() { return firstName; } @@ -38,7 +38,7 @@ public void setMaleGender(boolean maleGender) { this.maleGender = maleGender; } - public int getPhoto() { + int getPhoto() { return 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..c94b0e7 --- /dev/null +++ b/app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java @@ -0,0 +1,69 @@ +package ru.ok.technopolis.students; + +import android.support.annotation.NonNull; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; +import android.support.v7.widget.RecyclerView; + +import java.util.List; + +public class StudentAdapter extends RecyclerView.Adapter { + private final Listener onStudentClickListener; + private final List students; + + StudentAdapter(List students, Listener onStudentClickListener) { + this.students = students; + this.onStudentClickListener = onStudentClickListener; + } + + @NonNull + @Override + public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { + View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view_item, + viewGroup, false); + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + onStudentClickListener.onStudentClick((Student) v.getTag()); + } + }); + return new StudentViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull StudentViewHolder viewHolder, int i) { + Student student = students.get(i); + viewHolder.bind(student); + viewHolder.itemView.setTag(student); + } + + @Override + public int getItemCount() { + return students.size(); + } + + static final class StudentViewHolder extends RecyclerView.ViewHolder { + + private final TextView nameTextView; + private final ImageView iconImageView; + + StudentViewHolder(@NonNull View itemView) { + super(itemView); + nameTextView = itemView.findViewById(R.id.recycler_view_item_name); + iconImageView = itemView.findViewById(R.id.recycler_view_item_icon); + } + + private void bind(@NonNull Student student) { + nameTextView.setText(String.format("%s %s", student.getFirstName(), student.getSecondName())); + iconImageView.setImageResource(student.getPhoto()); + } + } + + interface Listener { + void onStudentClick(Student student); + } + +} \ No newline at end of file diff --git a/app/src/main/res/drawable/recycler_view_item_circle_icon.xml b/app/src/main/res/drawable/recycler_view_item_circle_icon.xml new file mode 100644 index 0000000..edf3ed5 --- /dev/null +++ b/app/src/main/res/drawable/recycler_view_item_circle_icon.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 304e72d..9ac7556 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,9 +1,154 @@ + android:orientation="vertical"> + + + + + + + + + + + + + + + + + + + + + + + + + + +