ListView内部滚动视图魔术

时间:2013-04-12 10:16:54

标签: android android-listview touch android-actionbar android-scrollview

对于我的应用程序,我想启用一个看起来像这样的东西:

<ScrollView>
    <LinearLayout orientation="vertical">
        <FrameLayout layout_height="48dp">
            Anything I want here...
        </FrameLayout>
        <ListView>
            It's size will be set to screen height anyway
        </ListView>
    </LinearLayout>
</ScrollView>

我想这样做,当用户触摸屏幕滚动时,首先只滚动滚动,然后,当它到达其中一个边缘时,滚动事件转到列表视图并且它开始滚动。

我怎样才能做到这一点?

P.S。这样做的原因是制作某种隐藏动作条,当用户向下移动时滚动离开屏幕,然后当它向上滚动时,它应该从底部出现而不是动画,但是当列表被移动时。所以,如果我将列表的一半高度移动到操作栏 - 我会在屏幕上看到操作栏的下半部分。 您可以在iOS Google+应用中看到我的意思。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:4)

在Layaout中设置参数

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true" >

      <ListView
                    android:id="@+id/listview"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
      </ListView>
</ScrollView>

在编码部分中根据设备高度设置listview的动态高度

    LayoutParams lp = (LayoutParams) list1.getLayoutParams();
    int height = (arraylist.size()) * 80; 

        //arraylist list is in which all data is kept

    lp.height = height;
    listview.setLayoutParams(lp);

    listview.setAdapter(Adapter);

答案 1 :(得分:0)

*     ListView lv = (ListView)findViewById(R.id.myListView);  
      lv.setOnTouchListener(new ListView.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;

            case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }

            // Handle ListView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });k;
            }
            // Handle ListView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });*

答案 2 :(得分:0)

    setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                mFirstVisibleItem = firstVisibleItem;
                mLastVisibleItem = firstVisibleItem + visibleItemCount;
                mTotalOfItems = totalItemCount;
            }
});

我这样做是为了知道我在哪里滚动,使用OnScrollListener和OnTouchListener知道我将滚动的位置,它对我来说很好......

setOnTouchListener(new OnTouchListener() {

        float oldY = -1;

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                oldY = event.getY();
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;

            case MotionEvent.ACTION_UP:
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }

            if (oldY > -1 && 
                    (mFirstVisibleItem == 0 && oldY < event.getY()) || 
                    (mLastVisibleItem >= mTotalOfItems && oldY > event.getY())) {
                v.getParent().requestDisallowInterceptTouchEvent(false);
            }

            v.onTouchEvent(event);

            return true;
        }
    });