This repository has been archived by the owner on Mar 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Version 1.0 Loginov #1
Open
l0gark
wants to merge
9
commits into
polis-vk:master
Choose a base branch
from
l0gark:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d8943c8
Students_Homework Логинов Аркадий
l0gark ee11d43
Update Student.java
l0gark bed07f7
Bitmap не тормозит, добавил редактирование
l0gark 323129c
remove .idea
l0gark bb0076e
Исправил замечания.
l0gark 14800fd
Исправил замечания.
l0gark 4145fc0
Исправил. Так и не понимаю как мне без контекста этому литсенеру пере…
l0gark f10ec72
убрал лишние импорты
l0gark f0f59dd
поставил setter для листенеров
l0gark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
app/src/main/java/ru/ok/technopolis/students/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,152 @@ | ||
package ru.ok.technopolis.students; | ||
|
||
import android.app.AlertDialog; | ||
import android.content.DialogInterface; | ||
import android.support.design.widget.FloatingActionButton; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.support.v7.widget.GridLayoutManager; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.View; | ||
import android.widget.ImageView; | ||
import android.widget.LinearLayout; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Random; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
private static final String DIALOG_TAG = "DialogAdder"; | ||
private String[] mansFirstNames; | ||
private String[] womenFirstNames; | ||
private String[] lastNames; | ||
private int[] manPhotos; | ||
private int[] womanPhotos; | ||
private Random random; | ||
private int manPhotoIndex, womanPhotoIndex; | ||
private ArrayList<Student> students; | ||
private StudentsAdapter adapter; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
random = new Random(); | ||
manPhotoIndex = womanPhotoIndex = 0; | ||
mansFirstNames = getResources().getStringArray(R.array.mans_first_names); | ||
womenFirstNames = getResources().getStringArray(R.array.women_first_names); | ||
lastNames = getResources().getStringArray(R.array.last_names); | ||
manPhotos = new int[]{R.drawable.male_01, R.drawable.male_02, R.drawable.male_03}; | ||
womanPhotos = new int[]{R.drawable.female_01, R.drawable.female_02, R.drawable.female_03}; | ||
|
||
|
||
RecyclerView recyclerView = findViewById(R.id.activity_main__recycler_view); | ||
FloatingActionButton fabAdd = findViewById(R.id.activity_main__fab_add); | ||
|
||
fabAdd.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
StudentAdderDialog dialog = new StudentAdderDialog(); | ||
dialog.setOnAdderStudentListener(new StudentAdderDialog.OnAdderStudentListener() { | ||
@Override | ||
public void add(String firstName, String lastName, boolean maleGender) { | ||
Student student = new Student(firstName, lastName, maleGender, maleGender ? manPhotos[manPhotoIndex++ % 3] : womanPhotos[womanPhotoIndex++ % 3]); | ||
students.add(student); | ||
adapter.notifyDataSetChanged(); | ||
} | ||
}); | ||
dialog.show(getSupportFragmentManager(), DIALOG_TAG); | ||
} | ||
}); | ||
|
||
students = createStudents(); | ||
adapter = new StudentsAdapter(students, new StudentsAdapter.OnStudentClickListener() { | ||
@Override | ||
public void onStudentClick(Student student) { | ||
showDialog(student); | ||
} | ||
}); | ||
recyclerView.setAdapter(adapter); | ||
LinearLayoutManager mLayoutManager = new GridLayoutManager(this, 2); | ||
recyclerView.setLayoutManager(mLayoutManager); | ||
} | ||
|
||
private void showDialog(final Student student) { | ||
ImageView view = new ImageView(this); | ||
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 300); | ||
view.setScaleType(ImageView.ScaleType.CENTER_CROP); | ||
view.setLayoutParams(layoutParams); | ||
view.setImageResource(student.getPhoto()); | ||
|
||
l0gark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); | ||
|
||
builder.setView(view); | ||
builder.setTitle(student.getFirstName() + " " + student.getSecondName()); | ||
builder.setMessage(student.isMaleGender() ? R.string.guy : R.string.girl); | ||
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { | ||
@Override | ||
public void onClick(DialogInterface dialogInterface, int i) { | ||
dialogInterface.cancel(); | ||
} | ||
}); | ||
|
||
builder.setNegativeButton(R.string.edit, new DialogInterface.OnClickListener() { | ||
@Override | ||
public void onClick(DialogInterface dialogInterface, int i) { | ||
StudentAdderDialog dialog = new StudentAdderDialog(); | ||
Bundle bundle = new Bundle(); | ||
bundle.putSerializable(StudentAdderDialog.STUDENT_TAG, student); | ||
dialog.setArguments(bundle); | ||
|
||
dialog.setOnUpdateStudentListener(new StudentAdderDialog.OnUpdateStudentListener() { | ||
@Override | ||
public void update(Student oldStudent, Student newStudent) { | ||
int index = students.indexOf(oldStudent); | ||
students.remove(oldStudent); | ||
if (oldStudent.isMaleGender() != newStudent.isMaleGender()) { | ||
newStudent.setPhoto(newStudent.isMaleGender() ? manPhotos[manPhotoIndex++ % 3] : womanPhotos[womanPhotoIndex++ % 3]); | ||
} | ||
students.add(index, newStudent); | ||
adapter.notifyDataSetChanged(); | ||
} | ||
}); | ||
|
||
dialog.show(getSupportFragmentManager(), DIALOG_TAG); | ||
dialogInterface.cancel(); | ||
} | ||
}); | ||
|
||
builder.setNeutralButton(R.string.delete, new DialogInterface.OnClickListener() { | ||
@Override | ||
public void onClick(DialogInterface dialogInterface, int i) { | ||
students.remove(student); | ||
adapter.notifyDataSetChanged(); | ||
dialogInterface.cancel(); | ||
} | ||
}); | ||
builder.create().show(); | ||
} | ||
|
||
|
||
ArrayList<Student> createStudents() { | ||
ArrayList<Student> students = new ArrayList<>(); | ||
for (int i = 0; i < 20; i++) { | ||
students.add(nextStudent(i % 2 == 0)); | ||
} | ||
return students; | ||
} | ||
|
||
|
||
Student nextStudent(boolean maleGender) { | ||
if (maleGender) { | ||
int nameIndex = random.nextInt(mansFirstNames.length); | ||
int lastNameIndex = random.nextInt(mansFirstNames.length); | ||
return new Student(mansFirstNames[nameIndex], lastNames[lastNameIndex], true, manPhotos[manPhotoIndex++ % 3]); | ||
} | ||
int nameIndex = random.nextInt(womenFirstNames.length); | ||
int lastNameIndex = random.nextInt(womenFirstNames.length); | ||
return new Student(womenFirstNames[nameIndex], lastNames[lastNameIndex] + "a", false, womanPhotos[womanPhotoIndex++ % 3]); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А если у модели Student было бы 50 полей? Лучше хранить список студентов
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это решил не исправлять?