1

我在滚动和在正确位置拾取触摸信号时遇到了很多问题。

基本上应用程序是:

ViewPager ->
Fragment ->
ScrollView -> 
LinearLayout -> 
Lots of dynamically added customer View objects

片段 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/transaction_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/add_new_transaction"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/AddNewTransaction" />

<com.company.application.TransactionScroller
    android:id="@+id/transaction_scroller"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <com.company.application.TransactionGroup
        android:id="@+id/transaction_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill"
        android:orientation="vertical" >

    </com.company.application.TransactionGroup>

</com.company.application.TransactionScroller>
</LinearLayout>

其中TransactionScroller 是ScrollView,TransactionGroup 是LinearLayout。视图对象是自定义绘制的,并执行以下操作:

@Override
protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec )
{
    // Try for a width based on our minimum
    setMeasuredDimension( width, height ) ;
}

@Override
public boolean onTouchEvent( MotionEvent event )
{
   Toast.makeText( getContext(), "Grid", Toast.LENGTH_SHORT ).show() ;

   return true ;
}

public void draw( Canvas canvas )
{
   if( tmpBmp.isRecycled() || 
       tmpBmp.getWidth()!=canvas.getWidth() ||
       tmpBmp.getHeight()!=canvas.getHeight() )
    {
        tmpBmp.recycle();
        tmpBmp = Bitmap.createBitmap( canvas.getWidth(), canvas.getHeight(), 
                                      Config.ARGB_8888 ) ;
        c.setBitmap( tmpBmp ) ;
    }

    //clear previous drawings
    c.drawColor( Color.TRANSPARENT, Mode.CLEAR ) ;

    // Draw the background
    c.drawRect( spacing, top, width - spacing , bottom, newpaint ) ;

    // Draw the lines
   for( Line line : lineList )
   {  
      c.drawLine( line.x1, line.y1, line.x2, line.y2, paint ) ;
   }

   canvas.drawBitmap( tmpBmp, 0, 0, null ) ;

   canvas.drawText( String.valueOf( amount ), left, top + ( bottom - top ) /2 , paint);

   canvas.drawBitmap( 
      ( ( BitmapDrawable )MainActivity.iconList[ index ].getDrawable() ).getBitmap(),
      left + MainActivity.iconList[ index ].getWidth(),
      top + ( bottom - top ) /2, paint ) ;
}

1)首先,我试图在自定义视图对象中捕获触摸和拖动事件。但是我只能捕获ScrollView中的事件,LinearLayout中只有Down事件,自定义View中什么都没有。

在滚动视图中:

setOnTouchListener( new OnTouchListener()
                       {   
                          @Override
                          public boolean onTouch( View view, MotionEvent event )
                          {
                             // Only called when touched outside the ScrollView

                             if( event.getAction() ==
                                 android.view.MotionEvent.ACTION_DOWN )
                             {
                                Toast.makeText( getContext(),
                                                "S_Down",
                                                Toast.LENGTH_SHORT ).show() ;

                                return false ;
                             }
                             else if( event.getAction() ==
                                      android.view.MotionEvent.ACTION_UP )
                             {
                                Toast.makeText( getContext(),
                                                "S_Up",
                                                Toast.LENGTH_SHORT ).show() ;

                                return false ;
                             }

                             return false;
                          }
                       } ) ;

两者都在 ScrollView 中被调用。但是在 LinearLayout 中也是一样的,只有 Down 事件被捕获而在自定义视图中,什么都没有被捕获。我试过设置可聚焦、可点击等。

有任何想法吗?

2)其次,当 ScrollView 只有我的自定义 View 对象时,它不会滚动。但是当填充其他东西时它会这样做,例如按钮。正如您在上面看到的,我正在使用 onMeasure(),并且我尝试对这些值进行硬编码但无济于事。

提前致谢。杰克

4

0 回答 0