I write my weekly calendar for the application. I created a fragment that I add to the ViewPager via my adapter using newInstance() . In the fragment there is a GridView , which is filled with dates depending on the page number. When I select a GridView cell, I get a cell GridView . But if you select a cell on one GridView page, then flip to another and select a cell there, the selection is saved on both pages.

The task is to ensure that when highlighting a date on another calendar page, previous selections disappear, I don’t understand how to do this.

Class in which the Viewpager and adapter

 public class ServiceMenuActivity extends AppCompatActivity { static final String TAG = "myLogs"; static final int PAGE_COUNT = 9; int pos; ViewPager CalendarPager; CalendarPagerAdapter _CalendarPagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); setContentView(R.layout.activity_service_menu); CalendarPager = (ViewPager)findViewById(R.id.CalendarPager); _CalendarPagerAdapter = new CalendarPagerAdapter(getSupportFragmentManager()); CalendarPager.setAdapter(_CalendarPagerAdapter); CalendarPager.setOffscreenPageLimit(9); CalendarPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { pos =position; //Log.d(TAG, ""+pos); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(int state) { } }); } public void onClick(View view) { Log.d(TAG, ""+_CalendarPagerAdapter.getItemId(pos)); CalendarFragmentPager.newInstance(pos).ClearC(); } } class CalendarPagerAdapter extends FragmentPagerAdapter { static final String TAG = "myLogs"; private long baseId = 0; public CalendarPagerAdapter(FragmentManager fm) { super(fm); } @Override public Object instantiateItem(ViewGroup container, int position) { Fragment fragment = (Fragment) super.instantiateItem(container, position); registeredFragments.put(position, fragment); return fragment; } @Override public Fragment getItem(int position) { return CalendarFragmentPager.newInstance(position); } public Fragment getRegisteredFragment(int position){ return registeredFragments.get(position); } @Override public long getItemId(int position) { // give an ID different from position when position has been changed return baseId + position; } @Override public int getCount() { return PAGE_COUNT; } } 

Fragment itself

 public class CalendarFragmentPager extends Fragment { static final String ARGUMENT_PAGE_NUMBER = "arg_page_number"; static final String TAG = "myLogs"; TextView tvPage; int pageNumber; int StartPosition=0; Calendar calendar; GridView CalendarGrid; CustomCalendarAdapter _CustomCalendarAdapter; static CalendarFragmentPager newInstance(int page) { CalendarFragmentPager _CalendarFragmentPager = new CalendarFragmentPager(); Bundle arguments = new Bundle(); arguments.putInt(ARGUMENT_PAGE_NUMBER, page); _CalendarFragmentPager.setArguments(arguments); return _CalendarFragmentPager; } public CalendarFragmentPager(){ } @Override public void onCreate(Bundle saveInstanceState) { super.onCreate(saveInstanceState); pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.calendar_fragment, null); final String[] DateArray = {"", "", "", "", "", "", ""}; String Month = ""; String DayName; final Date CurrentDate; Date newDate; calendar = Calendar.getInstance(Locale.getDefault()); CurrentDate = calendar.getTime(); switch (pageNumber) { case 0: for (int i = 0; i < 7; i++) { calendar.add(Calendar.DAY_OF_YEAR, i); newDate = calendar.getTime(); DayName = (String) DateFormat.format("dd\nEE", newDate); Month = (String) DateFormat.format("LLLL", newDate); DateArray[i] = DayName; calendar.setTime(CurrentDate); } break; case 1: for (int i = 0; i < 7; i++) { calendar.add(Calendar.DAY_OF_YEAR, i + 7); newDate = calendar.getTime(); DayName = (String) DateFormat.format("dd\nEE", newDate); Month = (String) DateFormat.format("LLLL", newDate); DateArray[i] = DayName; calendar.setTime(CurrentDate); } break; case 2: for (int i = 0; i < 7; i++) { calendar.add(Calendar.DAY_OF_YEAR, i + 14); newDate = calendar.getTime(); DayName = (String) DateFormat.format("dd\nEE", newDate); Month = (String) DateFormat.format("LLLL", newDate); DateArray[i] = DayName; calendar.setTime(CurrentDate); } break; case 3: for (int i = 0; i < 7; i++) { calendar.add(Calendar.DAY_OF_YEAR, i + 21); newDate = calendar.getTime(); DayName = (String) DateFormat.format("dd\nEE", newDate); Month = (String) DateFormat.format("LLLL", newDate); DateArray[i] = DayName; calendar.setTime(CurrentDate); } break; case 4: for (int i = 0; i < 7; i++) { calendar.add(Calendar.DAY_OF_YEAR, i + 28); newDate = calendar.getTime(); DayName = (String) DateFormat.format("dd\nEE", newDate); Month = (String) DateFormat.format("LLLL", newDate); DateArray[i] = DayName; calendar.setTime(CurrentDate); } break; case 5: for (int i = 0; i < 7; i++) { calendar.add(Calendar.DAY_OF_YEAR, i + 35); newDate = calendar.getTime(); DayName = (String) DateFormat.format("dd\nEE", newDate); Month = (String) DateFormat.format("LLLL", newDate); DateArray[i] = DayName; calendar.setTime(CurrentDate); } break; case 6: for (int i = 0; i < 7; i++) { calendar.add(Calendar.DAY_OF_YEAR, i + 42); newDate = calendar.getTime(); DayName = (String) DateFormat.format("dd\nEE", newDate); Month = (String) DateFormat.format("LLLL", newDate); DateArray[i] = DayName; calendar.setTime(CurrentDate); } break; case 7: for (int i = 0; i < 7; i++) { calendar.add(Calendar.DAY_OF_YEAR, i + 49); newDate = calendar.getTime(); DayName = (String) DateFormat.format("dd\nEE", newDate); Month = (String) DateFormat.format("LLLL", newDate); DateArray[i] = DayName; calendar.setTime(CurrentDate); } break; case 8: for (int i = 0; i < 7; i++) { calendar.add(Calendar.DAY_OF_YEAR, i + 56); newDate = calendar.getTime(); DayName = (String) DateFormat.format("dd\nEE", newDate); Month = (String) DateFormat.format("LLLL", newDate); DateArray[i] = DayName; calendar.setTime(CurrentDate); } break; } tvPage = (TextView) view.findViewById(R.id.tvPage); tvPage.setText(Month.toUpperCase()); CalendarGrid = (GridView) view.findViewById(R.id.CalendarGrid); _CustomCalendarAdapter= new CustomCalendarAdapter(getActivity().getApplicationContext(), DateArray); CalendarGrid.setChoiceMode(GridView.CHOICE_MODE_SINGLE); CalendarGrid.setAdapter(_CustomCalendarAdapter); CalendarGrid.setNumColumns(7); final String temp =(String) DateFormat.format("dd\nEE", CurrentDate); if(_CustomCalendarAdapter.getItem(0).equals(temp)) { CalendarGrid.setItemChecked(0, true); } CalendarGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { } }); return view; } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy: " + pageNumber); } public void ClearC() { CalendarGrid.clearChoices(); } } 
  • Methods and variables are named with a small letter. You have a Big and the code from this is almost unreadable. Also, it’s almost impossible to help you without an error message and specifying an occurrence string. - Yuriy SPb
  • Thank you very much for the remark I will consider and refractor. It's not even a mistake, I can't figure out how to do it at all. - Jorik
  • You can save references to the fragments in the adapter and then call the fragment methods to deselect the selection - YuriySPb
  • Yuri You can sample code pls. How to save these links? - Jorik
  • Get in the adapter List <Fragment> frags = new ArrayList <> (); Then put it into this list in getItem before returning the fragment - Yuriy SPb

0