画廊失去了焦点

时间:2011-12-13 15:17:49

标签: android focus gallery

我写了一个代码来升级我的画廊中的所选图像,我不知道为什么每次我滚动,画廊失去焦点,这是我的代码。

gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View v,
                int position, long id) {

            if (lasview != null)
                lasview
                        .setLayoutParams(new Gallery.LayoutParams(130, 195));
            lasview = v;
            v.setLayoutParams(new Gallery.LayoutParams(170, 230));

        }

        public void onNothingSelected(AdapterView<?> parent) {
            // System.out.println("NOTHING SELECTED");

        }
    });

2 个答案:

答案 0 :(得分:1)

尝试lasview.getLayoutParams(),然后在检索到的LayoutParams上设置值 调用setLayoutParams()会导致重绘/刷新,这可能会导致您的问题。

答案 1 :(得分:0)

我这样解决了这个问题。

     <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="50"
        android:nextFocusDown="@+id/gallery1"
        android:nextFocusLeft="@+id/gallery1"
        android:nextFocusRight="@+id/gallery1"
        android:nextFocusUp="@+id/gallery1" 
        android:focusable="false"
        android:clickable="false"
        android:focusableInTouchMode="false"
        android:descendantFocusability="afterDescendants"/>

在活动中

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {

        gl.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);

        return true;
    }
    if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {

        gl.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);

        return true;
    }
            return false;

}

它对我有用。