修复ScrollView上的元素

时间:2015-05-23 19:37:25

标签: 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仅挂钩触摸事件。所以我覆盖onScrollChanged来执行相同的功能(有一些更改)。好,现在元素保持在顶部,但它不跟随滚动运动,它变得“不稳定”,直到滚动停止(投掷和快速滚动速度的结果)。

最好的办法是什么?

1 个答案:

答案 0 :(得分:2)

您可以通过更改嵌套来轻松实现目标。如果将FrameLayout设置为根元素,则可以将叠加层浮动到ScrollView的顶部。您支付的价格是它在布局中出现两次。如果意见不是太多,那可能值得付出。

一旦滚动导航到达视图顶部,您需要编程控制来更改粘性导航的exports.upvoteQuestion = function(req, res, next) { req.question.upvote(function(err, question){ if (err) { return next(err); } res.json(question); }); }; 。您不需要编程控制来保持粘性导航。

visibility

(我已经省略了示例的<!-- 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> 前缀。你确实需​​要它们。)