我正在尝试通过日历实现Viewpager
。我正在使用日历Fragment
的三个相同实例来测试ViewPager
,请让我们平稳地将一个片段传输到下一个SWIPING。滑动将不会在片段本身中动态创建布局的片段区域中起作用,但是页面的其余部分(日期按钮的上方和下方)滑动效果很好!这是整个片段代码不适合的一些代码,因此必须编辑一些声明和按钮侦听器方法
//主要活动
public class calenderview extends AppCompatActivity { ArrayList<Fragment> fraglist; ViewPager pager; calenderviewpageradaptor adaptor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calenderview); fraglist = new ArrayList<>(); fraglist.add(new Calview_frag()); fraglist.add(new Calview_frag()); fraglist.add(new Calview_frag()); adaptor = new calenderviewpageradaptor(getSupportFragmentManager(),fraglist); pager = (ViewPager)findViewById(R.id.pager); pager.setAdapter(adaptor); }
public class calenderviewpageradaptor extends FragmentStatePagerAdapter {
ArrayList<Fragment> adaptorlist;
public calenderviewpageradaptor(FragmentManager fm,ArrayList<Fragment> list){
super (fm);
this.adaptorlist = list;
}
@Override
public int getCount(){
return 3;
}
@Override
public Fragment getItem(int pos){
return adaptorlist.get(pos);
}
}
}
公共类Calview_frag扩展了片段{
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
} private LinearLayout.LayoutParams getdaysLayoutParams() { LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); buttonParams.weight = 1; return buttonParams; } private void addDaysinCalendar(ViewGroup.LayoutParams buttonParams, DisplayMetrics metrics) { int engDaysArrayCounter = 0; LinearLayout.LayoutParams doitwithmarg = new LinearLayout.LayoutParams(120,120); doitwithmarg.setMarginStart(cacmarg); LinearLayout.LayoutParams doittoit = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT); doittoit.weight = 1;
for (int weekNumber = 0; weekNumber < 6; ++weekNumber) { for (int dayInWeek = 0; dayInWeek < 7; ++dayInWeek) { Button day = new Button(getContext()); day.setBackground(getResources().getDrawable(R.drawable.roundshapeclear)); day.setTextColor(getResources().getColor(R.color.GREY)); day.setBackgroundColor(Color.TRANSPARENT); day.setTextSize((int) metrics.density *5 ); day.setSingleLine(); if(dayInWeek!=0){ day.setLayoutParams(doitwithmarg); Space s = new Space(getContext()); s.setLayoutParams(doittoit); weeks[weekNumber].addView(s); days[engDaysArrayCounter] = day; weeks[weekNumber].addView(day);}
else{ day.setLayoutParams(doitwithmarg); days[engDaysArrayCounter] = day; weeks[weekNumber].addView(day);}
++engDaysArrayCounter; } } } private void init(View view) { DisplayMetrics metrics = getResources().getDisplayMetrics(); calendar = Calendar.getInstance(); weekOneLayout = (LinearLayout)view.findViewById(R.id.calendar_week_1); weekTwoLayout = (LinearLayout)view.findViewById(R.id.calendar_week_2); weekThreeLayout = (LinearLayout)view.findViewById(R.id.calendar_week_3); weekFourLayout = (LinearLayout)view.findViewById(R.id.calendar_week_4); weekFiveLayout = (LinearLayout)view.findViewById(R.id.calendar_week_5); weekSixLayout = (LinearLayout)view.findViewById(R.id.calendar_week_6); currentYear = (TextView)view.findViewById(R.id.current_date); currentMonth = (TextView)view.findViewById(R.id.current_month); currentDateDay = chosenDateDay = calendar.get(Calendar.DAY_OF_MONTH); if (userMonth != 0 && userYear != 0) { currentDateMonth = chosenDateMonth = userMonth; currentDateYear = chosenDateYear = userYear; } else { currentDateMonth = chosenDateMonth = calendar.get(Calendar.MONTH); currentDateYear = chosenDateYear = calendar.get(Calendar.YEAR); } currentYear.setText("" + chosenDateYear); currentMonth.setText(ENG_MONTH_NAMES[currentDateMonth]); initializeDaysWeeks(); if (userButtonParams != null) { defaultButtonParams = userButtonParams; } else { defaultButtonParams = getdaysLayoutParams(); } addDaysinCalendar(defaultButtonParams,metrics); initCalendarWithDate(chosenDateYear, chosenDateMonth, chosenDateDay,view); } private void initializeDaysWeeks() { weeks = new LinearLayout[6]; days = new Button[6 * 7]; weeks[0] = weekOneLayout; weeks[1] = weekTwoLayout; weeks[2] = weekThreeLayout; weeks[3] = weekFourLayout; weeks[4] = weekFiveLayout; weeks[5] = weekSixLayout; } private void initCalendarWithDate(int year, int month, int day, View v) { if (calendar == null) calendar = Calendar.getInstance(); calendar.set(year, month, day); int daysInCurrentMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); chosenDateYear = year; chosenDateMonth = month; chosenDateDay = day; calendar.set(year, month, 1); int firstDayOfCurrentMonth = calendar.get(Calendar.DAY_OF_WEEK); Log.d(tag,"FFFFFFiiiiiiiiiiirrrrrrrrrrsssssssssst day of month int"+firstDayOfCurrentMonth); calendar.set(year, month, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); Log.d(tag,"LLLLLLLLLLLLlaaaaasttttttttttt day of month int"+daysInCurrentMonth); int dayNumber = 1; int daysLeftInFirstWeek = 0; firstDayOfCurrentMonth= firstDayOfCurrentMonth-1; int indexOfDayAfterLastDayOfMonth = 0; if (firstDayOfCurrentMonth != 0) { daysLeftInFirstWeek = firstDayOfCurrentMonth; indexOfDayAfterLastDayOfMonth = daysLeftInFirstWeek + daysInCurrentMonth; > for (int i = firstDayOfCurrentMonth; i < firstDayOfCurrentMonth + daysInCurrentMonth; ++i) { //checks all dates of calendar to set background and text colors if (currentDateMonth == chosenDateMonth && currentDateYear == chosenDateYear && dayNumber == currentDateDay){ days[i].setBackground(getResources().getDrawable(R.drawable.roundshapegrey)); days[i].setTextColor(Color.WHITE); } else { days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear)); days[i].setTextColor(Color.BLACK); } int[] dateArr = new int[3]; dateArr[0] = dayNumber; dateArr[1] = chosenDateMonth; dateArr[2] = chosenDateYear; days[i].setTag(dateArr); days[i].setText(String.valueOf(dayNumber)); days[i].setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onDayClick(v); } }); ++dayNumber; } > for (int i = 7; i < 7 + daysInCurrentMonth; ++i) { if (currentDateMonth == chosenDateMonth && currentDateYear == chosenDateYear && dayNumber == currentDateDay) { days[i].setBackground(getResources().getDrawable(R.drawable.roundshape)); days[i].setTextColor(Color.WHITE); } else { days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear)); days[i].setTextColor(Color.BLACK); } int[] dateArr = new int[3]; dateArr[0] = dayNumber; dateArr[1] = chosenDateMonth; dateArr[2] = chosenDateYear; days[i].setTag(dateArr); days[i].setText(String.valueOf(dayNumber)); days[i].setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onDayClick(v); } }); ++dayNumber; } }
//显示上个月和下个月的天
if (month > 0) calendar.set(year, month - 1, 1); else calendar.set(year - 1, 11, 1); int daysInPreviousMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); for (int i = daysLeftInFirstWeek - 1; i >= 0; --i) { int[] dateArr = new int[3]; if (chosenDateMonth > 0) { if (currentDateMonth == chosenDateMonth - 1 && currentDateYear == chosenDateYear && daysInPreviousMonth == currentDateDay) { } else { days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear)); } dateArr[0] = daysInPreviousMonth; dateArr[1] = chosenDateMonth - 1; dateArr[2] = chosenDateYear; } else { if (currentDateMonth == 11 && currentDateYear == chosenDateYear - 1 && daysInPreviousMonth == currentDateDay) { } else { days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear)); } dateArr[0] = daysInPreviousMonth; dateArr[1] = 11; dateArr[2] = chosenDateYear - 1; } days[i].setTag(dateArr); days[i].setTextColor(getResources().getColor(R.color.GREY));; days[i].setText(""); days[i].setOnClickListener(null); } int nextMonthDaysCounter = 1; for (int i = indexOfDayAfterLastDayOfMonth; i < days.length; ++i) { int[] dateArr = new int[3]; if (chosenDateMonth < 11) { if (currentDateMonth == chosenDateMonth + 1 && currentDateYear == chosenDateYear && nextMonthDaysCounter == currentDateDay) { // days[i].setBackground(getResources().getDrawable(R.drawable.roundshapegrey)); days[i].setTextColor(Color.WHITE); } else { days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear)); } dateArr[0] = nextMonthDaysCounter; dateArr[1] = chosenDateMonth + 1; dateArr[2] = chosenDateYear; } else { if (currentDateMonth == 0 && currentDateYear == chosenDateYear + 1 && nextMonthDaysCounter == currentDateDay) { // selectedDayButton.setBackground(getResources().getDrawable(R.drawable.roundshapegrey)); selectedDayButton.setTextColor(Color.WHITE); // days[i].setBackground(getResources().getDrawable(R.drawable.roundshapegrey)); days[i].setTextColor(Color.WHITE); } else { days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear)); } dateArr[0] = nextMonthDaysCounter; dateArr[1] = 0; dateArr[2] = chosenDateYear + 1; } days[i].setTag(dateArr); days[i].setText(""); days[i].setOnClickListener(null); } calendar.set(chosenDateYear, chosenDateMonth, chosenDateDay); currentYear.setText(" " + chosenDateYear); currentMonth.setText(ENG_MONTH_NAMES[chosenDateMonth]); }
public Calview_frag() {//required empty constructor }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View layoutview = inflater.inflate(R.layout.fragment_calview,container,false); but = (ImageButton)layoutview.findViewById(R.id.getitbutton); fbut = (ImageButton)layoutview.findViewById(R.id.getit2button); display = getActivity().getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); screenheight = size.y; screenwidth = size.x; Log.d(tag,"sssssssssssccreen size "+screenwidth); if(screenwidth<1300) cacmarg = screenwidth/50; else cacmarg = screenwidth/25; init(layoutview); return layoutview; }
}
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ScrollView android:id="@+id/scrollView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageButton android:id="@+id/getitbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_alignParentLeft="true" android:src="@drawable/backbutton" tools:layout_editor_absoluteY="0dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/current_month" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/bluegrey" android:textSize="28sp" /> <TextView android:id="@+id/current_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="4dp" android:textColor="@color/bluegrey" android:textSize="28sp" /> </LinearLayout> <ImageButton android:id="@+id/getit2button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:src="@drawable/forwardbutton" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="7"> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/transparent" android:text="@string/sunday" android:textColor="@color/bluegrey" android:textSize="16sp" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/transparent" android:text="@string/monday" android:textColor="@color/bluegrey" android:textSize="16sp" /> <Button android:id="@+id/button10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/transparent" android:text="@string/tuesday" android:textColor="@color/bluegrey" android:textSize="16sp" /> <Button android:id="@+id/button9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/transparent" android:text="@string/wednesday" android:textColor="@color/bluegrey" android:textSize="16sp" /> <Button android:id="@+id/button8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/transparent" android:text="@string/thursday" android:textColor="@color/bluegrey" android:textSize="16sp" /> <Button android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/transparent" android:text="@string/friday" android:textColor="@color/bluegrey" android:textSize="16sp" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/transparent" android:text="@string/saturday" android:textColor="@color/bluegrey" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:id="@+id/calendar_week_1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="7" /> <LinearLayout android:id="@+id/calendar_week_2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="7" /> <LinearLayout android:id="@+id/calendar_week_3" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="7" /> <LinearLayout android:id="@+id/calendar_week_4" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="7" /> <LinearLayout android:id="@+id/calendar_week_5" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="7" /> <LinearLayout android:id="@+id/calendar_week_6" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="7" /> </LinearLayout> </ScrollView> </LinearLayout>
答案 0 :(得分:0)
您是否正在将Fragment的实例传递给适配器?不,我想。您应该将片段列表与片段管理器一起传递给适配器,还应该相应地更改calendarViewPager。
List<Fragment> fragments =new ArrayList<>();
fragments.add(Calview_frag());
fragments.add(Calview_frag());
fragments.add(Calview_frag());
calenderviewpageradaptor adaptor =
new calenderviewpageradaptor(getSupportFragmentManager(),fragments);