【问题标题】:Implementing ViewPager in custom calendar在自定义日历中实现 ViewPager
【发布时间】:2012-09-09 22:29:34
【问题描述】:

我有一个带有 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);
        }
    }
..

}

Calendar fragment 中,我想为月份视图实现 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;
    }

但不会发生滑动。我哪里错了?

【问题讨论】:

  • NUM_AWESOME_VIEWS 的值是多少?
  • private static int NUM_AWESOME_VIEWS = 100;
  • 这个问题你解决了吗?
  • @JasonHessley,没有。我还在寻找答案。
  • 这样做:Log.d("Object", "Instantiated");出现在您的调试日志中?

标签: java android calendar android-viewpager fragment


【解决方案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 的方式是在视图之间保持其状态。

【讨论】:

  • 但我需要实现onClickListener 以在点击日期时显示对话框。
  • 如果删除它可以解决滑动问题,那么这个问题就解决了。为了帮助您让他们一起工作,我们需要查看相关代码。
  • 不幸的是,它没有修复。
  • 非常感谢!这至少让我去某个地方。
  • 这行得通,但它也增加了月份的位置。这意味着如果它位于位置 2,它将增加 2 个月的日期。所以当我滚动时,我会得到 10 月、11 月、1 月、4 月。怎么了?
【解决方案2】:

我被困在这里,因为我不知道如何在用户向左滑动时显示上个月日历,当用户向右滑动时显示下个月日历。

假设GridCellAdapter 编写正确,这应该会自动发生。 instantiateItem() 将为每个职位调用,您可以在其中创建一个适合该职位相关的任何月份的 GridCellAdapter,然后将 GridCellAdapter 与您从 instantiateItem() 返回的 View 一起使用。

【讨论】:

猜你喜欢
  • 2015-10-02
  • 2021-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多