这是我的代码我的问题是这个。在日历视图寻呼机适配器我是5000页,但offScreenPage限制是1,我有3页准时,但查看寻呼机不是smoth滚动,并有不良的性能。当我从日历片段视图分页器中删除此行时,平滑滚动精细。但我不知道是什么问题。
gregorianCal = new GregorianCalendar(persianCal);
IslamicCalendar islamicCal = new IslamicCalendar(persianCal);
///////////////////////////////////////////////
for (int i = 0; i < persianCal.getNumberOfDaysInMonth(); i++) {
DateCalendar dateCalendar = new DateCalendar();
dateCalendar.nameOFDay = persianCal.getWeekDay();
//////set month names/////////////
dateCalendar.shamsiMonthName = persianCal.getMonthName();
dateCalendar.hejriMonthName = islamicCal.getMonthName();
dateCalendar.gregorianMonthName = gregorianCal.getMonthName();
//////set persian calendar/////
dateCalendar.shamsiDayNumber = persianCal.getDay();
dateCalendar.shamsiMonthNumber = persianCal.getMonth();
dateCalendar.shamsiYearNumber = persianCal.getYear();
//////set islamic calendar/////
dateCalendar.hejriDayNumber = islamicCal.getDay();
dateCalendar.hejriMonthNumber = islamicCal.getMonth();
dateCalendar.hejriYearNumber = islamicCal.getYear();
//////set gregorian calendar/////
dateCalendar.gregorianDayNumber = gregorianCal.getDay();
dateCalendar.gregorianMonthNumber = gregorianCal.getMonth();
dateCalendar.gregorianYearNumber = gregorianCal.getYear();
///////select current day of Month
if (persianCal.getDay() == currentPersianCal.getDay() && persianCal.getMonth() == currentPersianCal.getMonth() && persianCal.getYear() == currentPersianCal.getYear()) {
dateCalendar.isCurrentDay = true;
dateCalendar.isDaySelected = true;
}
///////add days of current month//////
dateCalendarList.add(dateCalendar);
if (i < persianCal.getNumberOfDaysInMonth() - 1) {
persianCal.nextDay();
gregorianCal.nextDay();
islamicCal.nextDay();
}
}
我搜索了很多类似的viewPager主题,但我没有找到它的灵魂。
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;
import com.example.moein.mycalendar.R;
import com.example.moein.mycalendar.myCalendar_adapter.GridAdapter1;
import com.example.moein.mycalendar.myCalendar_library.date.GregorianCalendar;
import com.example.moein.mycalendar.myCalendar_library.date.IslamicCalendar;
import com.example.moein.mycalendar.myCalendar_library.date.PersianCalendar;
import com.example.moein.mycalendar.myCalendar_library.util.Operations;
import com.example.moein.mycalendar.myCalendar_model.DateCalendar;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
/**
* Created by Moein on 9/16/2017.
*/
public class CalendarFragment extends Fragment {
private int year;
private int month;
private int day;
// newInstance constructor for creating fragment with arguments
public static CalendarFragment newInstance(int year, int month, int day) {
CalendarFragment calendarFragment = new CalendarFragment();
Bundle args = new Bundle();
args.putInt("year", year);
args.putInt("month", month);
args.putInt("day", day);
calendarFragment.setArguments(args);
return calendarFragment;
}
// Store instance variables based on arguments passed
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
year = getArguments().getInt("year");
month = getArguments().getInt("month");
day = getArguments().getInt("day");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_calendar, container, false);
TextView yearAndMonth = view.findViewById(R.id.yearAndMonth);
GridView gridView = view.findViewById(R.id.gridCalendar);
/////set timeZone Asia/Tehran and set current calendar///////////////////////////////
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.setTimeZone(TimeZone.getTimeZone("Asia/Tehran"));
GregorianCalendar currentGregorianCal = new GregorianCalendar(calendar);
PersianCalendar currentPersianCal = new PersianCalendar(currentGregorianCal);
GregorianCalendar gregorianCal = new GregorianCalendar(year, month, day);
PersianCalendar persianCal = new PersianCalendar(gregorianCal);
/////////add weekDay names to the list of gridAdapter///////
String[] weekDays = getResources().getStringArray(R.array.weekDays);
List<DateCalendar> dateCalendarList = new ArrayList<>();
for (String weekDay : weekDays) {
DateCalendar dateCalendar = new DateCalendar();
dateCalendar.nameOFDay = weekDay;
dateCalendarList.add(dateCalendar);
}
//set title of calendar/////////
yearAndMonth.setText(persianCal.getMonthName() + " " + Operations.toPersianNumber(String.valueOf(persianCal.getYear())));
///Back to the beginning of the month
persianCal.subtractDays(persianCal.getDay() - 1);
//////add empty days to the beginning of the month
int lengthOfEmptyDays = 0;
switch (persianCal.getWeekDay()) {
case "شنبه":
lengthOfEmptyDays = 0;
break;
case "یکشنبه":
lengthOfEmptyDays = 1;
break;
case "دوشنبه":
lengthOfEmptyDays = 2;
break;
case "سه\u200Cشنبه":
lengthOfEmptyDays = 3;
break;
case "چهارشنبه":
lengthOfEmptyDays = 4;
break;
case "پنج\u200Cشنبه":
lengthOfEmptyDays = 5;
break;
case "جمعه":
lengthOfEmptyDays = 6;
break;
}
for (int i = 0; i < lengthOfEmptyDays; i++) {
DateCalendar dateCalendar = new DateCalendar();
dateCalendar.isEmpty = true;
dateCalendarList.add(dateCalendar);
}
gregorianCal = new GregorianCalendar(persianCal);
IslamicCalendar islamicCal = new IslamicCalendar(persianCal);
///////////////////////////////////////////////
for (int i = 0; i < persianCal.getNumberOfDaysInMonth(); i++) {
DateCalendar dateCalendar = new DateCalendar();
dateCalendar.nameOFDay = persianCal.getWeekDay();
//////set month names/////////////
dateCalendar.shamsiMonthName = persianCal.getMonthName();
dateCalendar.hejriMonthName = islamicCal.getMonthName();
dateCalendar.gregorianMonthName = gregorianCal.getMonthName();
//////set persian calendar/////
dateCalendar.shamsiDayNumber = persianCal.getDay();
dateCalendar.shamsiMonthNumber = persianCal.getMonth();
dateCalendar.shamsiYearNumber = persianCal.getYear();
//////set islamic calendar/////
dateCalendar.hejriDayNumber = islamicCal.getDay();
dateCalendar.hejriMonthNumber = islamicCal.getMonth();
dateCalendar.hejriYearNumber = islamicCal.getYear();
//////set gregorian calendar/////
dateCalendar.gregorianDayNumber = gregorianCal.getDay();
dateCalendar.gregorianMonthNumber = gregorianCal.getMonth();
dateCalendar.gregorianYearNumber = gregorianCal.getYear();
///////select current day of Month
if (persianCal.getDay() == currentPersianCal.getDay() && persianCal.getMonth() == currentPersianCal.getMonth() && persianCal.getYear() == currentPersianCal.getYear()) {
dateCalendar.isCurrentDay = true;
dateCalendar.isDaySelected = true;
}
///////add days of current month//////
dateCalendarList.add(dateCalendar);
if (i < persianCal.getNumberOfDaysInMonth() - 1) {
persianCal.nextDay();
gregorianCal.nextDay();
islamicCal.nextDay();
}
}
gridView.setAdapter(new GridAdapter1(getActivity(), dateCalendarList));
return view;
}
}
public class CalendarViewPagerAdapter extends FragmentStatePagerAdapter{
private GregorianCalendar centerGregorianCalendar;
private GregorianCalendar nextGregorianCalendar;
private GregorianCalendar prevGregorianCalendar;
private final int MAX = 5000;
public CalendarViewPagerAdapter(FragmentManager fm, GregorianCalendar centerGregorianCalendar, GregorianCalendar nextGregorianCalendar, GregorianCalendar prevGregorianCalendar) {
super(fm);
this.centerGregorianCalendar = centerGregorianCalendar;
this.prevGregorianCalendar = prevGregorianCalendar;
this.nextGregorianCalendar = nextGregorianCalendar;
}
@Override
public Fragment getItem(int position) {
position = position % 3;
if (position == 0) {
return new CalendarFragment().newInstance(prevGregorianCalendar.getYear(), prevGregorianCalendar.getMonth(), prevGregorianCalendar.getDay());
} else if (position == 1) {
return new CalendarFragment().newInstance(centerGregorianCalendar.getYear(), centerGregorianCalendar.getMonth(), centerGregorianCalendar.getDay());
} else {
return new CalendarFragment().newInstance(nextGregorianCalendar.getYear(), nextGregorianCalendar.getMonth(), nextGregorianCalendar.getDay());
}
}
@Override
public int getCount() {
return MAX;
}
}
public class MainActivity extends AppCompatActivity {
private int[] CenterPage = {2500};
private CalendarViewPagerAdapter calendarViewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/////set timeZone Asia/Tehran///////////////////////////////
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.setTimeZone(TimeZone.getTimeZone("Asia/Tehran"));
final GregorianCalendar gregorianCal = new GregorianCalendar(calendar);
final GregorianCalendar nextGregorianCal = new GregorianCalendar(calendar);
final GregorianCalendar prevGregorianCal = new GregorianCalendar(calendar);
if (gregorianCal.getMonth() == 12) {
nextGregorianCal.setMonth(1);
nextGregorianCal.setYear(nextGregorianCal.getYear() + 1);
} else {
nextGregorianCal.setMonth(nextGregorianCal.getMonth() + 1);
}
if (gregorianCal.getMonth() == 1) {
prevGregorianCal.setMonth(12);
prevGregorianCal.setYear(prevGregorianCal.getYear() - 1);
} else {
prevGregorianCal.setMonth(prevGregorianCal.getMonth() - 1);
}
final ViewPager calendarViewPager = findViewById(R.id.calendar_viewPager);
calendarViewPagerAdapter = new CalendarViewPagerAdapter(getSupportFragmentManager(), gregorianCal, nextGregorianCal, prevGregorianCal);
calendarViewPager.setAdapter(calendarViewPagerAdapter);
calendarViewPager.setOffscreenPageLimit(1);
calendarViewPager.setCurrentItem(2500);
calendarViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position < CenterPage[0]) {
switch (position % 3) {
case 0:
nextGregorianCal.set(prevGregorianCal);
if (nextGregorianCal.getMonth() == 1) {
nextGregorianCal.setMonth(12);
nextGregorianCal.setYear(nextGregorianCal.getYear() - 1);
} else {
nextGregorianCal.setMonth(nextGregorianCal.getMonth() - 1);
}
break;
case 1:
prevGregorianCal.set(gregorianCal);
if (prevGregorianCal.getMonth() == 1) {
prevGregorianCal.setMonth(12);
prevGregorianCal.setYear(prevGregorianCal.getYear() - 1);
} else {
prevGregorianCal.setMonth(prevGregorianCal.getMonth() - 1);
}
break;
case 2:
gregorianCal.set(nextGregorianCal);
if (gregorianCal.getMonth() == 1) {
gregorianCal.setMonth(12);
gregorianCal.setYear(gregorianCal.getYear() - 1);
} else {
gregorianCal.setMonth(gregorianCal.getMonth() - 1);
}
break;
}
} else if (position > CenterPage[0]) {
switch (position % 3) {
case 0:
gregorianCal.set(prevGregorianCal);
if (gregorianCal.getMonth() == 12) {
gregorianCal.setMonth(1);
gregorianCal.setYear(gregorianCal.getYear() + 1);
} else {
gregorianCal.setMonth(gregorianCal.getMonth() + 1);
}
break;
case 1:
nextGregorianCal.set(gregorianCal);
if (nextGregorianCal.getMonth() == 12) {
nextGregorianCal.setMonth(1);
nextGregorianCal.setYear(nextGregorianCal.getYear() + 1);
} else {
nextGregorianCal.setMonth(nextGregorianCal.getMonth() + 1);
}
break;
case 2:
prevGregorianCal.set(nextGregorianCal);
if (prevGregorianCal.getMonth() == 12) {
prevGregorianCal.setMonth(1);
prevGregorianCal.setYear(prevGregorianCal.getYear() + 1);
} else {
prevGregorianCal.setMonth(prevGregorianCal.getMonth() + 1);
}
break;
}
}
CenterPage[0] = position;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
答案 0 :(得分:0)
我希望你可以使用这个代码 我的网站 https://mohammadhosseinaref.ir
public static string PersianDayOfWeek(DayOfWeek dayOfWeek)
{
switch (dayOfWeek)
{
case DayOfWeek.Sunday:
return "یکشنبه";
case DayOfWeek.Monday:
return "دوشنبه";
case DayOfWeek.Tuesday:
return "سه شنبه";
case DayOfWeek.Wednesday:
return "چهار شنبه";
case DayOfWeek.Thursday:
return "پنج شنبه";
case DayOfWeek.Friday:
return "جمعه";
case DayOfWeek.Saturday:
return "شنبه";
default:
return null;
}
}
public static Dictionary<int, string> ListOfPersianDayOfMonth(int month, int years)
{
Dictionary<int, string> dicDays = new Dictionary<int, string>();
PersianCalendar p = new PersianCalendar();
var dates = Enumerable.Range(1, p.GetDaysInMonth(years, month))
.Select(day => ParsePersianToGorgian(years + "/" + month + "/" + day))
.ToList();
foreach (var item in dates)
{
var day = GeorgianToPersian(item.Value, ShowMode.OnlyDate);
dicDays.Add(int.Parse(day.Split('/')[2]), DayPersian(item.Value.DayOfWeek) + " " + day);
}
return dicDays;
}