Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

First impl #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'de.hdodenhof:circleimageview:3.0.0'
}
127 changes: 126 additions & 1 deletion app/src/main/java/ru/ok/technopolis/students/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,139 @@
package ru.ok.technopolis.students;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends AppCompatActivity {

private List<Student> students;
private StudentAdapter studentAdapter;
private int pics[];
private Student currentStudent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
students = new ArrayList<>();
pics = new int[6];
currentStudent = null;
setContentView(R.layout.activity_main);
setupRecyclerView();
setupAddButton();
setupSaveButton();
setupDeleteButton();
setupPics();
}

private void setupRecyclerView() {
RecyclerView recyclerView = findViewById(R.id.activity_main__rv_students);
studentAdapter = new StudentAdapter(students, this::onStudentClick);
recyclerView.setAdapter(studentAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
}

private void setupPics() {
pics[0] = R.drawable.male_1;
pics[1] = R.drawable.male_2;
pics[2] = R.drawable.male_3;
pics[3] = R.drawable.female_1;
pics[4] = R.drawable.female_2;
pics[5] = R.drawable.female_3;
}

private void setupAddButton() {
Button button = findViewById(R.id.activity_main__btn_add);
button.setOnClickListener(v -> onAddClick());
}

private void onAddClick() {
setupEdit("", "", false, 0);
currentStudent = new Student("", "", false, 0);
}

private void setupEdit(String name, String familyName, Boolean isMale, int pic) {
EditText nameEdit = findViewById(R.id.names_layout__edit_name);
EditText familyNameEdit = findViewById(R.id.names_layout__edit_fname);
CheckBox male = findViewById(R.id.sex_layout__checkbox);
CircleImageView pics = findViewById(R.id.data_layout__edit_img);
nameEdit.setText(name);
familyNameEdit.setText(familyName);
male.setChecked(isMale);
pics.setImageResource(pic);
}

private void setupSaveButton() {
Button button = findViewById(R.id.cl_edit__save);
button.setOnClickListener(v -> onSaveClick());
}

private void setupDeleteButton() {
Button button = findViewById(R.id.cl_edit__delete);
button.setOnClickListener(v -> onDeleteClick());
}

private void onDeleteClick() {
if (currentStudent == null) {
Toast.makeText(this, R.string.empty_student, Toast.LENGTH_SHORT).show();
} else {
students.remove(currentStudent);
currentStudent = null;
studentAdapter.notifyDataSetChanged();
setupEdit("", "", Boolean.FALSE, 0);
}
}

private void onSaveClick() {
EditText name = findViewById(R.id.names_layout__edit_name);
EditText familyName = findViewById(R.id.names_layout__edit_fname);
CheckBox male = findViewById(R.id.sex_layout__checkbox);
CircleImageView pic = findViewById(R.id.data_layout__edit_img);
int picId;
if ((name.getText() != null) && (familyName.getText() != null)) {
if (TextUtils.isEmpty(name.getText().toString().trim())
|| TextUtils.isEmpty(familyName.getText().toString().trim())) {
Toast.makeText(this, R.string.wrong_enter, Toast.LENGTH_SHORT).show();
} else {
if (currentStudent == null || currentStudent.getFirstName().equals("")) {
students.add(currentStudent = new Student(name.getText().toString().trim(),
familyName.getText().toString().trim(),
male.isChecked(),
picId = getRandomPick(male.isChecked())));
pic.setImageResource(picId);
studentAdapter.notifyDataSetChanged();
} else {
currentStudent.setFirstName(name.getText().toString().trim());
currentStudent.setSecondName(familyName.getText().toString().trim());
currentStudent.setMaleGender(male.isChecked());
studentAdapter.notifyDataSetChanged();
}
}
}

}

private int getRandomPick(Boolean isMale) {
Random random = new Random();
int maleRandom = isMale ? 0 : 3;
return pics[maleRandom + random.nextInt(3)];
}

private void onStudentClick(Student student) {
setupEdit(student.getFirstName(), student.getSecondName(), student.isMaleGender(), student.getPhoto());
currentStudent = student;
}

}
75 changes: 75 additions & 0 deletions app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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.TextView;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.StudentViewHolder> {

private final List<Student> students;
private final Listener onStudentClickListener;

public StudentAdapter(List<Student> 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.student_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 holder, final int position) {
Student student = students.get(position);
holder.bind(student);
holder.itemView.setTag(student);
}

@Override
public int getItemCount() {
return students.size();
}

interface Listener {

void onStudentClick(Student student);

}

static final class StudentViewHolder extends RecyclerView.ViewHolder {

private final CircleImageView profileImage;
private final TextView nameTextView;
private final TextView familyNameTextView;

public StudentViewHolder(@NonNull View itemView) {
super(itemView);
profileImage = itemView.findViewById(R.id.student_item__img);
nameTextView = itemView.findViewById(R.id.student_item__name);
familyNameTextView = itemView.findViewById(R.id.student_item__fname);
}

private void bind(@NonNull Student student) {
nameTextView.setText(student.getFirstName());
familyNameTextView.setText(student.getSecondName());
profileImage.setImageResource(student.getPhoto());
}

}
}
130 changes: 130 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,137 @@
xmlns:tools="http://schemas.android.com/tools"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Очень большая вложенность. Не стоит вкладывать констрейнт в констрейнт

android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/activity_main__rv_students"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/activity_main__btn_add"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="40" />

<Button
android:id="@+id/activity_main__btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:text="@string/btn_add"
android:textSize="12sp"
app:layout_constraintBottom_toTopOf="@id/activity_main__sep_line"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/activity_main__rv_students"
app:layout_constraintVertical_weight="5" />

<View
android:id="@+id/activity_main__sep_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="5dp"
android:background="@android:color/darker_gray"
app:layout_constraintBottom_toTopOf="@id/cl_edit__title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/activity_main__btn_add"
app:layout_constraintVertical_weight="1" />

<TextView
android:id="@+id/cl_edit__title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/edit_title"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@id/data_layout__edit_img"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/activity_main__sep_line"
app:layout_constraintVertical_weight="5" />

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/data_layout__edit_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
app:layout_constraintBottom_toBottomOf="@id/names_layout__edit_fname"
app:layout_constraintEnd_toStartOf="@string/edit_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/names_layout__edit_name" />

<EditText
android:id="@+id/names_layout__edit_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_name"
android:textSize="15sp"
app:layout_constraintBottom_toTopOf="@id/names_layout__edit_fname"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/data_layout__edit_img"
app:layout_constraintTop_toBottomOf="@id/cl_edit__title"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintVertical_weight="5" />

<EditText
android:id="@+id/names_layout__edit_fname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_fname"
android:textSize="15sp"
app:layout_constraintBottom_toTopOf="@id/sex_layout__checkbox"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/data_layout__edit_img"
app:layout_constraintTop_toBottomOf="@id/names_layout__edit_name"
app:layout_constraintVertical_weight="5" />

<CheckBox
android:id="@+id/sex_layout__checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/cl_edit__delete"
app:layout_constraintEnd_toStartOf="@id/sex_layout__text"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toEndOf="@id/data_layout__edit_img"
app:layout_constraintTop_toBottomOf="@id/names_layout__edit_fname" />

<TextView
android:id="@+id/sex_layout__text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/edit_checkbox"
android:textSize="15sp"
app:layout_constraintBottom_toTopOf="@id/cl_edit__delete"
app:layout_constraintEnd_toEndOf="@id/names_layout__edit_fname"
app:layout_constraintStart_toEndOf="@id/sex_layout__checkbox"
app:layout_constraintTop_toBottomOf="@id/names_layout__edit_fname"
app:layout_constraintVertical_weight="5" />

<Button
android:id="@+id/cl_edit__delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/edit_delete"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/cl_edit__save"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/cl_edit__save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/edit_save"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/cl_edit__delete" />

</android.support.constraint.ConstraintLayout>
Loading