1

2つのタブがあるFragmentActivityがあります。各タブはフラグメントを呼び出します-カレンダーとコンバーター:

public class Calendar extends SherlockFragmentActivity {

//create tabs
}


private class MyTabListener implements ActionBar.TabListener
{
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if(tab.getPosition()==0)
        {
            CalendarFragment frag = new CalendarFragment();
            ft.replace(android.R.id.content, frag);
        }
        else
        {
            ConverterFragment frag = new ConverterFragment();
            ft.replace(android.R.id.content, frag);
        }
    }
..

}

カレンダーフラグメントで、月ビューにViewPagerを実装して、ユーザーが月を変更するときに次/前にスワイプできるようにします。

これは私が今まで持っているコードです。しかし、これはすべて、現在の月のカレンダーを表示することです。

public class CalendarFragment extends Fragment implements OnClickListener {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View v =  inflater.inflate(R.layout.simple_calendar_view, container, false);

       //set calendar adapter 
       //Code from http://w2davids.wordpress.com/android-simple-calendar/

       context = this.getActivity().getApplicationContext();
       CalendarPagerAdapter adapter = new CalendarPagerAdapter();
       myPager = (ViewPager) v.findViewById(R.id.pager);
       myPager.setAdapter(adapter);
       myPager.setCurrentItem(1);
    }


   public class CalendarPagerAdapter extends PagerAdapter {

         public int getCount() {
                 return NUM_AWESOME_VIEWS;
         }

         public Object instantiateItem(View collection, int position) {
                 Log.d("Object", "Instantiated");
                LayoutInflater inflater = (LayoutInflater) collection.getContext()
                                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                View v =  inflater.inflate(R.layout.simple_calendar_view, null);

                _calendar = Calendar.getInstance(Locale.getDefault());
                month = _calendar.get(Calendar.MONTH) + 1;
                year = _calendar.get(Calendar.YEAR);

                calendarView = (GridView) v.findViewById(R.id.calendar);
                calendarWeek = (GridView) v.findViewById(R.id.calendarweek);

                calendarWeek.setAdapter(new CalendarWeekAdapter(context));
                // Initialised
                adapter = new GridCellAdapter(context, R.id.calendar_day_gridcell, month, year);
                adapter.notifyDataSetChanged();
                calendarView.setAdapter(adapter);

                ((ViewPager) collection).addView(v, 0);

                return v;
         }

         @Override
         public void destroyItem(View arg0, int arg1, Object arg2) {
                 ((ViewPager) arg0).removeView((View) arg2);

         }

         @Override
         public void finishUpdate(View arg0) {
                 // TODO Auto-generated method stub

         }

         @Override
         public boolean isViewFromObject(View arg0, Object arg1) {
                 return arg0 == ((View) arg1);

         }

         @Override
         public void restoreState(Parcelable arg0, ClassLoader arg1) {
                 // TODO Auto-generated method stub

         }

         @Override
         public Parcelable saveState() {
                 // TODO Auto-generated method stub
                 return null;
         }

         @Override
         public void startUpdate(View arg0) {
                 // TODO Auto-generated method stub

         }

     }

}

ユーザーが左にスワイプしたときに前の月のカレンダーを表示し、ユーザーが右にスワイプしたときに次の月のカレンダーを表示する方法がわからないため、ここで立ち往生しています。

私はこのコードをに追加しようとしましたObject instantiateItem

 switch(position){
    case 0:
       Log.d("ViewPager", "Prev");
       if (month <= 1)
            {
                month = 12;
                year--;
            }
        else
            {
                month--;
            }
        if (hmonth <= 1)
            {
                hmonth = 12;
                hyear--;
            }
        else
            {
                hmonth--;
            }
        setGridCellAdapterToDate(month, year);
    break;
    case 2:

       Log.d("ViewPager", "Next");
       if (month > 11)
            {
                month = 1;
                year++;
            }
        else
            {
                month++;
            }
        if (hmonth > 11)
            {
                hmonth = 1;
                hyear++;
            }
        else
            {
                hmonth++;
            }
        setGridCellAdapterToDate(month, year);

    break;
    }

ただし、スワイプは発生しません。どこが間違っているのですか?

4

2 に答える 2

1

タッチイベントリスナーがうまく一緒にプレイできない場合があります。CalendarFragmentにonClickListenrを実装すると、PagerAdapterからタッチイベントが盗まれる可能性があります。

- - - 編集 - - - -

Log.d( "Object"、 "Instantiated");の場合 スワイプするたびにデバッグログにが表示され、PagerAdapterが機能しています。これは、表示する現在の月をどのように変更するかという問題があることを意味します。

これを試して:

// put this at the class level of calendarFragment.
Calendar holdTodaysDate = Calendar.getInstance(Locale.getDefault());


_calendar = Calendar.getInstance(Locale.getDefault());
_calendar = holdTodaysDate; // Add this line;
_calendar.add(Calendar.MONTH, position); // Add this line
month = _calendar.get(Calendar.MONTH) + 1;
year = _calendar.get(Calendar.YEAR);

これは、カレンダーに位置を追加する前に、カレンダーを今日の日付にリセットするためのものです。現在の_calendarは、ビュー間の状態を維持しています。

于 2012-10-20T08:22:35.077 に答える
0

ユーザーが左にスワイプしたときに前の月のカレンダーを表示し、右にスワイプしたときに翌月のカレンダーを表示する方法がわからないため、ここで立ち往生しています。

適切に記述されていると仮定するとGridCellAdapter、これは自動的に行われるはずです。instantiateItem()は各ポジションに対して呼び出され、GridCellAdapterそのポジションに関連する月に適した を作成し、それを元の で使用GridCellAdapterViewますinstantiateItem()

于 2012-09-09T23:13:41.097 に答える