滚动视图魔法里面的ListView(ListView inside scroll view magi

2019-08-22 06:43发布

对于我的应用我想能够在某种程度上看起来像这样一件事:

<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>

我想这样做,当用户触摸屏幕滚动,起初只是滚动卷轴,然后,当它达到其边缘的一个scroll事件去ListView和它begans滚动。

我怎样才能做到这一点?

PS这样做的原因是为了某种隐藏动作条中,当用户进入更深层次的下降,这滚出屏幕,然后当他滚动起来,应该从底部不是动画而是列表移动出现。 所以,如果我滚动列表中的动作条高度的一半 - 我在屏幕上看到的动作条的下半部分。 你可以明白我的意思在iOS版Google+应用。

任何帮助将不胜感激。

Answer 1:

在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>

在上述编码部分设置根据设备的高度列表视图的动态高度

    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);


Answer 2:

*     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;
        }
    });*


Answer 3:

    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;
        }
    });


文章来源: ListView inside scroll view magic