roomorama / Caldroid

A better calendar for Android
Other
1.42k stars 532 forks source link

Caldroid with default PageViewer Fragments #115

Closed PietroAvolio closed 10 years ago

PietroAvolio commented 10 years ago

Hi, I'd like to use a caldroid embedded fragment in my app, in the second tab of a viewpager that already contains a fragment in the first tab.

I'm getting a lot of errors and i don't know how to move to fix that but i really want to use this calendar.

Here there's a paste of my actual code:

package it.dreamerspillow.ediary;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

@SuppressLint("SimpleDateFormat")
public class Diary extends FragmentActivity implements ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    private static DatabaseManager dbManager;

    public static long ONE_DAY_MILLS = 86400000;
    public static long ONE_HOUR_MILLS = 3600000;
    public static long ONE_MINUTE_MILLS = 60000;

    private static boolean refreshWeekView = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diary);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                            actionBar.setSelectedNavigationItem(position);
                    }
                });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }

        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
        this.setTitle(getResources().getString(R.string.Diary) + " " + sharedPref.getString("config_classname", null));

        dbManager = new DatabaseManager(this.getApplicationContext());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.diary, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_timeSchedule) {
            Intent newActivity = new Intent(this.getApplicationContext(), TimeSchedule.class);
            newActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            this.startActivity(newActivity);
            refreshWeekView = true;
            return true;
        }
        if(id == R.id.ab_add){
            Intent newActivity = new Intent(this.getApplicationContext(), TaskAdd.class);
            newActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            refreshWeekView = true;
            this.startActivity(newActivity);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return getString(R.string.WeekView).toUpperCase(l);
            case 1:
                return getString(R.string.MonthView).toUpperCase(l);
            }
            return null;
        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";
        private static ArrayList<Long> giorniConCompiti;
        private static DiaryWeekViewListAdapter elvAdapter;

        /**
         * Returns a new instance of this fragment for the given section number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);

            giorniConCompiti = new ArrayList<Long>();

            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public void onPause(){
            super.onPause();
            if(getArguments().getInt(ARG_SECTION_NUMBER) == 1){
                refreshWeekView = true;
            }
        }

        @Override
        public void onResume(){
            super.onResume();

            if(getArguments().getInt(ARG_SECTION_NUMBER) == 1 && refreshWeekView){
                ExpandableListView lView = (ExpandableListView) this.getView().findViewById(R.id.expandableListView1);
                giorniConCompiti.clear();
                this.caricaGiorniConCompiti();
                elvAdapter = new DiaryWeekViewListAdapter(this.getActivity(), giorniConCompiti);
                lView.setAdapter(elvAdapter);

                refreshWeekView = false;
            }
        }

        public void caricaGiorniConCompiti(){
            giorniConCompiti.clear();
            for(int i = 1; i <= 7; i++){
                ArrayList<Compito >compiti = dbManager.caricaCompiti(getMillis(0, 0)+(ONE_DAY_MILLS*i), getMillis(23, 59)+(ONE_DAY_MILLS*i));
                if(compiti.size() != 0){
                    giorniConCompiti.add(getMillis(8, 0)+(ONE_DAY_MILLS*i));
                }

            }
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView;

            if(getArguments().getInt(ARG_SECTION_NUMBER) == 1){ 
                rootView = inflater.inflate(R.layout.week_view, container, false);

                this.caricaGiorniConCompiti(); //RIEMPIE L'ARRAYLIST giorniConCompiti

                final ExpandableListView lView = (ExpandableListView) rootView.findViewById(R.id.expandableListView1);
                elvAdapter = new DiaryWeekViewListAdapter(this.getActivity(), giorniConCompiti);
                lView.setAdapter(elvAdapter);

            }else{
                rootView = inflater.inflate(R.layout.acaso, container, false);              
            }
            return rootView;
        }
    }   

    public static long getMillis(int startHour, int startMinute) {
        Calendar c = Calendar.getInstance(TimeZone.getDefault());
        c.set(Calendar.HOUR_OF_DAY,startHour);
        c.set(Calendar.MINUTE,startMinute);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.MILLISECOND,0);

        return c.getTime().getTime();
    }

    public static String unixToString(Long timestamp, String format){
        SimpleDateFormat formatter = new SimpleDateFormat(format); //"EEEE, dd MMMM"
        String dateString = formatter.format(new Date(timestamp));

        return dateString.substring(0, 1).toUpperCase(Locale.getDefault()) + dateString.substring(1);
    }

    public static int dpsToPixels(Context ctx, int dps){
        final float scale = ctx.getResources().getDisplayMetrics().density;
        int pixels = (int) (dps * scale + 0.5f);

        return pixels;
    }
}

This is the default code generated when you create a new activity in ecplise. I'm new to android so sorry if this is a dumb question.

thomasdao commented 10 years ago

Your question may get better answers on Stackoverflow, please keep this issues tracker for Caldroid bugs only, thanks