android - 修复 ScrollView 上的一个元素

标签 android scrollview

我想做类似 sticky menu 的事情,但我对此一无所知。我的 XML 是这样的:

<com.example.customscrollview.scrollview
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <!-- I need sticky this element bellow! -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="210.0dip"
            android:orientation="vertical">
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="210.0dip">
            <!-- A lot of TextViews -->
            <TextView...>
            <TextView...>
            <TextView...>
        </LinearLayout>
    </FrameLayout>
</com.example.customscrollview.scrollview>

我尝试以编程方式进行:

private void setTouchListeners() {
    setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            boolean rt = false;
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                final View element = (View) findViewById(R.id.element);
                LayoutParams params = new LayoutParams(
                        LayoutParams.MATCH_PARENT,
                        element.getHeight()
                );
                params.setMargins(0, (int) getScrollY(), 0, 0);
                params.gravity = Gravity.TOP;
                element.setLayoutParams(params);
                Handler handler = new Handler();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        childElement.requestLayout();
                    }
                });
            }
            return rt;
        }
    });
}

它有效但并不完美。当 ScrollView 开始滑动时,移动尚未完成,因为 MotionEvent 仅 Hook 触摸事件。因此,我覆盖了 onScrollChanged 以执行相同的功能(有一些更改)。很好,现在元素停留在顶部,但它不跟随滚动运动,它变得“不稳定”直到滚动停止(快速滚动和快速滚动的结果)。

更好的方法是什么?

最佳答案

您可以通过更改嵌套最轻松地到达那里。如果使 FrameLayout 成为根元素,则可以将叠加层 float 在 ScrollView 的顶部。您付出的代价是它在布局中出现两次。如果观看次数不是太多,那可能是值得的。

一旦滚动导航到达 View 顶部,您需要编程控制来更改粘性导航的可见性。您不需要编程控制来保持粘性导航就位。

<!-- FrameLayout simply positions all its children at the requested height, width, 
     gravity and margins. It doesn't try to avoid child views overlapping each
     other. The items are rendered in top-to-bottom XML order. -->
<FrameLayout layout_width="match_parent" 
             layout_height="match_parent">

    <!-- The scrollable main content. -->
    <ScrollView layout_width="match_parent" 
                layout_height="match_parent">
        <LinearLayout layout_width="match_parent"
                      layout_height="wrap_content"
                      orientation="vertical">

            <!-- The scrolling version of the navigation section -->
            <LinearLayout layout_width="match_parent" 
                          layout_height="wrap_content"
                          orientation="horizontal">
                <!-- menu items go here -->
            </LinearLayout>

            <!-- content views go here. Note: if you're doing data binding to
                 populate this, consider RecyclerView instead of ScrollView. -->
            <TextView />
            <TextView />

        </LinearLayout>
    </ScrollView>

    <!-- This is a copy of the navigation section. -->
    <LinearLayout layout_width="match_parent" 
                  layout_height="wrap_content"
                  layout_gravity="top" 
                  visibility="invisible"
                  orientation="horizontal">
        <!-- menu items go here -->
    </LinearLayout>
</FrameLayout>

(我在示例中省略了 android: 前缀。您确实需要它们。)

关于android - 修复 ScrollView 上的一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30416829/

相关文章:

android - 从自定义 SurfaceView 获取位图

android - 制作所有16个模拟器组合

iOS UIScrollview 不以自由形式滚动全高度

java - 垂直 recyclerView 和水平 recyclerview 一起滚动

iphone - UIImageView 缩放、点击、手势不起作用

android - 检测单击动态壁纸选择器中的 "Set wallpaper"按钮

java - 如何制作显示 x 轴和 y 轴值的弹出窗口

java - 在 Android 上获取数据数量的 Volley 库是否有任何限制?

Android ScrollView 高度不变

android - ScrollView 不滚动的 RelativeLayout