隐藏查看屏幕外只有时显示用户拉下 [英] Hidden View Offscreen Only to Display When User Pulls Down

查看:140
本文介绍了隐藏查看屏幕外只有时显示用户拉下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,其布局包括其中包含的主要布局的滚动型的(我们将其称为主视图)页面。我使用的是滚动型的原因是,一些较小的屏幕,内容可能需要比屏幕本身更多的垂直空间。我想一个视图中添加或滚动视图上面将提请关闭屏幕(我们将其称为隐藏视图),而当用户拉下时,他们已经在的顶部将只显示滚动型。下面是我描述什么例子:

I have an activity whose layout consists of a ScrollView which contains the main layout (we'll call this the "Main View") of the page. The reason I'm using a ScrollView is that the on some smaller screens, the content may take up more vertical space than the screen itself. I would like to add a view to or above the ScrollView that will be drawn off the screen (we'll call this the "Hidden View"), and will only display when the user pulls down when they are already at the top of the ScrollView. Below are examples of what I'm describing:

之前向下滚动

后向下滚动

此外,为了使问题更加复杂,如果滚动型被滚动到顶部,我想它的隐藏视图映入眼帘前停止。我只希望当用户滚动视图已经滚动到顶部(类似于pulltorefresh)后拉低要显示的隐藏视图。

Also, to make it more complicated, If the ScrollView is being scrolled to the top, I would like it to stop before the "hidden view" comes into view. I only want the "hidden view" to be displayed when the user pulls down after the ScrollView has already been scrolled to the top (similar to pulltorefresh).

我也想用户能够备份到重新隐藏隐藏视图滚动。

I would also like the user to be able to scroll back up to "re-hide" the hidden view.

我不知道最好的方式来实现这一点。我打得周围的pul​​ltorefresh库,但似乎什么,我试图完成太强大了。另外,它不完全是我后的效果。例如,我在下拉后正在加载...屏幕表现不错,但在那之后我想我的隐藏视图,以保留在屏幕上,直到用户把它退回了。如果我能做到这一点使用pulltorefresh,那简直太好了,但我迷路试图按照图书馆的code。

I'm not sure the best way to implement this. I've played around with the pulltorefresh library, but it seems too powerful for what I'm trying to accomplish. Plus, it's not exactly the effect that I'm after. For example, I'm OK with the "Loading..." screen showing after the pulldown, but after that I'd like my "hidden view" to remain on screen until the user pushes it back up. If I can accomplish this using pulltorefresh, that'd be great, but I'm getting lost trying to follow the library's code.

我也认为简单地将隐藏视图的滚动型,并使用 scrollView.fullScroll(ScrollView.FOCUS_DOWN)来强制滚动型滚动至底部。与该问题是,如果该设备的屏幕不是非常小的,有足够的空间,为主视图和隐藏视图同时被显示,并且不需要滚动。此外,隐藏视图亮起屏幕之前,我不会说我的滚动型停止后,我的效果。

I've also considered simply adding the hidden view to the ScrollView, and using scrollView.fullScroll(ScrollView.FOCUS_DOWN) to force the ScrollView to scroll to the bottom. The problem with that is, if the device's screen isn't very small, there is enough room for the "main view" and "hidden view" to be displayed at the same time, and no scrolling is needed. Also, I wouldn't get the effect that I'm after of the ScrollView stopping before the "hidden view" comes on to the screen.

我已经考虑另一种选择就是做上述我,然后调整了主视图迫使滚动型底部之前以匹配设备的屏幕大小。但同样,我也失去了滚动型制动的效果。

The other option I've considered is to do what I described above, then resize the "main view" to match the device's screen size before forcing the ScrollView to the bottom. But again, I would lose the effect of the ScrollView stopping.

我觉得必须有这样做,我很想念的更容易或更优雅的方式。谢谢!

I feel like there has to be an easier or more elegant way of doing this that I'm missing. Thanks!

推荐答案

我的方式做它是通过听,当用户已经滚动到页面的顶部,然后试图再次向上滚动页面。我对在ListView,onScrollListener和onTouchListener两个侦听器之间基本通信。当用户试图在顶部的时候就已经向上滚动,会出现一个搜索框。一旦他们滚动回落消失。

The way I did it was by listening on when the user had scrolled to the top of the page, then tried to scroll up the page again. I had to basically communicate between two listeners on the ListView, onScrollListener and onTouchListener. When the user tries to scroll up when already at the top, a search box appears. Once they scroll back down it disappears.

下面是code:

lstView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                if (listIsAtTop() && expose)
                    searchBox.setVisibility(View.VISIBLE);
                break;
            case MotionEvent.ACTION_DOWN:
                searchBox.setVisibility(View.GONE);
                break;
        }

        return false;
    }
});
lstView.setOnScrollListener(new AbsListView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE && listIsAtTop())
            expose = true;
        else if (!listIsAtTop())
            expose = false;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    }
});

找到

Helper方法时,列表是,在最高层,不能再继续滚动:

Helper method to find when the list is at the very top and can't scroll any further:

private boolean listIsAtTop()   {
   if (lstView.getChildCount() == 0)
       return true;
   return lstView.getChildAt(0).getTop() == 0;
}

然后有一些静态变量在初始设置为true类暴露。为我工作。

Then have some static variable expose in the class that is initially set to true. Worked for me.

这篇关于隐藏查看屏幕外只有时显示用户拉下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆