如何获得一个' 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);
答案 0 :(得分:0)
请更清楚地了解您要实现的目标!
ScrollView
添加到Listview
?ScrollView
添加到listview是一个不好的做法
因为listView
已经提供了滚动功能scrollView
添加listView
,机器人会感到困惑
在哪个Touch上检测,ListView
或ScrollView
ScrollView
添加到ListView
请重新考虑一下
设计因为这不是一个好习惯 您绝不应将ScrollView
与ListView
一起使用,因为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);
}
}