Tibolte / AgendaCalendarView

[DEPRECATED] An Android project providing easy navigation between a calendar and an agenda. This library replicates the basic features from the Sunrise Calendar (now Outlook) app, coupled with some small design touch from the Google Calendar app.
1.23k stars 335 forks source link

calendarViewFragment cannot be cast to android.support.v4.app.Fragment #95

Open tchape opened 7 years ago

tchape commented 7 years ago

I don't open my calendarView in fragment my source MainActivity package seed.prie.iuc.myiucap;

import android.content.res.Configuration; import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.view.GravityCompat; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.MenuItem; import android.widget.Toast;

import com.astuetz.PagerSlidingTabStrip;

import seed.prie.iuc.myiucap.fragment.calendarViewFragment;

public class MainActivity extends AppCompatActivity{ private DrawerLayout drawerLayout; private DrawerLayout mDrawer; private Toolbar toolbar; private NavigationView nvDrawer; private ActionBarDrawerToggle drawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager()));

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Find our drawer view
    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerToggle = setupDrawerToggle();

    // Tie DrawerLayout events to the ActionBarToggle
    mDrawer.addDrawerListener(drawerToggle);

    // Find our drawer view
    nvDrawer = (NavigationView) findViewById(R.id.nvView);
    // Setup drawer view
    setupDrawerContent(nvDrawer);

    // Give the PagerSlidingTabStrip the ViewPager
    PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    // Attach the view pager to the tab strip
    tabsStrip.setViewPager(viewPager);
   // toolbar = (Toolbar) findViewById(R.id.toolbar);
   // setSupportActionBar(toolbar);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // The action bar home/up action should open or close the drawer.
    switch (item.getItemId()) {
        case android.R.id.home:
            mDrawer.openDrawer(GravityCompat.START);
            return true;
    }
    if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    selectDrawerItem(menuItem);
                    return true;
                }
            });
}
public void selectDrawerItem(MenuItem menuItem) {

    // Create a new fragment and specify the fragment to show based on nav item clicked
    Fragment fragment = null;
    Class fragmentClass;
    switch(menuItem.getItemId()) {
        case R.id.nav_Accueil:
            fragmentClass = calendarViewFragment.class;
            break;
        case R.id.nav_aide:
            fragmentClass = calendarViewFragment.class;
           //fragmentClass = SecondFragment.class;
            break;
        case R.id.nav_Apropos:
            fragmentClass = calendarViewFragment.class;
           // fragmentClass = ThirdFragment.class;
            break;
        case R.id.nav_deconnexion:
            fragmentClass = calendarViewFragment.class;
            // fragmentClass = ThirdFragment.class;
            break;
        case R.id.nav_ETD:
            fragmentClass = calendarViewFragment.class;
           // fragmentClass = ThirdFragment.class;
            break;
        case R.id.nav_note:
            fragmentClass = calendarViewFragment.class;
           // fragmentClass = ThirdFragment.class;
            break;
        case R.id.nav_parametre:
            fragmentClass = calendarViewFragment.class;
           // fragmentClass = ThirdFragment.class;
            break;
        case R.id.nav_sco:
            fragmentClass = calendarViewFragment.class;
           // fragmentClass = ThirdFragment.class;
            break;
        default:
            fragmentClass = calendarViewFragment.class;
         //  fragmentClass = FirstFragment.class;
    }

    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        Log.d("ERREUR", e.getMessage());
        e.printStackTrace();
    }

    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

    // Highlight the selected item has been done by NavigationView
    menuItem.setChecked(true);
    // Set action bar title
   //setTitle(menuItem.getTitle());
    // Close the navigation drawer
  //  mDrawer.closeDrawers();
}

private ActionBarDrawerToggle setupDrawerToggle() {
    // NOTE: Make sure you pass in a valid toolbar reference.  ActionBarDrawToggle() does not require it
    // and will not render the hamburger icon without it.
    return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open,  R.string.drawer_close);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggles
    drawerToggle.onConfigurationChanged(newConfig);
}

}

my source calendarViewFragment package seed.prie.iuc.myiucap.fragment;

import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;

import com.github.tibolte.agendacalendarview.AgendaCalendarView; import com.github.tibolte.agendacalendarview.CalendarPickerController; import com.github.tibolte.agendacalendarview.models.BaseCalendarEvent; import com.github.tibolte.agendacalendarview.models.CalendarEvent; import com.github.tibolte.agendacalendarview.models.DayItem;

import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Locale;

import seed.prie.iuc.myiucap.MainActivity; import seed.prie.iuc.myiucap.R; import seed.prie.iuc.myiucap.edt.emploiTemps;

/**

public class calendarViewFragment extends Fragment implements CalendarPickerController { private static final String LOG_TAG = emploiTemps.class.getSimpleName(); AgendaCalendarView mAgendaCalendarView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //super.onCreate(savedInstanceState);
    //setContentView(R.layout.emploitemps);
    View view  = inflater.inflate(R.layout.emploitemps, container, false);
    Calendar minDate = Calendar.getInstance();
    Calendar maxDate = Calendar.getInstance();

    minDate.add(Calendar.MONTH, -2);
    minDate.set(Calendar.DAY_OF_MONTH, 1);
    maxDate.add(Calendar.YEAR, 1);

    List<CalendarEvent> eventList = new ArrayList<>();
    mockList(eventList);
    mAgendaCalendarView.init(eventList, minDate, maxDate, Locale.getDefault(), this);
return view;
}

private void mockList(List<CalendarEvent> eventList) {
    Calendar startTime1 = Calendar.getInstance();
    Calendar endTime1 = Calendar.getInstance();
    endTime1.add(Calendar.MONTH, 1);
    BaseCalendarEvent event1 = new BaseCalendarEvent("Thibault travels in Iceland", "A wonderful journey!", "Iceland",
            ContextCompat.getColor(getContext(), R.color.orange_dark), startTime1, endTime1, true);
    eventList.add(event1);

    Calendar startTime2 = Calendar.getInstance();
    startTime2.add(Calendar.DAY_OF_YEAR, 1);
    Calendar endTime2 = Calendar.getInstance();
    endTime2.add(Calendar.DAY_OF_YEAR, 3);
    BaseCalendarEvent event2 = new BaseCalendarEvent("Visit to Dalvík", "A beautiful small town", "Dalvík",
            ContextCompat.getColor(getContext(), R.color.yellow), startTime2, endTime2, true);
    eventList.add(event2);

    // Example on how to provide your own layout
    Calendar startTime3 = Calendar.getInstance();
    Calendar endTime3 = Calendar.getInstance();
    startTime3.set(Calendar.HOUR_OF_DAY, 14);
    startTime3.set(Calendar.MINUTE, 0);
    endTime3.set(Calendar.HOUR_OF_DAY, 15);
    endTime3.set(Calendar.MINUTE, 0);
    BaseCalendarEvent event3 = new BaseCalendarEvent("Visit of Harpa", "", "Dalvík",
            ContextCompat.getColor(getContext(), R.color.blue_dark), startTime3, endTime3, false);
    eventList.add(event3);
}

@Override
public void onEventSelected(CalendarEvent event) {
    Log.d(LOG_TAG, String.format("Selected day: %s", event));
}

@Override
public void onScrollToDate(Calendar calendar) {
   /* if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle(calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()));
    }*/
}

@Override
public void onDaySelected(DayItem dayItem) {
    Log.d(LOG_TAG, String.format("Selected day: %s", dayItem));
}

}

CreatureOX commented 6 years ago

I have the same problem. I have a fragment which contains in a Activity.I wanna show the fragment with AgendaCalendarView. But each time I click then the app crashed. So did you solve it? The following is my code.

`package com.creatureox.worldcup.fragment;

import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;

import com.creatureox.worldcup.R; import com.github.tibolte.agendacalendarview.AgendaCalendarView; import com.github.tibolte.agendacalendarview.CalendarPickerController; import com.github.tibolte.agendacalendarview.models.BaseCalendarEvent; import com.github.tibolte.agendacalendarview.models.CalendarEvent; import com.github.tibolte.agendacalendarview.models.DayItem;

import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Locale;

public class CalenderFragment extends Fragment implements CalendarPickerController{

AgendaCalendarView mAgendaCalendarView;
private View view;
private Context context;

public CalenderFragment() {
    // Required empty public constructor
}

public static CalenderFragment newInstance(){
    CalenderFragment homeFragment=new CalenderFragment();
    return homeFragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view  = inflater.inflate(R.layout.fragment_calender, container, false);
    Calendar minDate = Calendar.getInstance();
    Calendar maxDate = Calendar.getInstance();

    minDate.add(Calendar.MONTH, -2);
    minDate.set(Calendar.DAY_OF_MONTH, 1);
    maxDate.add(Calendar.YEAR, 1);

    List<CalendarEvent> eventList = new ArrayList<>();
    mockList(eventList);
    mAgendaCalendarView.init(eventList, minDate, maxDate, Locale.getDefault(), this);
    return view;
}

private void mockList(List<CalendarEvent> eventList) {
    Calendar startTime1 = Calendar.getInstance();
    Calendar endTime1 = Calendar.getInstance();
    endTime1.add(Calendar.MONTH, 1);
    BaseCalendarEvent event1 = new BaseCalendarEvent("Thibault travels in Iceland", "A wonderful journey!", "Iceland",
            ContextCompat.getColor(getContext(), R.color.orange_dark), startTime1, endTime1, true);
    eventList.add(event1);

    Calendar startTime2 = Calendar.getInstance();
    startTime2.add(Calendar.DAY_OF_YEAR, 1);
    Calendar endTime2 = Calendar.getInstance();
    endTime2.add(Calendar.DAY_OF_YEAR, 3);
    BaseCalendarEvent event2 = new BaseCalendarEvent("Visit to Dalvík", "A beautiful small town", "Dalvík",
            ContextCompat.getColor(getContext(), R.color.yellow), startTime2, endTime2, true);
    eventList.add(event2);
}

@Override
public void onDaySelected(DayItem dayItem) {

}

@Override
public void onEventSelected(CalendarEvent event) {

}

@Override
public void onScrollToDate(Calendar calendar) {

}

} `