Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取';滚动视图';从ListView_Android_Android Layout - Fatal编程技术网

Android 获取';滚动视图';从ListView

Android 获取';滚动视图';从ListView,android,android-layout,Android,Android Layout,如何从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

如何从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);

请更清楚地回答您想要实现的目标

  • 为什么要将
    滚动视图
    添加到
    列表视图
  • 在android中,向列表视图中添加
    ScrollView
    是一种糟糕的做法 因为
    listView
    已经提供了滚动功能
  • 如果在
    列表视图中添加
    滚动视图
    ,android会感到困惑 在哪个触摸屏上检测,
    列表视图
    滚动视图
  • 如果要将
    ScrollView
    添加到
    ListView
    中,请重新考虑 因为这不是一个好的实践

从文件中引用 您不应该将
滚动视图
列表视图
一起使用,因为
列表视图
负责自己的垂直滚动。最重要的是,这样做会破坏
ListView
中处理大型列表的所有重要优化,因为它有效地迫使
ListView
显示其整个项目列表,以填充
ScrollView
提供的无限容器

您可以将视图作为页眉和页脚添加到listview中。页眉和页脚将滚动


您也可以尝试解决此问题,从中检查此解决方案,但我不建议这样做,因为这是一种不好的做法,可能会导致性能问题


ExpandableHeightGridView.java:

    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" />
    mAppsGrid = (ExpandableHeightGridView) findViewById(R.id.myId);
    mAppsGrid.setExpanded(true);
像这样将其包含在布局中:

    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" />
    mAppsGrid = (ExpandableHeightGridView) findViewById(R.id.myId);
    mAppsGrid.setExpanded(true);


如果需要帮助,请告诉我

您可以为listview添加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);
}
 });
 }

将OnScrollListener用于ListView和GridView,可以通过以下公式获得“t”

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

            }

        }

尝试在ListView的getView()中执行此操作,而不是使用ListView滚动。它足够强大,可以处理列表上发生的所有更改。我需要一些参数,如参数l Current horizontal scroll origin。t当前垂直滚动原点。oldl上一个水平滚动原点。oldt上一个垂直滚动原点。