Android - Gallery.onItemSelected()会导致滚动捕捉

时间:2011-12-01 20:40:37

标签: android

我有一个带有箭头指示器的图库,可以显示左侧或右侧是否有图像。

      <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/black"
        android:layout_weight="1" >
          <com.package.views.MyGallery
            android:id="@+id/carGallery"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:spacing="2dip"
            android:fadingEdge="none" />
          <ImageView
            android:id="@+id/prev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:src="@drawable/left_orange_arrow"
            android:paddingLeft="25dip" />
          <ImageView
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:src="@drawable/right_orange_arrow"
            android:paddingRight="25dip" />
      </RelativeLayout>

我这样更新箭头:

carGallery.setOnItemSelectedListener(new OnItemSelectedListener(){
    @Override
    public void onItemSelected(AdapterView<?> adapter, View view, final int position, long id) {
        Car car = carAdapter.getCar(position);
        top.setText(car.nickname);
        bottom.setText(car.year+" "+car.make+" "+car.model);

        if(carAdapter.getItems().size() == 1) {
            prev.setVisibility(View.GONE);
            next.setVisibility(View.GONE);
        }
        else if(position == 0) {
            prev.setVisibility(View.GONE);
            next.setVisibility(View.VISIBLE);
        }
        else if(position == carAdapter.getItems().size()-1) {
            prev.setVisibility(View.VISIBLE);
            next.setVisibility(View.GONE);
        }
        else {
            prev.setVisibility(View.VISIBLE);
            next.setVisibility(View.VISIBLE);
        }
    }
});

每当我更改箭头的可见性时,图库都会捕捉到刚刚滚动到选区的项目。我知道Gallery.onItemSelected()导致了捕捉,因为当我执行Gallery.setCallbackDuringFling(false)时,它不会在fling上执行捕捉,直到选择了最终项目。

如何阻止Gallery像这样对齐?

提前致谢!

1 个答案:

答案 0 :(得分:1)