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
Igor Lopatinskiy #23
Open
IgorLo
wants to merge
6
commits into
polis-vk:master
Choose a base branch
from
IgorLo: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
Igor Lopatinskiy #23
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
22dec9d
Реализовал добавление, удаление, изменение студентов
IgorLo 7756345
вынес мусор
IgorLo a8c2065
Merge pull request #1 from otopba/master
IgorLo 0dee4be
Фиксы в соответствии с комментариями
IgorLo 78549e4
Фикс метода копирования студента
IgorLo 7e14097
Выделение активного студента
IgorLo 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 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
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
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
116 changes: 116 additions & 0 deletions
116
app/src/main/java/ru/ok/technopolis/students/StudentAdapter.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 |
---|---|---|
@@ -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<StudentAdapter.StudentViewHolder> { | ||
|
||
public static final int NO_STUDENT = -1; | ||
private MainActivity context; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Плохой паттерн передавать активити в адаптер. Для работы с активити у тебя есть лисенеры |
||
private List<Student> 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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше всегда ставить фигурные скобки |
||
mainLayout.setBackgroundColor(Color.BLUE); | ||
else | ||
mainLayout.setBackgroundColor(Color.DKGRAY); | ||
face.setImageDrawable(context.getResources().getDrawable(student.getPhoto())); | ||
fullName.setText(student.getFirstName() + " " + student.getSecondName()); | ||
} | ||
} | ||
|
||
} |
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.
Зачем тут создаются новые объекты?