包含带自定义视图的LinearLayout的Android自定义ScrollView

时间:2013-04-05 13:41:49

标签: android android-layout

我遇到很多问题,包括滚动,以及在正确的位置拾取触摸信号。

基本上应用程序是:

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)首先,我试图在自定义View对象中捕获触摸和拖动事件。但我只能捕获ScrollView中的事件,只能捕获LinearLayout中的Down事件,而自定义视图中没有任何内容。

在ScrolView中:

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(),我尝试过对这些值进行硬编码无济于事。

提前致谢。 千斤顶

0 个答案:

没有答案
相关问题