2

我是安卓新手。我为我的应用程序开发了一个日历,我希望用户能够在月份之间顺利切换。所以我决定使用 ViewPager 并让它使用无限的几个月,我已经实施了这个解决方案:

http://code.google.com/p/electricsleep/source/browse/trunk/src/com/androsz/electricsleepbeta/app/HistoryMonthActivity.java

不幸的是,它对我不起作用。当我尝试向左或向右滚动时,下一个要显示的月份到来,直到我用手指滚动,但是当我松开手指时,较早显示的月份又来了,但指示器上的日期仍然是正确的。

我发布代码:

            calendarView = new GridView(this);
            calendarView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            calendarView.setNumColumns(7);
            adapter = new GridCellAdapter(getApplicationContext(), R.id.calendar_day_gridcell, newdatum);
            adapter.notifyDataSetChanged();
            calendarView.setAdapter(adapter);
            calendarView.setTag(newdatum.toString());

            viewpager = (ViewPager) this.findViewById(R.id.calviewpager);
            viewpager.setAdapter(new MonthPagerAdapter());
            viewpager.setOffscreenPageLimit(3);
            mTitleIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
            mTitleIndicator.setViewPager(viewpager, 1);
            mTitleIndicator.setOnPageChangeListener(new PageListener());
            }

    private class PageListener implements OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_IDLE) {

                        final DateTime old0 = DateTime.parse(viewpager.getChildAt(0).getTag().toString());
                        final DateTime old1 = DateTime.parse(viewpager.getChildAt(1).getTag().toString());
                        final DateTime old2 = DateTime.parse(viewpager.getChildAt(2).getTag().toString());

                        if (focusedPage == 0) {
                            Log.d("FOCUSEDPAGE=0",newdatum.toString());
                            DateTime minus = new DateTime();
                            minus=old0.minusMonths(1);
                            viewpager.getChildAt(0).setTag(minus.toString());
                            viewpager.getChildAt(1).setTag(old2.toString());
                            viewpager.getChildAt(2).setTag(old1.toString());
                            } 

                        else if (focusedPage == 2) {
                            Log.d("FOCUSEDPAGE=2",newdatum.toString());
                            DateTime plus = new DateTime();
                            plus=old2.plusMonths(1);
                            viewpager.getChildAt(0).setTag(old1.toString());
                            viewpager.getChildAt(1).setTag(old2.toString());
                            viewpager.getChildAt(2).setTag(plus.toString());
                        }
                        mTitleIndicator.setCurrentItem(1);
                }
        }

        @Override
        public void onPageSelected(int position) {
            newdatum=DateTime.parse(viewpager.getChildAt(position).getTag().toString());
            focusedPage = position;
        }

    }


     private class MonthPagerAdapter extends PagerAdapter{

         ViewPager container;

         private GridView addMonthViewAt(int position, DateTime date) {
             final GridView gv = new GridView(CalendarActivity.this);
             gv.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
             gv.setNumColumns(7);
             gv.setTag(date.toString());
             setGridCellAdapterToDate(date, gv);
             container.addView(gv, position);
             return gv;
             }

         @Override
         public void destroyItem(View container, int position, Object object) {
                 ((ViewPager) container).removeViewAt(position);
         }

         @Override
         public void finishUpdate(View container) {
         }

         @Override
         public int getCount() {
                 return 3;
         }

         @Override
         public Object instantiateItem(View container, int pos) {
             this.container = (ViewPager) container;
             DateTime newdate=newdatum.plusMonths(pos-1);
             Log.d("INSTATIATE",newdate.toString());
             return addMonthViewAt(pos, newdate);
         }

         @Override
         public String getPageTitle(int position) {
                 final GridView gv = (GridView) container.getChildAt(position);
                 DateTime dt=DateTime.parse(gv.getTag().toString());
                 String title=dt.monthOfYear().getAsText()+" "+dt.getYear();
                 Log.d("GET PAGE TITLE",dt.toString());
                 return title;
         }

         @Override
         public boolean isViewFromObject(View view, Object object) {
                 return view.equals(object);
         }

         @Override
         public void restoreState(Parcelable state, ClassLoader loader) {
         }

         @Override
         public Parcelable saveState() {
                 return null;
         }

         @Override
         public void startUpdate(View container) {
         }

     }


    private void setGridCellAdapterToDate(DateTime newdate, GridView gridview){
        GridCellAdapter adapt = new GridCellAdapter(getApplicationContext(), R.id.calendar_day_gridcell, newdate);
        adapt.notifyDataSetChanged();
        gridview.setAdapter(adapt);
        }

以及 GridCellAdapter 代码:

 public class GridCellAdapter extends BaseAdapter implements OnClickListener{
            private final Context _context;
            private final List<String> list;
            private Button gridcell;

            public GridCellAdapter(Context context, int textViewResourceId, DateTime datum){
                super();
                this._context = context;
                this.list = new ArrayList<String>();
                printMonth(datum);
                }

            public String getItem(int position){
                return list.get(position);
                }


            public int getCount(){
                return list.size();
                }

            private void printMonth(DateTime cal){
                    int daysInMonth = cal.dayOfMonth().getMaximumValue();
                    int daysInPrevMonth = cal.minusMonths(1).dayOfMonth().getMaximumValue()+1;
                    int trailingSpaces = cal.withDayOfMonth(1).dayOfWeek().get()-1;
                    SQLiteAdapter sql= new SQLiteAdapter(getApplicationContext());
                    sql.openToWrite(month(cal));
                    if(sql.tableExists()){
                        String back="blue";
                        for(int i=0;i<7;i++)
                            list.add("DAY:"+cal.withDayOfWeek(i+1).toString()+": ");
                        for (int i = 0; i < trailingSpaces; i++){
                            if(sql.rowExists(cal.minusMonths(1).withDayOfMonth((daysInPrevMonth-trailingSpaces)+i).toString("yyyy-MM-dd")))
                                back="green";
                            else back="blue";
                            list.add("GREY:"+cal.minusMonths(1).withDayOfMonth((daysInPrevMonth-trailingSpaces)+i).toString("yyyy-MM-dd")+": "+":"+back);
                            }
                        for (int i = 1; i <= daysInMonth; i++){
                            if(sql.rowExists(cal.withDayOfMonth(i).toString("yyyy-MM-dd")))
                                back="green";
                            else back="blue";
                                if (cal.withDayOfMonth(i).toString("yyyy-MM-dd").equals(DateTime.now().toString("yyyy-MM-dd"))){
                                    list.add("WHITE:"+cal.withDayOfMonth(i).toString("yyyy-MM-dd")+":"+checkIsFeiertag(cal.withDayOfMonth(i)).toString()+":"+back);
                                    }
                                else{
                                    checkIsFeiertag(cal.withDayOfMonth(i));
                                    list.add("BLUE:"+cal.withDayOfMonth(i).toString("yyyy-MM-dd")+":"+checkIsFeiertag(cal.withDayOfMonth(i)).toString()+":"+back);
                                    }
                            }

                        for (int i = 0; i < list.size() % 7; i++){
                            if(sql.rowExists(cal.plusMonths(1).withDayOfMonth(i+1).toString("yyyy-MM-dd")))
                                back="green";
                            else back="blue";
                            list.add("GREY:"+cal.plusMonths(1).withDayOfMonth(i+1).toString("yyyy-MM-dd")+": "+":"+back);
                            }
                    }
                    else{
                        for(int i=0;i<7;i++)
                            list.add("DAY:"+cal.withDayOfWeek(i+1).toString()+": "+": ");
                        for (int i = 0; i < trailingSpaces; i++){
                            list.add("GREY:"+cal.minusMonths(1).withDayOfMonth((daysInPrevMonth-trailingSpaces)+i).toString("yyyy-MM-dd")+": "+": ");
                            }
                        for (int i = 1; i <= daysInMonth; i++){
                                if (cal.withDayOfMonth(i).toString("yyyy-MM-dd").equals(DateTime.now().toString("yyyy-MM-dd"))){
                                    list.add("WHITE:"+cal.withDayOfMonth(i).toString("yyyy-MM-dd")+":"+checkIsFeiertag(cal.withDayOfMonth(i)).toString()+": ");
                                    }
                                else{
                                    checkIsFeiertag(cal.withDayOfMonth(i));
                                    list.add("BLUE:"+cal.withDayOfMonth(i).toString("yyyy-MM-dd")+":"+checkIsFeiertag(cal.withDayOfMonth(i)).toString()+": ");
                                    }
                            }

                        for (int i = 0; i < list.size() % 7; i++){
                            list.add("GREY:"+cal.plusMonths(1).withDayOfMonth(i+1).toString("yyyy-MM-dd")+": "+": ");
                            }
                    }
                    sql.close();
                }


            public long getItemId(int position){
                return position;
                }

            @SuppressLint("NewApi")
            public View getView(int position, View convertView, ViewGroup parent){
                    View row = convertView;
                    if(row == null){
                        LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        row = inflater.inflate(R.layout.calendar_day_gridcell, parent, false);
                        }

                    gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
                    String[] day_color = list.get(position).split(":");
                    DateTime datum = DateTime.parse(day_color[1]);
                    if(!(day_color[0].equals("DAY"))){
                        gridcell.setText(String.valueOf(datum.getDayOfMonth()));
                        gridcell.setTag(datum.toString("yyyy-MM-dd"));
                        gridcell.setOnClickListener(this);
                    if (!day_color[2].equals(" "))
                        gridcell.setTextColor(Color.RED);
                    else if (day_color[0].equals("GREY"))
                        {
                            gridcell.setTextColor(Color.LTGRAY);
                        }
                    else if (day_color[0].equals("WHITE"))
                        {
                            gridcell.setTextColor(Color.WHITE);
                        }
                    else if (day_color[0].equals("BLUE"))
                        {
                            gridcell.setTextColor(getResources().getColor(R.color.static_text_color));
                        }
                    if(day_color[3].equals("green")){
                            int sdk = android.os.Build.VERSION.SDK_INT;
                            if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                                gridcell.setBackgroundDrawable(getResources().getDrawable(R.drawable.calendar_tile_green));
                            } else {
                                gridcell.setBackground(getResources().getDrawable(R.drawable.calendar_tile_green));
                            }
                            }
                    }                               
                    else {
                        gridcell.setText(datum.toString("EE"));
                        gridcell.setClickable(false);
                    }
                    gridcell.setOnLongClickListener(new View.OnLongClickListener() {
                        public boolean onLongClick(View v) {
                            if(gridcell.getBackground()==getResources().getDrawable(R.drawable.calendar_tile_green)){
                                SQLiteAdapter sql= new SQLiteAdapter(context);
                                DateTime bt = DateTime.parse(gridcell.getTag().toString());
                                sql.openToWrite(month(bt));
                                sql.getRows(month(bt));
                                sql.close();
                            }
                            return true;
                        }
                    });
                    return row;
                }

当然,我接受不同的解决方案来提供相同的平滑滚动。

4

0 回答 0