一个列表视图,其中特定的行总是可以滚动到顶部? [英] A ListView where a specific row can always be scrolled to the top?

查看:121
本文介绍了一个列表视图,其中特定的行总是可以滚动到顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够采取的ListView ,并有特定行可滚动到了顶部 ListView的界限,即使该行已经接近尾声,通常将无法滚动高在一个正常的Andr​​oid 的ListView (类似于当你深入到微博是如何工作的一个特定的鸣叫和鸣叫​​始终是滚动​​的顶端,即使没有什么它下面。)

I want to be able to take a ListView and have a specific row be scrollable to the top of that Listview's bounds, even if the row is near the end and normally wouldn't be able to scroll that high in a normal android ListView (similar to how twitter works when you drill into a specific tweet and that tweet is always scrollable to the top even when there's nothing underneath it.)

有什么办法,我可以轻松地完成这个任务?我已经试过测量我想滚动到顶部和应用底部填充占多余的空间就需要行,但会产生奇怪的结果(我presume因为一个在测量处理过程中改变填充和这样的观点是不明智的)。在这样做之前测量通并不因为有问题的电池(和之后的任何细胞)的测量高度的工作还没有发生。

Is there any way I can accomplish this task easily? I've tried measuring the row i want to scroll to the top and applying bottom padding to account for the extra space it would need, but that yields odd results (i presume because changing padding and such during the measure pass of a view is ill advised). Doing so before the measure pass doesn't work since the measured height of the cell in question (and any cells after it) hasn't happened yet.

推荐答案

我想通了;它有点复杂,但它似乎主要工作:

I figured it out; its a bit complex but it seems to work mostly:

public int usedHeightForAndAfterDesiredRow() {
    int totalHeight = 0;
    for (int index = 0; index < rowHeights.size(); index++) {
        int height = rowHeights.get(rowHeights.keyAt(index));
        totalHeight += height;
    }
    return totalHeight;
}

@Override
public View getView(int position, View convertView, final ViewGroup parent) {
    View view = super.getView(position, convertView, parent);

    if (measuringLayout.getLayoutParams() == null) {
        measuringLayout.setLayoutParams(new AbsListView.LayoutParams(parent.getWidth(), parent.getHeight()));
    }

    // measure the row ahead of time so that we know how much space will need to be added at the end
    if (position >= mainRowPosition && position < getCount()-1 && rowHeights.indexOfKey(position) < 0) {
        measuringLayout.addView(view, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        measuringLayout.measure(MeasureSpec.makeMeasureSpec(parent.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.UNSPECIFIED);

        rowHeights.put(position, view.getMeasuredHeight());
        measuringLayout.removeAllViews();

        view.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    }

    if (position == getCount()-1 && view.getLayoutParams().height == 0) {
        // we know how much height the prior rows take, so calculate the last row with that.
        int height = usedHeightForAndAfterDesiredRow();
        height = Math.max(0, parent.getHeight() - height);
        view.getLayoutParams().height = height;
    }
    return view;
}

这是在我的适配器。这是一个融合适配器的子类,但你可以把它放在你code,并以无论你生成你行的超级电话。

This is in my adapter. It's a subclass of a merge adapter, but you can just put it in your code and substitute the super call with however you generate your rows.

第一如果getView()语句设置了一个框架布局成员VAR是仅用于测量布局PARAMS,它没有父视图。

the first if statement in getView() sets the layout params of a frame layout member var that is only intended for measuring, it has no parent view.

if语句计算所有行高为行,包括和我在乎滚动到顶部行的位置之后的第二。 rowHeights中是一个SparseIntArray。

the second if statement calculates all the row heights for rows including and after the position of the row that I care about scrolling to the top. rowHeights is a SparseIntArray.

if语句假定存在与布局PARAMS一个额外的观点已经设置的意见,其唯一目的是要透明,随意展开列表的底部。在usedHeightForAndAfterDesiredRow电话加起来是从父视图的高度减去(为0分,所以我们没有得到负面的高度)所有的precalculated高度。这结束了上创建它扩展在基于所述其他项目的高度意愿的底部的视图,所以一个特定行总是可以滚动到列表的顶部,无论它是在列表中。

the last if statement assumes that there is one extra view with layout params already set at the bottom of the list of views whose sole intention is to be transparent and expand at will. the usedHeightForAndAfterDesiredRow call adds up all the precalculated heights which is subtracted from the parent view's height (with a min of 0 so we don't get negative heights). this ends up creating a view on the bottom that expands at will based on the heights of the other items, so a specific row can always scroll to the top of the list regardless of where it is in the list.

这篇关于一个列表视图,其中特定的行总是可以滚动到顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆