java 我怎么能用android左右水平滑动

bn31dyow  于 2022-12-17  发布在  Java
关注(0)|答案(2)|浏览(116)

我是android新手,我正在做一个日历应用程序来练习。它快完成了,但有一件事我不能开始工作。我想从左向右水平滑动来浏览月份。我一直在用这段代码尝试它:

public boolean onTouchEvent(MotionEvent event)
     {     
         switch(event.getAction())
         {
           case MotionEvent.ACTION_DOWN:
               x1 = event.getX();                         
           break;         
           case MotionEvent.ACTION_UP:
               x2 = event.getX();
               float deltaX = x2 - x1;
               if (Math.abs(deltaX) > MIN_DISTANCE)
               {
                    month++; 
                    Log.v("test", "test");
                    theCursor = dbh.getDiaries(month);
                    adapter = new MyCursorAdapter(this, theCursor);
                    this.setListAdapter(adapter);
               }
               else
               {
                    month--; 
                    Log.v("test", "test");
                    theCursor = dbh.getDiaries(month);
                    adapter = new MyCursorAdapter(this, theCursor);
                    this.setListAdapter(adapter);
               }                          
           break;   
         }           
         return super.onTouchEvent(event);       
     }

这只能从上到下滑动,而不是回来,有人能帮我修复这个代码,这样我就可以从左到右和回来滑动?
谢谢!

roejwanj

roejwanj1#

在您的xml:

<ScrollView
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:fillViewport="true" >

    <--! Your xml -->

</ScrollView>
9avjhtql

9avjhtql2#

我知道这是很晚的答复,但它可能会帮助别人。请检查下面更新的代码。

public boolean onTouchEvent(MotionEvent event)
 {     
     switch(event.getAction())
     {
       case MotionEvent.ACTION_DOWN:
           x1 = event.getX();                         
       break;         
       case MotionEvent.ACTION_UP:
           x2 = event.getX();
           float deltaX = x2 - x1;
           if (Math.abs(deltaX) > MIN_DISTANCE)
           {
             if (deltaX > 0) {
                month++; 
                Log.v("test", "test");
                theCursor = dbh.getDiaries(month);
                adapter = new MyCursorAdapter(this, theCursor);
                this.setListAdapter(adapter);
             } else {
                month--; 
                Log.v("test", "test");
                theCursor = dbh.getDiaries(month);
                adapter = new MyCursorAdapter(this, theCursor);
                this.setListAdapter(adapter);
             }
           }                          
       break;   
     }           
     return super.onTouchEvent(event);       
 }

相关问题