0

我正在编写一个应用程序,它看起来像这样:

图片1

您如何看到顶部有 SeekBar,下面的 Horizo​​ntalScrollView 中有几个按钮。我想使用 SeekBar 来滚动按钮,现在这工作正常,但我无法关闭滚动视图(拖动屏幕)的默认方式。我在论坛上找到了类似的话题:

相似主题

但问题是,这种方式锁定了整个 ScrollView,我不能通过 seekBar 和默认方式使用它 - 也许我做错了什么(我在链接中做了同样的事情,即使我在 mScrollable 中有 TRUE 这也没有用范围)。第二个问题是我的按钮(具有 fill_parent 高度参数)非常小。

问题:有什么好的方法可以默认锁定滚动视图并使用 SeekBar 来做到这一点?

我的代码(默认 Horizo​​ntalScrollView):

 public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener {

    static private String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        SeekBar seek = (SeekBar) findViewById(R.id.seekBar);
        final HorizontalScrollView hsv = (HorizontalScrollView)findViewById(R.id.hsv);      

        seek.setMax(948);
        seek.setProgress(474);
        seek.setOnSeekBarChangeListener(this);
        hsv.post(new Runnable() {
            @Override
            public void run() {
                hsv.scrollTo(474, 0);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        HorizontalScrollView hsv = (HorizontalScrollView)findViewById(R.id.hsv);
        hsv.scrollTo(progress, 0);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        Log.d(TAG, "Tracking on");
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        Log.d(TAG, "Tracking off");

    }

活动主:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#0A0A0A"
    android:orientation="vertical" >

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <HorizontalScrollView
        android:id="@+id/hsv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

           <!-- BUTTONS CODE - NOT IMPORTANT --> 

        </RelativeLayout>
    </HorizontalScrollView>

</LinearLayout>
4

1 回答 1

1

有什么好的方法可以以默认方式锁定滚动视图并使用 SeekBar 来做到这一点?

编辑:

解决方案似乎要简单得多,重写HorizontalScrollView类并通过视图传递触摸事件:

public class MyHScroll extends HorizontalScrollView {

    // the constructors

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return false;
    }

}

在布局中使用此类而不是默认的HorizontallScrollView

<com.your.package.MyHScroll
    android:id="@+id/hsv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="none" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

       <!-- BUTTONS CODE - NOT IMPORTANT --> 

    </RelativeLayout>
</com.your.package.MyHScroll>


尝试在顶部放置一个覆盖视图HorizontalScrollView以“吸收”触摸事件,而不会弄乱它HorizontalScrollView本身:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#0A0A0A"
    android:orientation="vertical" >

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <FrameLayout android:layout_width="wrap_content"
        android:layout_height="fill_parent">

    <HorizontalScrollView
        android:id="@+id/hsv"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:scrollbars="none" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

           <!-- BUTTONS CODE - NOT IMPORTANT --> 

        </RelativeLayout>
    </HorizontalScrollView>

    <View android:id="@+id/overlay" android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    </FrameLayout

</LinearLayout>

然后OnTouchListener在方法中为覆盖小部件添加一个onCreate以进一步阻止触摸事件:

findViewById(R.id.overlay).setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });

第二个问题是我的按钮(具有 fill_parent 高度参数)非常小。

请参阅上面的布局文件。

于 2012-12-09T16:55:49.610 回答