获取ScrollView'来自ListView

时间:2014-07-30 01:50:33

标签: android android-layout

如何获得一个' ScrollView'来自ListView?

我希望实现该功能

public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
  final int headerHeight = getActivity().findViewById(R.id.fragment_mensagem_list_header)
          .getHeight() - getActivity().getActionBar().getHeight();
  final float ratio = (float) Math.min(Math.max(t, 0), headerHeight) / headerHeight;
  final int newAlpha = (int) (ratio * 255);
  mActionBarBackgroundDrawable.setAlpha(newAlpha);
}

((NotifyingScrollView) LISTVIEWHERE    ).setOnScrollChangedListener(mOnScrollChangedListener);

3 个答案:

答案 0 :(得分:0)

请更清楚地了解您要实现的目标!

  • 为什么要将ScrollView添加到Listview
  • ScrollView添加到listview是一个不好的做法 因为listView已经提供了滚动功能
  • 如果向scrollView添加listView,机器人会感到困惑 在哪个Touch上检测,ListViewScrollView
  • 如果您要将ScrollView添加到ListView请重新考虑一下 设计因为这不是一个好习惯

从文档

引用

您绝不应将ScrollViewListView 一起使用,因为ListView负责自己的垂直滚动。最重要的是,这样做会使ListView中处理大型列表的所有重要优化失败,因为它有效地强制ListView显示其整个项目列表以填充{{{0}提供的无限容器。 1}}。

您可以将自己的观点作为页眉和页脚添加到列表视图中。页眉和页脚将滚动。


你也可以用你的方式解决这个问题,从tacone检查此解决方案,但我不建议这样做,因为它是一种不好的做法,可能会导致性能问题


<强> ExpandableHeightGridView.java:

ScrollView

将其包含在您的布局中,如下所示:

    package com.example;
    public class ExpandableHeightGridView extends GridView
    {

        boolean expanded = false;

        public ExpandableHeightGridView(Context context)
        {
            super(context);
        }

        public ExpandableHeightGridView(Context context, AttributeSet attrs)
        {
            super(context, attrs);
        }

        public ExpandableHeightGridView(Context context, AttributeSet attrs,
                int defStyle)
        {
            super(context, attrs, defStyle);
        }

        public boolean isExpanded()
        {
            return expanded;
        }

        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            // HACK! TAKE THAT ANDROID!
            if (isExpanded())
            {
                // Calculate entire height by providing a very large height hint.
                // View.MEASURED_SIZE_MASK represents the largest height possible.
                int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                        MeasureSpec.AT_MOST);
                super.onMeasure(widthMeasureSpec, expandSpec);

                ViewGroup.LayoutParams params = getLayoutParams();
                params.height = getMeasuredHeight();
            }
            else
            {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        }

        public void setExpanded(boolean expanded)
        {
            this.expanded = expanded;
        }
    }

最后,您只需要让它展开:

    <com.example.ExpandableHeightGridView
        android:id="@+id/myId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:horizontalSpacing="2dp"
        android:isScrollContainer="false"
        android:numColumns="4"
        android:stretchMode="columnWidth"
        android:verticalSpacing="20dp" />

如果您需要任何帮助,请与我们联系!

答案 1 :(得分:0)

您可以为列表视图添加OnScrollListener,并获取滚动状态的回调,如下所示。

yourListViewReference.setOnScrollListener(new OnScrollListener(){
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
  // TODO Auto-generated method stub
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
  // TODO Auto-generated method stub

    final int headerHeight = getActivity().findViewById(R.id.fragment_mensagem_list_header)
      .getHeight() - getActivity().getActionBar().getHeight();
    final float ratio = (float) Math.min(Math.max(t, 0), headerHeight) / headerHeight;
    final int newAlpha = (int) (ratio * 255);
    mActionBarBackgroundDrawable.setAlpha(newAlpha);
}
 });
 }

答案 2 :(得分:0)

使用OnScrollListener for ListView和GridView,你可以获得&#39; t&#39;通过以下公式

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            View c = view.getChildAt(0);
            if (c != null) {
                int t = -c.getTop() + view.getFirstVisiblePosition() * c.getHeight();
                final int headerHeight = mHeaderImage.getHeight() - getActionBar().getHeight();
                final float ratio = (float) Math.min(Math.max(t, 0), headerHeight) / headerHeight;
                final int newAlpha = (int) (ratio * 255);
                ((MainActivity) mActivity).getDrawableBGActionBar().setAlpha(newAlpha);

            }

        }