Skip to content

Commit

Permalink
Add default item selection
Browse files Browse the repository at this point in the history
  • Loading branch information
behkha committed Feb 16, 2019
1 parent 34029af commit ec643c7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 27 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
25 changes: 0 additions & 25 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public void onDaySelect(YearMonth yearMonth, Day day) {
.hasAnimation(false) // Animation for item selection . default is false
.load();



}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class PersianDatePicker extends LinearLayout implements View.OnClickListe
private ArrayList<YearMonth> mYearMonths;
private OnDaySelectListener mOnDaySelectListener;
private Typeface typeface;
private int selectedPosition = -1;
private float elevation = 0f;
private float radius = 0f;
private int selectedItemBackgroundColor = R.color.colorPrimary;
Expand Down Expand Up @@ -131,6 +132,7 @@ public void load(){
return;
setupView(mYearMonthIndex);
}

private void setupView(int index){

YearMonth yearMonth = this.mYearMonths.get(index);
Expand All @@ -146,6 +148,7 @@ private void setupView(int index){
adapter.setElevation(this.elevation);
adapter.setRadius(this.radius);
adapter.setAnimation(this.hasAnimation);
adapter.setSelectionPosition(selectedPosition);
this.mDaysRecyclerView.setAdapter( adapter );
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext() , LinearLayoutManager.HORIZONTAL , true);
this.mDaysRecyclerView.setLayoutManager(linearLayoutManager);
Expand Down Expand Up @@ -176,6 +179,41 @@ private String getTitle(YearMonth yearMonth){
return yearMonth.getYear() + " " + yearMonth.getMonth();
}

public int setItemSelected(String date) {
String[] strings = date.split("-");
if (strings.length != 3)
throw new IllegalArgumentException("date must divided by -");
int year = Integer.valueOf(strings[0]);
int day = Integer.valueOf(strings[2]);
int index = findYearMonthIndex(year,strings[1]);
if (index != -1) {
mYearMonthIndex = index;
selectedPosition = findDayIndex(mYearMonths.get(index),day);
setupView(mYearMonthIndex);
}
return selectedPosition;
}

public void scrollToPosition(int pos) {
mDaysRecyclerView.scrollToPosition(pos);
}

private int findYearMonthIndex(int year,String month){
for (int i = 0; i < mYearMonths.size(); i++) {
if (mYearMonths.get(i).getYear() == year && mYearMonths.get(i).getMonthNumber().equals(month))
return i;
}
return -1;
}

private int findDayIndex(YearMonth yearMonth, int day){
for (int i = 0; i < yearMonth.getDays().size(); i++) {
if (yearMonth.getDays().get(i).getNumber() == day)
return i;
}
return -1;
}

@Override
public void onClick(View v) {
int i = v.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,5 @@ public void setRadius(float radius) {
public void setAnimation(boolean animation){
this.hasAnimation = animation;
}
public void setSelectionPosition(int selectedPosition){ this.selectedPosition = selectedPosition;}
}

0 comments on commit ec643c7

Please sign in to comment.