-
Notifications
You must be signed in to change notification settings - Fork 21
Students | Vadim Dyachkov #10
base: master
Are you sure you want to change the base?
Changes from 6 commits
1439ed4
cdc688d
b9d643e
d026308
ffcddc7
424897c
9db79e3
4ccd610
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ | |
/build | ||
/captures | ||
.externalNativeBuild | ||
.idea |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package ru.ok.technopolis.students; | ||
|
||
import android.content.Context; | ||
import android.support.constraint.ConstraintLayout; | ||
import android.util.AttributeSet; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.CheckBox; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import java.util.Random; | ||
|
||
public class StudentView extends ConstraintLayout { | ||
|
||
private static final Random rand = new Random(); | ||
private static final int[] malePhotos = {R.drawable.male_1, R.drawable.male_2, R.drawable.male_3}; | ||
private static final int[] femalePhotos = {R.drawable.female_1, R.drawable.female_2, R.drawable.female_3}; | ||
|
||
private Student student; | ||
|
||
private TextView firstNameTv; | ||
private TextView secondNameTv; | ||
private ImageView photoIv; | ||
private CheckBox isMaleCb; | ||
|
||
private OnClickListener onSave; | ||
private OnClickListener onCreate; | ||
private OnClickListener onRemove; | ||
private OnClickListener onHide; | ||
|
||
public StudentView(Context context) { | ||
super(context); | ||
init(context); | ||
} | ||
|
||
public StudentView(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
init(context); | ||
} | ||
|
||
public StudentView(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
init(context); | ||
} | ||
|
||
private void init(Context context) { | ||
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | ||
View view = inflater.inflate(R.layout.student_view, this, false); | ||
addView(view); | ||
|
||
photoIv = view.findViewById(R.id.student_view__photo); | ||
firstNameTv = view.findViewById(R.id.student_view__first_name); | ||
secondNameTv = view.findViewById(R.id.student_view__second_name); | ||
isMaleCb = view.findViewById(R.id.student_view__is_male); | ||
|
||
Button saveBtn = view.findViewById(R.id.student_view__save); | ||
saveBtn.setOnClickListener(new OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
if (student != null) { | ||
student.setFirstName(firstNameTv.getText().toString()); | ||
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. firstNameTv.getText() иногда возвращает null 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. fixed |
||
student.setSecondName(secondNameTv.getText().toString()); | ||
student.setMaleGender(isMaleCb.isChecked()); | ||
StudentView.this.onSave.onClick(v); | ||
} else { | ||
String firstName = firstNameTv.getText().toString(); | ||
String secondName = secondNameTv.getText().toString(); | ||
if (firstName.isEmpty() || secondName.isEmpty()) { | ||
String text = getContext().getString(R.string.error_insert_first_name_and_second_name); | ||
Toast.makeText(getContext(), text, Toast.LENGTH_LONG).show(); | ||
return; | ||
} | ||
boolean isMale = isMaleCb.isChecked(); | ||
int photo = randomPhoto(isMale); | ||
photoIv.setImageResource(photo); | ||
student = new Student(firstName, secondName, isMale, photo); | ||
v.setTag(student); | ||
onCreate.onClick(v); | ||
} | ||
} | ||
}); | ||
Button hideBtn = view.findViewById(R.id.student_view__hide); | ||
hideBtn.setOnClickListener(new OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
onHide.onClick(v); | ||
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. Во всех лисенерах нет проверки на нул. Если не был установлен лисенер 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. fixed |
||
StudentView.this.setVisibility(GONE); | ||
} | ||
}); | ||
Button removeBtn = view.findViewById(R.id.student_view__remove); | ||
removeBtn.setOnClickListener(new OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
if (student != null) { | ||
onRemove.onClick(v); | ||
StudentView.this.setVisibility(GONE); | ||
clearFields(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public void setOnRemove(OnClickListener onRemove) { | ||
this.onRemove = onRemove; | ||
} | ||
|
||
public void setOnSave(OnClickListener onSave) { | ||
this.onSave = onSave; | ||
} | ||
|
||
public void setOnCreate(OnClickListener onCreate) { | ||
this.onCreate = onCreate; | ||
} | ||
|
||
public void setOnHide(OnClickListener onHide) { | ||
this.onHide = onHide; | ||
} | ||
|
||
public void setStudent(Student student) { | ||
this.student = student; | ||
if (student != null) { | ||
photoIv.setImageResource(student.getPhoto()); | ||
firstNameTv.setText(student.getFirstName()); | ||
secondNameTv.setText(student.getSecondName()); | ||
isMaleCb.setChecked(student.isMaleGender()); | ||
} else { | ||
clearFields(); | ||
} | ||
setVisibility(VISIBLE); | ||
} | ||
|
||
private void clearFields() { | ||
photoIv.setImageResource(R.color.colorWhite); | ||
firstNameTv.setText(""); | ||
secondNameTv.setText(""); | ||
isMaleCb.setChecked(false); | ||
} | ||
|
||
private static int randomPhoto(boolean isMale) { | ||
return isMale | ||
? malePhotos[rand.nextInt(malePhotos.length)] | ||
: femalePhotos[rand.nextInt(femalePhotos.length)]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package ru.ok.technopolis.students; | ||
|
||
import android.annotation.SuppressLint; | ||
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; | ||
|
||
public class StudentsAdapter extends RecyclerView.Adapter<StudentsAdapter.StudentViewHolder> { | ||
|
||
private final StudentsRepository repository; | ||
private final View.OnClickListener onClick; | ||
|
||
public StudentsAdapter(StudentsRepository repository, View.OnClickListener onClick) { | ||
this.repository = repository; | ||
this.onClick = onClick; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { | ||
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.student_list_item, viewGroup, false); | ||
return new StudentViewHolder(view); | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(@NonNull StudentViewHolder viewHolder, int i) { | ||
viewHolder.bind(repository.get(i)); | ||
viewHolder.itemView.setTag(i); | ||
|
||
Integer active = repository.getActive(); | ||
if (active != null && active.equals(i)) { | ||
viewHolder.itemView.setBackgroundResource(R.color.colorLightGray); | ||
} else { | ||
viewHolder.itemView.setBackgroundResource(R.color.colorWhite); | ||
} | ||
|
||
viewHolder.itemView.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
int position = (int) v.getTag(); | ||
repository.setActive(position); | ||
onClick.onClick(v); | ||
notifyDataSetChanged(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return repository.size(); | ||
} | ||
|
||
static final class StudentViewHolder extends RecyclerView.ViewHolder { | ||
|
||
private final ImageView photo; | ||
private final TextView name; | ||
|
||
StudentViewHolder(@NonNull View itemView) { | ||
super(itemView); | ||
photo = itemView.findViewById(R.id.students_list_item_photo); | ||
name = itemView.findViewById(R.id.students_list_item_name); | ||
} | ||
|
||
@SuppressLint("SetTextI18n") | ||
private void bind(@NonNull Student student) { | ||
photo.setImageResource(student.getPhoto()); | ||
name.setText(student.getFirstName() + " " + student.getSecondName()); | ||
} | ||
} | ||
|
||
} |
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.
Что бы не писать addView(view); можно третим аргументов поставить true
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.
fixed