2011-04-12 67 views
0

画廊捕捉轨迹球/ dpad导航水平。画廊捕捉轨迹球导航

下面是一个例子布局按钮上的画廊两侧:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <Gallery 
     android:layout_height="match_parent" 
     android:id="@+id/gallery" 
     android:layout_width="match_parent" 
     android:layout_weight="1" /> 
    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

当您导航到你无法逃避到两边的按钮轨迹球画廊。我试图在画廊中添加android:nextFocusLeft =“@ id/button”。我也尝试将其添加到画廊的适配器中的第一个视图。

有没有办法解决这个问题?

回答

1

我想通了,这是一个图库中的错误。解决方法是扩展Gallery,如下所示:

public class DpadableGallery extends Gallery { 

    public DpadableGallery(Context context) { 
     super(context); 
    } 
    public DpadableGallery(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     switch (keyCode) { 
     case KeyEvent.KEYCODE_DPAD_LEFT: 
      if (getSelectedItemPosition()==0) { 
       return false; 
      } 
      break; 
     case KeyEvent.KEYCODE_DPAD_RIGHT: 
      if (getSelectedItemPosition()==(getAdapter().getCount()-1)) { 
       return false; 
      } 
     } 

     return super.onKeyDown(keyCode, event); 
    } 

}